feat: Support slash command for learn command

This commit is contained in:
Siwoo Jeon 2024-10-01 13:50:15 +09:00
parent a0a7adff79
commit db701a7756
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
3 changed files with 79 additions and 28 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "muffinbot", "name": "muffinbot",
"version": "4.0.0-pudding.experimental_slash_test.4", "version": "4.0.0-pudding.experimental_slash_test.5",
"main": "dist/index.js", "main": "dist/index.js",
"private": true, "private": true,
"dependencies": { "dependencies": {

View file

@ -22,7 +22,7 @@ container.version = version
container.database = new PrismaClient() container.database = new PrismaClient()
container.dokdoAliases = ['dokdo', 'dok', 'Dokdo', 'Dok', '테스트'] container.dokdoAliases = ['dokdo', 'dok', 'Dokdo', 'Dok', '테스트']
container.chatBot = new ChatBot(container.database) container.chatBot = new ChatBot(container.database)
container.lastUpdated = new Date('2024-09-29') container.lastUpdated = new Date('2024-10-01')
if (release.startsWith('e')) { if (release.startsWith('e')) {
container.channel = 'EXPERIMENTAL' container.channel = 'EXPERIMENTAL'

View file

@ -1,5 +1,5 @@
import { ChatInputCommandInteraction, codeBlock, Message } from 'discord.js'
import { type Args, Command, container } from '@sapphire/framework' import { type Args, Command, container } from '@sapphire/framework'
import { codeBlock, type Message } from 'discord.js'
import { ApplyOptions } from '@sapphire/decorators' import { ApplyOptions } from '@sapphire/decorators'
@ApplyOptions<Command.Options>({ @ApplyOptions<Command.Options>({
@ -16,32 +16,69 @@ import { ApplyOptions } from '@sapphire/decorators'
}, },
}) })
class LearnCommand extends Command { class LearnCommand extends Command {
public async messageRun(msg: Message, args: Args) { public registerApplicationCommands(registry: Command.Registry) {
if (typeof this.detailedDescription === 'string') return registry.registerChatInputCommand(builder =>
const config = this.container.config builder
const command = (await args.pick('string').catch(() => null))?.replaceAll( .setName(this.name)
'_', .setDescription(this.description)
' ', .addStringOption(option =>
) option
const result = (await args.pick('string').catch(() => null))?.replaceAll( .setRequired(true)
'_', .setName('단어')
' ', .setDescription('등록할 단어를 입력해주세요.'),
) )
if (!command || !result) { .addStringOption(option =>
return await msg.reply( option
codeBlock( .setRequired(true)
'md', .setName('대답')
`사용법: ${this.detailedDescription} .setDescription('해당 단어의 대답을 입력해주세요.'),
예시: ${this.detailedDescription.examples?.map(example => example).join('\n')}`,
), ),
)
}
private async _run(ctx: Message | ChatInputCommandInteraction, args?: Args) {
if (ctx instanceof ChatInputCommandInteraction) ctx.deferReply()
if (typeof this.detailedDescription === 'string') return
const config = this.container.config
const IG_MSG = '해ㄷ당 단어는 배울ㄹ 수 없어요.'
const DI_MSG = '해당 단ㅇ어는 개발자님이 특별히 금지하였ㅇ어요.'
const SUCCESS_MSG = '을/를 배웠ㅇ어요.'
let command: string
let result: string
if (ctx instanceof Message) {
command = (await args!.pick('string').catch(() => null))!.replaceAll(
'_',
' ',
) )
result = (await args!.pick('string').catch(() => null))!.replaceAll(
'_',
' ',
)
if (!command || !result)
return await ctx.reply(
codeBlock(
'md',
`사용법: ${this.detailedDescription}
예시: ${this.detailedDescription.examples?.map(example => example).join('\n')}`,
),
)
} else {
command = ctx.options.getString('단어', true)
result = ctx.options.getString('대답', true)
} }
const commands: string[] = [] const commands: string[] = []
const aliases: string[] = [] const aliases: string[] = []
this.container.stores.get('commands').forEach(module => { this.container.stores.get('commands').forEach(module => {
commands.push(module.name) commands.push(module.name)
module.aliases.forEach(alias => aliases.push(alias)) module.aliases.forEach(alias => aliases.push(alias))
}) })
const ignore = [ const ignore = [
...commands, ...commands,
...aliases, ...aliases,
@ -52,27 +89,41 @@ class LearnCommand extends Command {
'간미', '간미',
] ]
const disallowed = ['@everyone', '@here', `<@${config.bot.owner_ID}>`] const disallowed = ['@everyone', '@here', `<@${config.bot.owner_ID}>`]
const user = ctx instanceof Message ? ctx.author : ctx.user
for (const ig of ignore) { for (const ig of ignore) {
if (command.includes(ig)) { if (command.includes(ig))
return msg.reply('해ㄷ당 단어는 배울ㄹ 수 없어요.') return ctx instanceof Message
} ? await ctx.reply(IG_MSG)
: await ctx.editReply(IG_MSG)
} }
for (const di of disallowed) { for (const di of disallowed) {
if (result.includes(di)) { if (result.includes(di))
return msg.reply('해당 단ㅇ어는 개발자님이 특별히 금지하였ㅇ어요.') return ctx instanceof Message
} ? await ctx.reply(DI_MSG)
: await ctx.editReply(DI_MSG)
} }
await this.container.database.learn.create({ await this.container.database.learn.create({
data: { data: {
user_id: msg.author.id, user_id: user.id,
command, command,
result, result,
}, },
}) })
await msg.reply(`${command}을/를 배웠ㅇ어요.`)
return ctx instanceof Message
? await ctx.reply(command + SUCCESS_MSG)
: await ctx.editReply(command + SUCCESS_MSG)
}
public async messageRun(msg: Message, args: Args) {
await this._run(msg, args)
}
public async chatInputRun(interaction: ChatInputCommandInteraction) {
await this._run(interaction)
} }
} }