feat: Support 3 commands slash command

This commit is contained in:
Siwoo Jeon 2024-09-30 21:22:32 +09:00
parent 33102d64dc
commit a0a7adff79
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
4 changed files with 89 additions and 26 deletions

View file

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

View file

@ -1,16 +1,18 @@
import { ApplyOptions } from '@sapphire/decorators'
import {
type SelectMenuComponentOptionData,
type User,
ChatInputCommandInteraction,
ComponentType,
codeBlock,
Message,
} from 'discord.js'
import {
Args,
Command,
container,
DetailedDescriptionCommandObject,
} from '@sapphire/framework'
import {
type SelectMenuComponentOptionData,
type Message,
ComponentType,
codeBlock,
} from 'discord.js'
import { ApplyOptions } from '@sapphire/decorators'
@ApplyOptions<Command.Options>({
name: '삭제',
@ -22,13 +24,36 @@ import { ApplyOptions } from '@sapphire/decorators'
},
})
class DeleteLearnCommand 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('삭제할 단어를 입력해주세요.'),
),
)
}
private async _run(ctx: Message | ChatInputCommandInteraction, args?: Args) {
let command: string | null
let user: User
if (ctx instanceof Message) {
command = await args!.rest('string').catch(() => null)
user = ctx.author
} else {
command = ctx.options.getString('단어', true)
user = ctx.user
}
const CUSTOM_ID = 'maa$deleteLearn'
const command = await args.rest('string').catch(() => null)
const options: SelectMenuComponentOptionData[] = []
const deleteDataList: string[] = []
if (!command) {
return await msg.reply(
return await ctx.reply(
`사용법: \n\`\`\`${(this.detailedDescription as DetailedDescriptionCommandObject).usage}\`\`\``,
)
}
@ -36,12 +61,12 @@ class DeleteLearnCommand extends Command {
const deleteDatas = await this.container.database.learn.findMany({
where: {
command,
user_id: msg.author.id,
user_id: user.id,
},
})
if (!deleteDatas) {
return await msg.reply('해당하는 걸 찾ㅈ을 수 없어요.')
return await ctx.reply('해당하는 걸 찾ㅈ을 수 없어요.')
}
for (let i = 1; i <= deleteDatas.length; i++) {
@ -53,7 +78,7 @@ class DeleteLearnCommand extends Command {
})
}
await msg.reply({
await ctx.reply({
embeds: [
{
title: '삭제',
@ -67,7 +92,7 @@ class DeleteLearnCommand extends Command {
components: [
{
type: ComponentType.StringSelect,
customId: `${CUSTOM_ID}@${msg.author.id}`,
customId: `${CUSTOM_ID}@${user.id}`,
placeholder: '지울 데이터를 선택해ㅈ주세요',
options,
},
@ -76,6 +101,14 @@ class DeleteLearnCommand extends Command {
],
})
}
public async messageRun(msg: Message, args: Args) {
await this._run(msg, args)
}
public async chatInputRun(interaction: ChatInputCommandInteraction) {
await this._run(interaction)
}
}
void container.stores.loadPiece({

View file

@ -1,6 +1,6 @@
import { ChatInputCommandInteraction, Message } from 'discord.js'
import { Command, container } from '@sapphire/framework'
import { ApplyOptions } from '@sapphire/decorators'
import { type Message } from 'discord.js'
@ApplyOptions<Command.Options>({
name: '데이터학습량',
@ -11,14 +11,21 @@ import { type Message } from 'discord.js'
},
})
class LearnDataCommand extends Command {
public async messageRun(msg: Message<true>) {
public registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand(builder =>
builder.setName(this.name).setDescription(this.description),
)
}
private async _run(ctx: Message | ChatInputCommandInteraction) {
const user = ctx instanceof Message ? ctx.author : ctx.user
const db = this.container.database
const data = await db.statement.findMany()
const nsfwData = await db.nsfw_content.findMany()
const learnData = await db.learn.findMany()
const userData = await db.learn.findMany({
where: {
user_id: msg.author.id,
user_id: user.id,
},
})
const muffin: any[] = []
@ -27,10 +34,18 @@ class LearnDataCommand extends Command {
else return
})
await msg.reply(`머핀 데이터: ${muffin.length}
await ctx.reply(`머핀 데이터: ${muffin.length}
nsfw 데이터: ${nsfwData.length}
단어: ${learnData.length}
${msg.author.username} 단어: ${userData.length}`)
${user.username} 단어: ${userData.length}`)
}
public async messageRun(msg: Message) {
await this._run(msg)
}
public async chatInputRun(interaction: ChatInputCommandInteraction) {
await this._run(interaction)
}
}

View file

@ -1,5 +1,5 @@
import { ChatInputCommandInteraction, Message, codeBlock } from 'discord.js'
import { ApplyOptions } from '@sapphire/decorators'
import { Message, codeBlock } from 'discord.js'
import { Command, container } from '@sapphire/framework'
@ApplyOptions<Command.Options>({
@ -11,27 +11,34 @@ import { Command, container } from '@sapphire/framework'
},
})
class ListCommand extends Command {
public async messageRun(msg: Message<boolean>) {
public registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand(builder =>
builder.setName(this.name).setDescription(this.description),
)
}
private async _run(ctx: Message | ChatInputCommandInteraction) {
const user = ctx instanceof Message ? ctx.author : ctx.user
const db = this.container.database
const data = await db.learn.findMany({
where: {
user_id: msg.author.id,
user_id: user.id,
},
})
const list: string[] = []
if (!data[0]) {
return await msg.reply('당신ㄴ은 단어를 가르쳐준 기억이 없ㅅ는데요.')
return await ctx.reply('당신ㄴ은 단어를 가르쳐준 기억이 없ㅅ는데요.')
}
for (const listData of data) {
list.push(listData.command)
}
await msg.reply({
await ctx.reply({
embeds: [
{
title: `${msg.author.username}님의 지식`,
title: `${user.username}님의 지식`,
description: `총합: ${data.length}\n${codeBlock(
'md',
list.map(item => `- ${item}`).join('\n'),
@ -42,6 +49,14 @@ class ListCommand extends Command {
],
})
}
public async messageRun(msg: Message<boolean>) {
await this._run(msg)
}
public async chatInputRun(interaction: ChatInputCommandInteraction) {
await this._run(interaction)
}
}
void container.stores.loadPiece({