From c107f1d7d9bc314c80983322f8d30bc4d07e4edc Mon Sep 17 00:00:00 2001 From: Migan178 Date: Tue, 18 Jun 2024 21:56:00 +0900 Subject: [PATCH] fix: Too Many Connections --- package.json | 2 +- src/Client.ts | 28 ++++++++++------------------ src/modules/ChatBot.ts | 8 +++----- src/modules/database/database.ts | 24 +++++++++++------------- 4 files changed, 25 insertions(+), 37 deletions(-) diff --git a/package.json b/package.json index f1ab171..810c166 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "muffinbot", - "version": "2.0.0-oreo.r240616b", + "version": "2.0.0-oreo.r240618a", "main": "dist/index.js", "private": true, "dependencies": { diff --git a/src/Client.ts b/src/Client.ts index f239551..660d033 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -14,22 +14,14 @@ import Dokdo from 'dokdo' const prefix = '머핀아 ' export default class MuffinBot extends Client { - get chatBot() { - return new ChatBot() - } - - get dokdo() { - return new Dokdo(this, { - aliases: ['dokdo', 'dok'], - owners: [config.bot.owner_ID], - noPerm, - prefix, - }) - } - get modules(): Collection { - return this.#modules - } - #modules: Collection = new Collection() + public chatBot = new ChatBot() + public dokdo = new Dokdo(this, { + aliases: ['dokdo', 'dok'], + owners: [config.bot.owner_ID], + noPerm, + prefix, + }) + public modules: Collection = new Collection() public constructor() { super({ intents: [ @@ -98,7 +90,7 @@ export default class MuffinBot extends Client { declare module 'discord.js' { interface Client { - get chatBot(): ChatBot - get modules(): Collection + chatBot: ChatBot + modules: Collection } } diff --git a/src/modules/ChatBot.ts b/src/modules/ChatBot.ts index 21e6b32..e34e703 100644 --- a/src/modules/ChatBot.ts +++ b/src/modules/ChatBot.ts @@ -1,13 +1,11 @@ -import type { Client, Message } from 'discord.js' +import type { Client, Message, TextChannel } from 'discord.js' import { MaaDatabase } from './database' -import { TextChannel } from 'discord.js' import config from '../../config.json' import { NODE_ENV } from '.' export default class ChatBot { - get db() { - return new MaaDatabase() - } + public db = new MaaDatabase() + public async getResponse(msg: Message): Promise { const data = await this.db.statement.all() const args = msg.content diff --git a/src/modules/database/database.ts b/src/modules/database/database.ts index 49bd683..fbbbcfa 100644 --- a/src/modules/database/database.ts +++ b/src/modules/database/database.ts @@ -1,27 +1,25 @@ import { createPool } from 'mysql2/promise' import { LearnTable, NSFWContentTable, StatementTable } from './model' import config from '../../../config.json' +import run from './run' export class MaaDatabase { private _database = createPool({ ...config.mysql, keepAliveInitialDelay: 10000, enableKeepAlive: true, + }).on('release', conn => { + console.log(`${conn.threadId} released.`) }) - - public get statement() { - return new StatementTable(this._database) - } - - public get nsfwContent() { - return new NSFWContentTable(this._database) - } - - public get learn() { - return new LearnTable(this._database) - } + public statement = new StatementTable(this._database) + public nsfwContent = new NSFWContentTable(this._database) + public learn = new LearnTable(this._database) public async ping() { - this._database.ping() + const db = await this._database.getConnection() + + await run(db, async () => { + await db.ping() + }) } }