From 011467782d8fce5818b32aa81443508ed9d0f4cf Mon Sep 17 00:00:00 2001 From: Migan178 Date: Mon, 6 Feb 2023 20:21:58 +0900 Subject: [PATCH] . --- example.env | 3 ++- src/Client.ts | 4 +-- src/Commands/학습데이터량.ts | 17 ++++++------- src/modules/ChatBot.ts | 48 ++++++++++++++++++------------------ src/modules/Database.ts | 1 + 5 files changed, 36 insertions(+), 37 deletions(-) diff --git a/example.env b/example.env index f72d772..df35bf9 100644 --- a/example.env +++ b/example.env @@ -2,4 +2,5 @@ DISCORD_TOKEN='BOT_TOKEN' MYSQL_USER='MYSQL_USER' MYSQL_HOST='MYSQL_HOST' MYSQL_PASSWORD='MYSQL_PASSWORD' -MYSQL_DATABASE='MYSQL_DATABASE' \ No newline at end of file +MYSQL_DATABASE='MYSQL_DATABASE' +MYSQL_PORT='MYSQL_PORT' \ No newline at end of file diff --git a/src/Client.ts b/src/Client.ts index d5691cb..48e68db 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -35,8 +35,8 @@ export default class MuffinAI extends Client { this.#modules.set(b.name, b) }) - this.once('ready', client => { - client.user!.setActivity({ + this.once('ready', () => { + this.user!.setActivity({ type: ActivityType.Playing, name: 'ㅅ살려주세요..!', }) diff --git a/src/Commands/학습데이터량.ts b/src/Commands/학습데이터량.ts index 3971bd8..97d39b6 100644 --- a/src/Commands/학습데이터량.ts +++ b/src/Commands/학습데이터량.ts @@ -7,16 +7,13 @@ export default class extends Command { } public async execute(msg: Message, args: string[]) { const conn = await database.getConnection() - await conn - .query('SELECT * FROM statement;') // - .then(rows => { - const muffin: ResponseData[] = [] - ;(rows[0] as ResponseData[]).forEach(row => { - if (row.persona === 'muffin') muffin.push(row) - else return - }) - msg.channel.send(`머핀 데이터: ${muffin.length}개`) - }) + const [rows] = await conn.query('SELECT * FROM statement;') + const muffin: ResponseData[] = [] + ;(rows as ResponseData[]).forEach(row => { + if (row.persona === 'muffin') muffin.push(row) + else return + }) + msg.channel.send(`머핀 데이터: ${muffin.length}개`) conn.release() } } diff --git a/src/modules/ChatBot.ts b/src/modules/ChatBot.ts index 18eb2bb..6fadeee 100644 --- a/src/modules/ChatBot.ts +++ b/src/modules/ChatBot.ts @@ -5,39 +5,39 @@ export default class ChatBot { public async getResponse(msg: Message): Promise { const conn = await database.getConnection() const request = msg.content.replace('머핀아 ', '') - const result = await conn.query('SELECT * FROM statement;') - const rows = result[0] as ResponseData[] - let response = rows[Math.floor(Math.random() * rows.length)].text - if (!response) response = '살ㄹ려주세요' console.log(`⌨️ㅣ${request}`) + const [rows] = await conn.query('SELECT * FROM statement;') + let response = (rows as ResponseData[])[ + Math.floor(Math.random() * (rows as ResponseData[]).length) + ].text + if (!response) response = '살ㄹ려주세요' + console.log(`🍰ㅣ${response}`) conn.release() return response } public async train(client: Client): Promise { - const conn = await database.getConnection() - client.on('messageCreate', msg => { + client.on('messageCreate', async msg => { + const conn = await database.getConnection() if (msg.author.bot) return if (msg.author.id !== '1026185545837191238') return - this.getResponse(msg) // - .then(async response => { - const result = await conn.query('SELECT * FROM statement;') - const rows = result[0] as ResponseData[] - try { - await conn.beginTransaction() - await conn.execute( - `INSERT INTO statement(id, text, persona, in_response_to) VALUES(?, ?, ?, ?);`, - [++rows[rows.length - 1].id, msg.content, 'muffin', response] - ) - await conn.commit() - } catch (err) { - console.log(err) - await conn.rollback() - } finally { - conn.release() - } - }) + const response = this.getResponse(msg) + const result = await conn.query('SELECT * FROM statement;') + const rows = result[0] as ResponseData[] + await conn.beginTransaction() + try { + await conn.execute( + `INSERT INTO statement(id, text, persona, in_response_to) VALUES(?, ?, ?, ?);`, + [++rows[rows.length - 1].id, msg.content, 'muffin', response] + ) + await conn.commit() + } catch (err) { + console.log(err) + await conn.rollback() + } finally { + conn.release() + } }) return this } diff --git a/src/modules/Database.ts b/src/modules/Database.ts index 0e9a1d7..1629b56 100644 --- a/src/modules/Database.ts +++ b/src/modules/Database.ts @@ -17,4 +17,5 @@ export default createPool({ user: process.env.MYSQL_USER, password: process.env.MYSQL_PASSWORD, database: process.env.MYSQL_DATABASE, + port: (process.env.MYSQL_PORT as unknown as number) || 3306, })