From 42519e6d46d74841ebef5b8424ee1dc787d988a0 Mon Sep 17 00:00:00 2001 From: Migan178 Date: Sat, 2 Dec 2023 23:33:55 +0900 Subject: [PATCH] . --- scripts/create_table.sql | 3 ++- src/Commands/deleteLearn.ts | 33 +++------------------------------ src/Commands/learn.ts | 30 +++++------------------------- src/Commands/list.ts | 2 +- src/modules/ChatBot.ts | 21 ++++++++++++++++----- src/modules/database.ts | 1 + 6 files changed, 28 insertions(+), 62 deletions(-) diff --git a/scripts/create_table.sql b/scripts/create_table.sql index c42a502..3ac2fb0 100755 --- a/scripts/create_table.sql +++ b/scripts/create_table.sql @@ -22,9 +22,10 @@ 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(`command`) + primary key(`id`) ); \ No newline at end of file diff --git a/src/Commands/deleteLearn.ts b/src/Commands/deleteLearn.ts index 4c7582a..9972c86 100644 --- a/src/Commands/deleteLearn.ts +++ b/src/Commands/deleteLearn.ts @@ -1,40 +1,13 @@ import { Message } from 'discord.js' -import { Command, LearnData } from '../modules' +import { Command } from '../modules' export default class extends Command { public constructor() { super('삭제') } public async execute(msg: Message, args: string[]) { - 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], + msg.channel.send( + '현재는 사용할 수 없는 기능이예요. 만약 삭제를 원할 경우 개발자에게 문의해주세요.', ) - - 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 6e415b5..ad3182d 100644 --- a/src/Commands/learn.ts +++ b/src/Commands/learn.ts @@ -33,16 +33,6 @@ 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('해ㄷ당 단어는 배울ㄹ 수 없어요.') @@ -57,21 +47,11 @@ export default class extends Command { try { await db.beginTransaction() - - 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.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}을/를 배웠ㅇ어요.`) await db.commit() } catch (err) { console.error(err) diff --git a/src/Commands/list.ts b/src/Commands/list.ts index 5a263bd..a8f51f2 100644 --- a/src/Commands/list.ts +++ b/src/Commands/list.ts @@ -33,7 +33,7 @@ export default class extends Command { list.map(item => `- ${item}`).join('\n'), ), color: 0x0000ff, - timestamp: new Date().toISOString() + timestamp: new Date().toISOString(), }, ], }) diff --git a/src/modules/ChatBot.ts b/src/modules/ChatBot.ts index ec94e31..746c8f1 100644 --- a/src/modules/ChatBot.ts +++ b/src/modules/ChatBot.ts @@ -30,11 +30,22 @@ export default class ChatBot { if (a === 1) { if (learn[0]) { - if (args.startsWith(learn[0].command)) { - db.release() - return `${learn[0].result}\n\`${ - (await msg.client.users.fetch(learn[0].user_id)).username - }님이 알려주셨어요.\`` + 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 + }님이 알려주셨어요.\`` + } } } } diff --git a/src/modules/database.ts b/src/modules/database.ts index da19b7a..5648fc9 100644 --- a/src/modules/database.ts +++ b/src/modules/database.ts @@ -17,6 +17,7 @@ export interface ResponseData extends BaseData { } export interface LearnData extends RowDataPacket { + id: number command: string result: string user_id: Snowflake