diff --git a/package.json b/package.json index 9a057aa..61cfeb7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "muffinbot", - "version": "3.0.0-cake.d240624a", + "version": "3.0.0-cake.d240624b", "main": "dist/index.js", "private": true, "dependencies": { diff --git a/src/Commands/deleteLearn.ts b/src/Commands/deleteLearn.ts index 59d8d7a..3a79400 100644 --- a/src/Commands/deleteLearn.ts +++ b/src/Commands/deleteLearn.ts @@ -19,74 +19,55 @@ import { type LearnData } from '../modules' }) export default class extends Command { public async messageRun(msg: Message, args: Args) { - const db = this.container.database const command = await args.rest('string').catch(() => null) - - if (!command) { - return await msg.channel.send('사용법: \n```머핀아 삭제 (지울 단어)```') - } - + const options: SelectMenuComponentOptionData[] = [] + const db = this.container.database const [datas] = await db.database.execute( 'SELECT * FROM learn WHERE command = ? AND user_id = ?;', [command, msg.author.id], ) + if (!command) { + return await msg.channel.send('사용법: \n```머핀아 삭제 (지울 단어)```') + } + if (!datas) { return await msg.channel.send('해당하는 걸 찾ㅈ을 수 없어요.') } - console.log(datas.length) - - if (datas.length > 1) { - const options: SelectMenuComponentOptionData[] = [] - - datas.forEach(data => { - console.log(data) - options.push({ - label: `${data.id}번`, - value: `maa$deleteLearn-${data.id}`, - description: data.result.slice(0, 100), - }) + datas.forEach(data => { + console.log(data) + options.push({ + label: `${data.id}번`, + value: `maa$deleteLearn-${data.id}`, + description: data.result.slice(0, 100), }) + }) - await msg.reply({ - embeds: [ - { - title: '삭제', - description: `${codeBlock( - 'md', - datas.map(data => `${data.id}. ${data.result}`).join('\n'), - )}`, - timestamp: new Date().toISOString(), - }, - ], - components: [ - { - type: ComponentType.ActionRow, - components: [ - { - type: ComponentType.StringSelect, - customId: 'maa$deleteLearn', - placeholder: '지울 데이터를 선택해ㅈ주세요', - options, - }, - ], - }, - ], - }) - } else { - // await db.learn.delete(command) - // await msg.reply('어라 이제 그ㄱ게 기억이 안나요. 그게 뭐ㅇ였죠?') - console.log('b') - } - - // if (data[0].user_id !== msg.author.id) { - // return await msg.channel.send( - // '어라 당ㅅ신은 언제 가르쳐 주셨죠?', - // ) - // } - // - // await db.learn.delete(command) - // await msg.reply('어라 이제 그ㄱ게 기억이 안나요.') + await msg.reply({ + embeds: [ + { + title: '삭제', + description: `${codeBlock( + 'md', + datas.map(data => `${data.id}. ${data.result}`).join('\n'), + )}`, + timestamp: new Date().toISOString(), + }, + ], + components: [ + { + type: ComponentType.ActionRow, + components: [ + { + type: ComponentType.StringSelect, + customId: 'maa$deleteLearn', + placeholder: '지울 데이터를 선택해ㅈ주세요', + options, + }, + ], + }, + ], + }) } } diff --git a/src/interaction-handlers/deleteLearn.ts b/src/interaction-handlers/deleteLearn.ts new file mode 100644 index 0000000..abbc485 --- /dev/null +++ b/src/interaction-handlers/deleteLearn.ts @@ -0,0 +1,36 @@ +import { + InteractionHandler, + InteractionHandlerTypes, +} from '@sapphire/framework' +import { type StringSelectMenuInteraction } from 'discord.js' +import { ApplyOptions } from '@sapphire/decorators' + +@ApplyOptions({ + interactionHandlerType: InteractionHandlerTypes.SelectMenu, +}) +export default class extends InteractionHandler { + public async parse(interaction: StringSelectMenuInteraction) { + if (interaction.customId !== 'maa$deleteLearn') return this.none() + return this.some() + } + + public async run(interaction: StringSelectMenuInteraction) { + await interaction.deferUpdate() + + const id = interaction.values[0].slice('maa$deleteLearn-'.length) + const db = this.container.database + + await db.learn.delete(id) + + await interaction.editReply({ + embeds: [ + { + title: '삭제', + description: `${id}번을 정상적으로 삭제하ㅇ였어요.`, + timestamp: new Date().toISOString(), + }, + ], + components: [], + }) + } +} diff --git a/src/modules/database/database.ts b/src/modules/database/database.ts index b4fff8e..ada4d47 100644 --- a/src/modules/database/database.ts +++ b/src/modules/database/database.ts @@ -1,10 +1,9 @@ import { LearnTable, NSFWContentTable, StatementTable } from './model' import { createPool, type QueryResult } from 'mysql2/promise' import { container } from '@sapphire/framework' -import run from './run' export class MaaDatabase { - public database = createPool({ + public readonly database = createPool({ ...container.config.mysql, keepAliveInitialDelay: 10000, enableKeepAlive: true, diff --git a/src/modules/database/model/learn.ts b/src/modules/database/model/learn.ts index a1221ae..051885e 100644 --- a/src/modules/database/model/learn.ts +++ b/src/modules/database/model/learn.ts @@ -55,7 +55,7 @@ export class LearnTable implements BaseTable { const db = await this._database.getConnection() await run(db, async () => { - await db.execute('DELETE FROM learn WHERE command = ?;', [key]) + await db.execute('DELETE FROM learn WHERE id = ?;', [key]) }) } diff --git a/tsup.config.ts b/tsup.config.ts index 118be0e..5c59f20 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -5,7 +5,12 @@ const sourcemap = process.env.NODE_ENV === 'development' ? true : false export default defineConfig({ clean: true, format: ['cjs'], - entry: ['src/index.ts', 'src/commands/*.ts', 'src/listeners/*.ts'], + entry: [ + 'src/index.ts', + 'src/Commands/*.ts', + 'src/listeners/*.ts', + 'src/interaction-handlers/*.ts', + ], minify: true, sourcemap, })