From 72ac25f7733fe3d4879c79bd820edd369adabdbb Mon Sep 17 00:00:00 2001 From: Migan178 Date: Sat, 2 Dec 2023 23:59:31 +0900 Subject: [PATCH] rollback --- scripts/create_table.sql | 3 +-- src/Commands/deleteLearn.ts | 33 ++++++++++++++++++++++++++--- src/Commands/learn.ts | 30 +++++++++++++++++++++------ src/Commands/list.ts | 41 +++++++++++++++++++++++++++++++++++++ src/modules/ChatBot.ts | 21 +++++-------------- src/modules/database.ts | 1 - 6 files changed, 101 insertions(+), 28 deletions(-) create mode 100644 src/Commands/list.ts diff --git a/scripts/create_table.sql b/scripts/create_table.sql index 3ac2fb0..c42a502 100755 --- a/scripts/create_table.sql +++ b/scripts/create_table.sql @@ -22,10 +22,9 @@ CREATE TABLE CREATE TABLE learn ( - id int(11) NOT NULL, command varchar(255) NOT NULL, result varchar(255) NOT NULL, user_id varchar(255) NOT NULL, created_at datetime NOT NULL DEFAULT current_timestamp(), - primary key(`id`) + primary key(`command`) ); \ No newline at end of file diff --git a/src/Commands/deleteLearn.ts b/src/Commands/deleteLearn.ts index 9972c86..4c7582a 100644 --- a/src/Commands/deleteLearn.ts +++ b/src/Commands/deleteLearn.ts @@ -1,13 +1,40 @@ import { Message } from 'discord.js' -import { Command } from '../modules' +import { Command, LearnData } from '../modules' export default class extends Command { public constructor() { super('삭제') } public async execute(msg: Message, args: string[]) { - msg.channel.send( - '현재는 사용할 수 없는 기능이예요. 만약 삭제를 원할 경우 개발자에게 문의해주세요.', + if (!args[0]) { + return await msg.channel.send('```멒힌아 삭제 (지울 단어)```') + } + 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]) { + return await msg.channel.send('해당하는 걸 찾ㅈ을 수 없어요.') + } + + if (rows[0].user_id !== msg.author.id) { + return 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/Commands/learn.ts b/src/Commands/learn.ts index ad3182d..9e0ce8b 100644 --- a/src/Commands/learn.ts +++ b/src/Commands/learn.ts @@ -33,6 +33,16 @@ export default class extends Command { [command], ) + if (learn[0]) { + if (msg.author.id !== learn[0].user_id) { + return msg.channel.send( + `해ㄷ당 단어는 이미 ${ + (await msg.client.users.fetch(learn[0].user_id)).username + }님에게서 배웠어요.`, + ) + } + } + for (const ig of ignore) { if (command.includes(ig)) { return msg.channel.send('해ㄷ당 단어는 배울ㄹ 수 없어요.') @@ -46,12 +56,20 @@ export default class extends Command { } try { - await db.beginTransaction() - await db.execute( - 'INSERT INTO learn (id, command, result, user_id) VALUES (?, ?, ?, ?);', - [++learn[learn.length - 1].id, command, result, msg.author.id], - ) - await msg.channel.send(`${command}을/를 배웠ㅇ어요.`) + if (learn[0] && msg.author.id === learn[0].user_id) { + await db.execute('UPDATE learn SET result = ? WHERE command = ?;', [ + result, + command, + ]) + await msg.channel.send(`${command}을/를 다시 배ㅂ웠어요.`) + } else { + await db.execute( + 'INSERT INTO learn (command, result, user_id) VALUES (?, ?, ?);', + [command, result, msg.author.id], + ) + await msg.channel.send(`${command}을/를 배웠ㅇ어요.`) + } + await db.commit() } catch (err) { console.error(err) diff --git a/src/Commands/list.ts b/src/Commands/list.ts new file mode 100644 index 0000000..a8f51f2 --- /dev/null +++ b/src/Commands/list.ts @@ -0,0 +1,41 @@ +import { Message, codeBlock } 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 [rows] = await db.execute( + 'SELECT * FROM learn WHERE user_id = ?;', + [msg.author.id], + ) + const list: string[] = [] + + if (!rows) { + return await msg.channel.send( + '당신ㄴ은 단어를 가르쳐준 기억이 없ㅅ는데요.', + ) + } + + for (const data of rows) { + list.push(data.command) + } + + await msg.channel.send({ + embeds: [ + { + title: '지식', + description: codeBlock( + 'md', + list.map(item => `- ${item}`).join('\n'), + ), + color: 0x0000ff, + timestamp: new Date().toISOString(), + }, + ], + }) + } +} diff --git a/src/modules/ChatBot.ts b/src/modules/ChatBot.ts index 746c8f1..ec94e31 100644 --- a/src/modules/ChatBot.ts +++ b/src/modules/ChatBot.ts @@ -30,22 +30,11 @@ export default class ChatBot { if (a === 1) { if (learn[0]) { - if (learn[1]) { - if (args.startsWith(learn[0].command)) { - const response = - learn[Math.floor(Math.random() * learn.length)].result - db.release() - return `${response}\n\`${ - (await msg.client.users.fetch(learn[0].user_id)).username - }님이 알려주셨어요.\`` - } - } else { - if (args.startsWith(learn[0].command)) { - db.release() - return `${learn[0].result}\n\`${ - (await msg.client.users.fetch(learn[0].user_id)).username - }님이 알려주셨어요.\`` - } + if (args.startsWith(learn[0].command)) { + db.release() + return `${learn[0].result}\n\`${ + (await msg.client.users.fetch(learn[0].user_id)).username + }님이 알려주셨어요.\`` } } } diff --git a/src/modules/database.ts b/src/modules/database.ts index 5648fc9..da19b7a 100644 --- a/src/modules/database.ts +++ b/src/modules/database.ts @@ -17,7 +17,6 @@ export interface ResponseData extends BaseData { } export interface LearnData extends RowDataPacket { - id: number command: string result: string user_id: Snowflake