feat: rewrite finish learnData

This commit is contained in:
Siwoo Jeon 2024-06-24 18:13:23 +09:00
parent 3c2bc0f238
commit 177527fcc0
Signed by: migan
GPG key ID: C4151385FFD2082A
6 changed files with 83 additions and 62 deletions

View file

@ -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": {

View file

@ -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<LearnData[]>(
'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,
},
],
},
],
})
}
}

View file

@ -0,0 +1,36 @@
import {
InteractionHandler,
InteractionHandlerTypes,
} from '@sapphire/framework'
import { type StringSelectMenuInteraction } from 'discord.js'
import { ApplyOptions } from '@sapphire/decorators'
@ApplyOptions<InteractionHandler.Options>({
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: [],
})
}
}

View file

@ -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,

View file

@ -55,7 +55,7 @@ export class LearnTable implements BaseTable<LearnData, string> {
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])
})
}

View file

@ -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,
})