From a4fd2390c5b3a5044a313ad2f68519dc6f0bcd40 Mon Sep 17 00:00:00 2001 From: Migan178 Date: Fri, 24 Nov 2023 12:40:27 +0900 Subject: [PATCH] feat: add delete learn --- src/deleteLearn.ts | 32 ++++++++++++++++++++++++++++++++ src/modules/database.ts | 10 +++++++++- src/modules/index.ts | 4 ++-- 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/deleteLearn.ts diff --git a/src/deleteLearn.ts b/src/deleteLearn.ts new file mode 100644 index 0000000..e031e21 --- /dev/null +++ b/src/deleteLearn.ts @@ -0,0 +1,32 @@ +import { Message } from 'discord.js' +import { Command, LearnData } from './modules' + +export default class extends Command { + public constructor() { + super('삭제') + } + public async execute(msg: Message, args: string[]) { + const db = await msg.client.chatBot.db.getConnection() + const command = args[0] + const [rows] = await db.execute('SELECT * FROM learn WHERE command = ?;', [command]) + + if (!rows[0]) { + await msg.channel.send('해당하는 걸 찾ㅈ을 수 없어요.') + } + + if (rows[0].user_id !== msg.author.id) { + await msg.channel.send('당신ㄴ은 해당 지식을 안가르ㄹ쳐 주셨ㅅ는데요?') + } + + try { + await db.beginTransaction() + await db.execute('DELETE FROM learn WHERE command = ?;', [command]) + await msg.channel.send('해당 단어를 삭ㄱ제했어요.') + await db.commit() + } catch (err) { + console.error(err) + } finally { + db.release() + } + } +} diff --git a/src/modules/database.ts b/src/modules/database.ts index dd4c861..da19b7a 100644 --- a/src/modules/database.ts +++ b/src/modules/database.ts @@ -1,5 +1,6 @@ -import { RowDataPacket, createPool } from 'mysql2/promise' +import { type RowDataPacket, createPool } from 'mysql2/promise' import config from '../../config.json' +import { type Snowflake } from 'discord.js' export interface BaseData extends RowDataPacket { id: number @@ -15,6 +16,13 @@ export interface ResponseData extends BaseData { search_in_response_to: string } +export interface LearnData extends RowDataPacket { + command: string + result: string + user_id: Snowflake + created_at: string +} + export { BaseData as NSFWData } const database = createPool({ diff --git a/src/modules/index.ts b/src/modules/index.ts index 0c9caef..0d8af77 100644 --- a/src/modules/index.ts +++ b/src/modules/index.ts @@ -1,6 +1,6 @@ import ChatBot from './ChatBot' import Command from './Command' -import database, { ResponseData, NSFWData } from './database' +import database, { ResponseData, NSFWData, LearnData } from './database' import noPerm from './noPerm' import { NODE_ENV } from './env' -export { ChatBot, Command, database, noPerm, ResponseData, NODE_ENV, NSFWData } +export { ChatBot, Command, database, noPerm, ResponseData, NODE_ENV, NSFWData, LearnData }