From e50a348686dfe9c1d1b98d76bda19e30289b43d9 Mon Sep 17 00:00:00 2001 From: Migan178 Date: Fri, 24 Nov 2023 23:20:57 +0900 Subject: [PATCH] . --- src/Commands/learn.ts | 46 ++++++++++++++++++++++++++++++++---------- src/modules/ChatBot.ts | 19 +++++++++++++---- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/Commands/learn.ts b/src/Commands/learn.ts index 3c2bdbb..7c0d754 100644 --- a/src/Commands/learn.ts +++ b/src/Commands/learn.ts @@ -1,4 +1,4 @@ -import { Command } from '../modules' +import { Command, LearnData } from '../modules' import { Message } from 'discord.js' export default class extends Command { @@ -20,26 +20,50 @@ export default class extends Command { ] const disallowed = ['@everyone', '@here'] const db = await msg.client.chatBot.db.getConnection() + const [learn] = await db.execute( + 'SELECT * FROM learn WHERE command = ?;', + [command], + ) - ignore.forEach(ig => { - if (result.includes(ig)) { + 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('해ㄷ당 단어는 배울ㄹ 수 없어요.') } - }) + } - disallowed.forEach(di => { + for (const di of disallowed) { if (result.includes(di)) { return msg.channel.send('해당 단ㅇ어는 금지되어 있ㅅ어요.') } - }) + } try { await db.beginTransaction() - db.execute( - 'INSERT INTO learn (command, result, user_id) VALUES (?, ?, ?);', - [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/modules/ChatBot.ts b/src/modules/ChatBot.ts index edd1313..6252e5f 100644 --- a/src/modules/ChatBot.ts +++ b/src/modules/ChatBot.ts @@ -12,8 +12,15 @@ export default class ChatBot { public async getResponse(msg: Message): Promise { const db = await this.db.getConnection() const [rows] = await db.execute('SELECT * FROM statement;') - const args = msg.content.slice('머핀아 '.length).trim().split(/ +/g) - const [learn] = await db.execute('SELECT * from learn;') + const args = msg.content + .slice('머핀아 '.length) + .trim() + .split(/ +/g) + .join(' ') + const [learn] = await db.execute( + 'SELECT * from learn WHERE command = ?;', + [args], + ) const a = Math.round(Math.random() * (2 - 1) + 1) if (NODE_ENV === 'development') { @@ -22,9 +29,13 @@ export default class ChatBot { } if (a === 1) { - if (args[0].startsWith(learn[0].command)) { - return `${learn[0].command}\n\`${(await msg.client.users.fetch(msg.author.id)).username}님이 알려주셨어요.\`` + if (learn[0]) { + if (args.startsWith(learn[0].command)) { + return `${learn[0].result}\n\`${ + (await msg.client.users.fetch(learn[0].user_id)).username + }님이 알려주셨어요.\`` } + } } let response: string