feat: Support slash command for learn command
This commit is contained in:
parent
a0a7adff79
commit
db701a7756
3 changed files with 79 additions and 28 deletions
|
@ -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": {
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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<Command.Options>({
|
||||
|
@ -16,32 +16,69 @@ import { ApplyOptions } from '@sapphire/decorators'
|
|||
},
|
||||
})
|
||||
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
|
||||
|
||||
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(
|
||||
'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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue