From db701a77560c30aedf260182d546fa911c48c1be Mon Sep 17 00:00:00 2001 From: Siwoo Jeon Date: Tue, 1 Oct 2024 13:50:15 +0900 Subject: [PATCH] feat: Support slash command for learn command --- package.json | 2 +- src/Client.ts | 2 +- src/Commands/learn.ts | 103 +++++++++++++++++++++++++++++++----------- 3 files changed, 79 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index f88e42e..930fe1b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "muffinbot", - "version": "4.0.0-pudding.experimental_slash_test.4", + "version": "4.0.0-pudding.experimental_slash_test.5", "main": "dist/index.js", "private": true, "dependencies": { diff --git a/src/Client.ts b/src/Client.ts index 562966b..074b4a8 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -22,7 +22,7 @@ container.version = version container.database = new PrismaClient() container.dokdoAliases = ['dokdo', 'dok', 'Dokdo', 'Dok', '테스트'] container.chatBot = new ChatBot(container.database) -container.lastUpdated = new Date('2024-09-29') +container.lastUpdated = new Date('2024-10-01') if (release.startsWith('e')) { container.channel = 'EXPERIMENTAL' diff --git a/src/Commands/learn.ts b/src/Commands/learn.ts index 1f0879d..2e2bc69 100644 --- a/src/Commands/learn.ts +++ b/src/Commands/learn.ts @@ -1,5 +1,5 @@ +import { ChatInputCommandInteraction, codeBlock, Message } from 'discord.js' import { type Args, Command, container } from '@sapphire/framework' -import { codeBlock, type Message } from 'discord.js' import { ApplyOptions } from '@sapphire/decorators' @ApplyOptions({ @@ -16,32 +16,69 @@ import { ApplyOptions } from '@sapphire/decorators' }, }) class LearnCommand extends Command { - public async messageRun(msg: Message, args: Args) { - if (typeof this.detailedDescription === 'string') return - const config = this.container.config - const command = (await args.pick('string').catch(() => null))?.replaceAll( - '_', - ' ', - ) - const result = (await args.pick('string').catch(() => null))?.replaceAll( - '_', - ' ', - ) - if (!command || !result) { - return await msg.reply( - codeBlock( - 'md', - `사용법: ${this.detailedDescription} - 예시: ${this.detailedDescription.examples?.map(example => example).join('\n')}`, + public registerApplicationCommands(registry: Command.Registry) { + registry.registerChatInputCommand(builder => + builder + .setName(this.name) + .setDescription(this.description) + .addStringOption(option => + option + .setRequired(true) + .setName('단어') + .setDescription('등록할 단어를 입력해주세요.'), + ) + .addStringOption(option => + option + .setRequired(true) + .setName('대답') + .setDescription('해당 단어의 대답을 입력해주세요.'), ), + ) + } + + 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 aliases: string[] = [] + this.container.stores.get('commands').forEach(module => { commands.push(module.name) module.aliases.forEach(alias => aliases.push(alias)) }) + const ignore = [ ...commands, ...aliases, @@ -52,27 +89,41 @@ class LearnCommand extends Command { '간미', ] const disallowed = ['@everyone', '@here', `<@${config.bot.owner_ID}>`] + const user = ctx instanceof Message ? ctx.author : ctx.user for (const ig of ignore) { - if (command.includes(ig)) { - return msg.reply('해ㄷ당 단어는 배울ㄹ 수 없어요.') - } + if (command.includes(ig)) + return ctx instanceof Message + ? await ctx.reply(IG_MSG) + : await ctx.editReply(IG_MSG) } for (const di of disallowed) { - if (result.includes(di)) { - return msg.reply('해당 단ㅇ어는 개발자님이 특별히 금지하였ㅇ어요.') - } + if (result.includes(di)) + return ctx instanceof Message + ? await ctx.reply(DI_MSG) + : await ctx.editReply(DI_MSG) } await this.container.database.learn.create({ data: { - user_id: msg.author.id, + user_id: user.id, command, 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) } }