feat: improve mention handling in bot and mention services

This commit is contained in:
devilreef 2025-11-22 12:38:48 +06:00
parent e54dee08c8
commit 1f1985c1ac
2 changed files with 24 additions and 23 deletions

View file

@ -1,9 +1,9 @@
import { join } from 'node:path'
import { autoload } from '@gramio/autoload'
import { Bot, mention } from 'gramio'
import { Bot } from 'gramio'
import env from '@/shared/env.js'
import { RoleService } from '@/shared/services/role.js'
import { MentionService } from '@/shared/services/mention.js'
import { RoleService } from '@/shared/services/role.js'
let botUsername: string | undefined
@ -15,10 +15,6 @@ export const bot = new Bot(env.botToken)
export type BotType = typeof bot
bot.on('message', async (ctx, next) => {
// if (ctx.chatId !== -1003212318013) {
// return
// }
if (!ctx.hasEntities('mention')) {
return next()
}
@ -27,14 +23,14 @@ bot.on('message', async (ctx, next) => {
if (!mentions.length) {
return next()
}
const rolesToMention = await Promise.all(mentions.map((mention) => RoleService.getBySlugOrAlias(mention.slice(1), ctx.chatId))).then((roles) => roles.filter((role) => role !== null))
const rolesToMention = await Promise.all(mentions.map(mention => RoleService.getBySlugOrAlias(mention.slice(1), ctx.chatId))).then(roles => roles.filter(role => role !== null))
if (!rolesToMention.length) {
return next()
}
for (const role of rolesToMention) {
await MentionService.mentionAll(role.slug, ctx.chatId, ctx.from, ctx.replyMessage?.id ?? undefined)
await MentionService.mentionAll(role.slug, ctx.chatId, ctx.from, ctx.replyMessage?.id ?? ctx.id)
}
})
@ -74,7 +70,7 @@ bot.onStart(async ({ info }) => {
await bot.api.setMyCommands({
scope: {
type: 'default'
type: 'default',
},
commands: [
{ command: 'roleadd', description: 'Add a new role in this chat' },
@ -84,6 +80,6 @@ bot.onStart(async ({ info }) => {
{ command: 'myroles', description: 'View your roles in this chat' },
{ command: 'roles', description: 'View all roles in this chat' },
],
suppress: true
suppress: true,
})
})

View file

@ -1,7 +1,8 @@
import { bold, format, join, mention, TelegramUser, User } from 'gramio'
import type { User } from 'gramio'
import { bold, format, italic, join, mention } from 'gramio'
import { bot } from '@/bot/index.js'
import { chunk } from '../utilities/chunk.js'
import { RoleService } from './role.js'
import { bot } from '@/bot/index.js'
export class MentionService {
static async mentionAll(query: string, chatId: number, caller: User, replyMessageId?: number) {
@ -14,26 +15,30 @@ export class MentionService {
const membersChunks = chunk(members, 5)
for (const memberChunk of membersChunks) {
const message = format`
${bold('Mass mention by')} ${bold(mention('user', { id: caller.id, is_bot: caller.isBot(), first_name: caller.firstName }))}
const chunkIdx = membersChunks.indexOf(memberChunk)
${join(this.buildMentions(memberChunk), (entity) => entity, ' ')}
const message = format`
${bold('Mass mention by')} ${bold(mention('user', { id: caller.id, is_bot: caller.isBot(), first_name: caller.firstName }))} ${chunkIdx > 0 ? italic(`[${chunkIdx + 1}/${membersChunks.length}]`) : ''}
${join(this.buildMentions(memberChunk), entity => entity, ' ')}
`
await bot.api.sendMessage({
chat_id: role.chatId,
text: message,
...(replyMessageId ? {
reply_parameters: {
message_id: replyMessageId,
allow_sending_without_reply: true
}
}: {})
...(replyMessageId
? {
reply_parameters: {
message_id: replyMessageId,
allow_sending_without_reply: true,
},
}
: {}),
})
}
}
static buildMentions (ids: number[]) {
return ids.map((id) => mention('👋', { id, is_bot: false, first_name: '' }))
static buildMentions(ids: number[]) {
return ids.map(id => mention('👋', { id, is_bot: false, first_name: '' }))
}
}