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) {
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 if (typeof this.detailedDescription === 'string') return
const config = this.container.config const config = this.container.config
const command = (await args.pick('string').catch(() => null))?.replaceAll( 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(
'_', '_',
' ', ' ',
) )
const result = (await args.pick('string').catch(() => null))?.replaceAll( result = (await args!.pick('string').catch(() => null))!.replaceAll(
'_', '_',
' ', ' ',
) )
if (!command || !result) {
return await msg.reply( if (!command || !result)
return await ctx.reply(
codeBlock( codeBlock(
'md', 'md',
`사용법: ${this.detailedDescription} `사용법: ${this.detailedDescription}
예시: ${this.detailedDescription.examples?.map(example => example).join('\n')}`, 예시: ${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)
} }
} }