diff --git a/.gitignore b/.gitignore index c3b5058..05437cc 100644 --- a/.gitignore +++ b/.gitignore @@ -129,6 +129,9 @@ dist .yarn/install-state.gz .pnp.* +# config file +config.json + # database db/ diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..2f2fe89 --- /dev/null +++ b/config.example.json @@ -0,0 +1,16 @@ +{ + "bot": { + "owner_ID": "", + "token": "" + }, + "train": { + "user_ID": "" + }, + "mysql": { + "user": "", + "host": "", + "password": "", + "database": "", + "port": 3306 + } +} diff --git a/example.env b/example.env deleted file mode 100644 index 79802a9..0000000 --- a/example.env +++ /dev/null @@ -1,8 +0,0 @@ -DISCORD_TOKEN='' -MYSQL_USER='' -MYSQL_HOST='' -MYSQL_PASSWORD='' -MYSQL_DATABASE='' -MYSQL_PORT='' -OWNER_ID='' -TRAIN_USER_ID='' diff --git a/src/Client.ts b/src/Client.ts index 2e91be3..f3517b6 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -8,8 +8,8 @@ import { import { Command, noPerm, ChatBot, NODE_ENV } from './modules' import { readdirSync } from 'node:fs' import { join } from 'node:path' -import 'dotenv/config' import { execSync } from 'node:child_process' +import config from '../config.json' const prefix = '멒힌아 ' @@ -70,13 +70,13 @@ export default class MuffinAI extends Client { if (NODE_ENV === 'development') console.log(args) const command = this.#modules.get(args.shift()!.toLowerCase()) if (!command) return - if (command.noPerm && msg.author.id !== process.env.OWNER_ID) + if (command.noPerm && msg.author.id !== config.bot.owner_ID) return await noPerm(msg) command.execute(msg, args) } }) - return super.login() + return super.login(config.bot.token) } public override destroy() { diff --git a/src/Commands/learning_data.ts b/src/Commands/learning_data.ts index 0d3e67f..d5c1917 100644 --- a/src/Commands/learning_data.ts +++ b/src/Commands/learning_data.ts @@ -6,7 +6,7 @@ export default class extends Command { super('학습데이터량') } public async execute(msg: Message, args: string[]) { - const db = await msg.client.chatBot.db + const db = await msg.client.chatBot.db.getConnection() const [rows] = await db.execute('SELECT * FROM statement;') const [nsfw] = await db.execute('SELECT * FROM nsfw_content;') const muffin: ResponseData[] = [] @@ -17,5 +17,6 @@ export default class extends Command { msg.channel.send( `머핀 데이터: ${muffin.length}개\nnsfw 데이터: ${nsfw.length}` ) + db.release() } } diff --git a/src/modules/ChatBot.ts b/src/modules/ChatBot.ts index 1fd0a6c..fd6f841 100644 --- a/src/modules/ChatBot.ts +++ b/src/modules/ChatBot.ts @@ -1,6 +1,7 @@ import type { Client, Message } from 'discord.js' import database, { ResponseData } from './database' import { TextChannel } from 'discord.js' +import config from '../../config.json' export default class ChatBot { get db() { @@ -8,9 +9,7 @@ export default class ChatBot { } #db = database public async getResponse(msg: Message): Promise { - const db = await this.db - const request = msg.content.replace('머핀아 ', '') - console.log(`req: ${request}`) + const db = await this.db.getConnection() const [rows] = await db.execute('SELECT * FROM statement;') let response: string if ((msg.channel as TextChannel).nsfw) { @@ -23,15 +22,15 @@ export default class ChatBot { response = rows[Math.floor(Math.random() * rows.length)].text } if (!response) response = '살ㄹ려주세요' - console.log(`res: ${response}`) + db.release() return response } - public train(client: Client): ChatBot { + public async train(client: Client): Promise { + const db = await this.db.getConnection() client.on('messageCreate', async msg => { if (msg.author.bot) return - const db = await this.db - if (msg.author.id === process.env.TRAIN_USER_ID) { + if (msg.author.id === config.train.user_ID) { const response = await this.getResponse(msg) const [rows] = await db.execute( 'SELECT * FROM statement;' @@ -68,10 +67,11 @@ export default class ChatBot { } } }) + db.release() return this } public async destroy() { - this.db.then(db => db.destroy()) + this.db.end() } } diff --git a/src/modules/database.ts b/src/modules/database.ts index 641f524..9782943 100644 --- a/src/modules/database.ts +++ b/src/modules/database.ts @@ -1,9 +1,5 @@ -import { - createConnection, - RowDataPacket, - ConnectionOptions, -} from 'mysql2/promise' -import 'dotenv/config' +import { RowDataPacket, createPool } from 'mysql2/promise' +import config from '../../config.json' export interface BaseData extends RowDataPacket { id: number @@ -19,14 +15,6 @@ export interface ResponseData extends BaseData { search_in_response_to: string } -export const config: ConnectionOptions = { - host: process.env.MYSQL_HOST, - 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, -} - export { BaseData as NSFWData } -export default createConnection(config) +export default createPool(config.mysql) diff --git a/src/modules/index.ts b/src/modules/index.ts index e8d06f3..0c9caef 100644 --- a/src/modules/index.ts +++ b/src/modules/index.ts @@ -1,15 +1,6 @@ import ChatBot from './ChatBot' import Command from './Command' -import database, { ResponseData, config, NSFWData } from './database' +import database, { ResponseData, NSFWData } from './database' import noPerm from './noPerm' import { NODE_ENV } from './env' -export { - ChatBot, - Command, - database, - noPerm, - ResponseData, - config, - NODE_ENV, - NSFWData, -} +export { ChatBot, Command, database, noPerm, ResponseData, NODE_ENV, NSFWData } diff --git a/tsconfig.json b/tsconfig.json index deb77a8..98306af 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -37,7 +37,7 @@ // "types": [], /* Specify type package names to be included without being referenced in a source file. */ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ - // "resolveJsonModule": true, /* Enable importing .json files. */ + "resolveJsonModule": true, /* Enable importing .json files. */ // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ /* JavaScript Support */