From 6fc5eae5cdd896e135b1a62894b8a517ada1d699 Mon Sep 17 00:00:00 2001 From: Migan178 Date: Sat, 11 Feb 2023 15:03:25 +0900 Subject: [PATCH] feat: nsfw only user train --- src/Client.ts | 9 ++++++--- src/modules/ChatBot.ts | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/Client.ts b/src/Client.ts index d61ff2b..e405ed5 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -13,7 +13,10 @@ import 'dotenv/config' const prefix = '멒힌아 ' export default class MuffinAI extends Client { - public chatBot = new ChatBot() + get chatBot() { + return this.#chatBot + } + #chatBot = new ChatBot() #modules: Collection = new Collection() public constructor() { super({ @@ -46,7 +49,7 @@ export default class MuffinAI extends Client { if (msg.author.bot) return if (msg.content.startsWith('머핀아 ')) { if (msg.channel instanceof TextChannel) { - if (msg.channel.nsfw) return + // if (msg.channel.nsfw) return await msg.channel.sendTyping() this.chatBot // .getResponse(msg) @@ -81,6 +84,6 @@ export default class MuffinAI extends Client { declare module 'discord.js' { interface Client { - chatBot: ChatBot + get chatBot(): ChatBot } } diff --git a/src/modules/ChatBot.ts b/src/modules/ChatBot.ts index 9c75b3a..47ddbd7 100644 --- a/src/modules/ChatBot.ts +++ b/src/modules/ChatBot.ts @@ -1,15 +1,28 @@ import type { Client, Message } from 'discord.js' import database, { ResponseData } from './database' +import { TextChannel } from 'discord.js' export default class ChatBot { - public db = database + get db() { + return this.#db + } + #db = database public async getResponse(msg: Message): Promise { const db = await this.db const request = msg.content.replace('머핀아 ', '') console.log(`req: ${request}`) const [rows] = await db.execute('SELECT * FROM statement;') - let response = rows[Math.floor(Math.random() * rows.length)].text - if (!response) response = '살ㄹ려주세요' + let response: string + if ((msg.channel as TextChannel).nsfw) { + const [rows2] = await db.execute( + 'SELECT * FROM nsfw_content;' + ) + response = [...rows, ...rows2][Math.floor(Math.random() * rows.length)] + .text + } else { + response = rows[Math.floor(Math.random() * rows.length)].text + if (!response) response = '살ㄹ려주세요' + } console.log(`res: ${response}`) return response } @@ -34,6 +47,25 @@ export default class ChatBot { console.log(err) await db.rollback() } + } else { + if (!(msg.channel as TextChannel).nsfw) return + if (!msg.content.startsWith('머핀아 ')) return + const user = `user:${msg.author.username.slice(0, 50).toLowerCase()}` + const text = msg.content.replace('머핀아 ', '') + const [rows] = await db.execute( + 'SELECT * FROM nsfw_content;' + ) + try { + await db.beginTransaction() + await db.execute( + `INSERT INTO nsfw_content (id, text, persona) VALUES (?, ?, ?);`, + [++rows[rows.length - 1].id, text, user] + ) + await db.commit() + } catch (err) { + console.log(err) + await db.rollback() + } } }) return this