roleping-bot/src/bot/commands/admin/role-delete.ts
devilreef 85c7410ffb refactor: simplify codebase with unified patterns and reduced duplication
- unify ChatConfigService setters into generic setPermission method
- replace PermissionService switch statements with data-driven checkers map
- deduplicate config.ts callback handlers via loop-based registration (414→254 lines)
- extract shared updateChatCommands utility from role-add/role-delete
- add proper generics to chunk utility, fix mention loop indexing
- extract isHiddenAdmin helper and ANONYMOUS_ADMIN_ID constant in perms
- remove debug console.logs, standardize arrow exports and type imports
- add roleAdminPermission schema and migration
2026-02-17 21:27:35 +06:00

42 lines
1.4 KiB
TypeScript

import type { BotType } from '@/bot/index.js'
import { bold, code, format } from 'gramio'
import { updateChatCommands } from '@/bot/utilities/commands.js'
import { PermissionService } from '@/shared/services/permission.js'
import { RoleService } from '@/shared/services/role.js'
export default (bot: BotType) => {
bot.command('roledel', async (ctx) => {
if (!['group', 'supergroup'].includes(ctx.chat.type)) {
return ctx.reply('this command can only be used in groups or supergroups')
}
const canManage = await PermissionService.canManageRoles(ctx.chat.id, ctx.from.id)
if (!canManage) {
return ctx.reply('You don\'t have permission to manage roles')
}
let [roleSlug] = (ctx.args ?? '').trim().split(' ')
if (!roleSlug) {
return ctx.reply(format`where is the role name bro?\nusage: ${code('/roledel <name>')}`)
}
roleSlug = roleSlug.toLowerCase()
const role = await RoleService.getBySlugOrAlias(roleSlug, ctx.chatId)
if (!role) {
return ctx.reply('role not found')
}
const members = await RoleService.getMemberIds(roleSlug, ctx.chatId)
await Promise.all(members.map(member => RoleService.removeMember(role.id!, member)))
const deletedRole = await RoleService.delete(role.id!)
if (!deletedRole) {
return ctx.reply('failed to delete role, skill issue')
}
updateChatCommands(bot, ctx.chatId)
return ctx.reply(format`role ${bold(role.slug)} deleted`)
})
}