roleping-bot/src/bot/commands/user/join.ts
2025-11-21 19:04:14 +06:00

43 lines
1.4 KiB
TypeScript

import type { BotType } from '@/bot/index.js'
import { bold, code, format, italic } from 'gramio'
import { ROLE_NAME_REGEX } from '@/bot/utilities/constants.js'
import { RoleService } from '@/shared/services/role.js'
export default (bot: BotType) => {
bot.command('join', async (ctx) => {
if (!['group', 'supergroup'].includes(ctx.chat.type)) {
return ctx.reply('This command can only be used in groups or supergroups')
}
let [slug] = (ctx.args ?? '').trim().split(' ')
slug = slug.toLowerCase()
if (!slug) {
return ctx.reply(format`where is role name bro?\nusage: ${code('/join <name>')}`)
}
if (!ROLE_NAME_REGEX.test(slug)) {
return ctx.reply(format`invalid role name\n${italic('it should be 4-32 characters long and contain only letters')}`)
}
const role = await RoleService.getBySlugOrAlias(slug, ctx.chatId)
console.log(role)
if (!role) {
return ctx.reply('role not found')
}
const isMember = await RoleService.getMemberById(role.id!, ctx.chatId, ctx.from.id)
console.log(isMember)
if (isMember !== null) {
return ctx.reply('you are already in this role')
}
const newMember = await RoleService.addMember(role.id!, ctx.from.id)
if (!newMember) {
return ctx.reply('failed to join role, skill issue')
}
return ctx.reply(format`you have joined the role ${bold(role.slug)}`)
})
}