diff --git a/package.json b/package.json index 79a6004..4b79ef5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "muffinbot", - "version": "3.0.0-cake.d240622a", + "version": "3.0.0-cake.d240622b", "main": "dist/index.js", "private": true, "dependencies": { @@ -25,7 +25,7 @@ }, "scripts": { "build": "tsup", - "dev": "yarn build && cross-env NODE_ENV=development node dist", + "dev": "cross-env NODE_ENV=development tsup --watch --onSuccess \"node dist\"", "start": "cross-env NODE_ENV=production node dist" }, "packageManager": "yarn@4.2.2" diff --git a/src/Client.ts b/src/Client.ts index 0f4ff92..887e72a 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -1,25 +1,14 @@ -import { noPerm, ChatBot, NODE_ENV, MaaDatabase } from './modules' -import { SapphireClient, container } from '@sapphire/framework' -import { ActivityType, GatewayIntentBits, Snowflake } from 'discord.js' +import { SapphireClient, container, LogLevel } from '@sapphire/framework' +import { GatewayIntentBits, type Snowflake } from 'discord.js' +import { ChatBot, NODE_ENV, MaaDatabase } from './modules' import config from '../config.json' -import Dokdo from 'dokdo' +container.config = config container.prefix = '머핀아 ' container.database = new MaaDatabase() container.chatBot = new ChatBot(container.database) -container.config = config export default class MuffinBot extends SapphireClient { - public database = container.database - public chatBot = container.chatBot - public prefix = container.prefix - public dokdo: Dokdo = new Dokdo(this, { - aliases: ['dokdo', 'dok'], - owners: [config.bot.owner_ID], - noPerm, - prefix: container.prefix, - }) - public constructor() { super({ intents: [ @@ -29,53 +18,18 @@ export default class MuffinBot extends SapphireClient { ], loadMessageCommandListeners: true, defaultPrefix: container.prefix, + logger: { + level: NODE_ENV === 'development' ? LogLevel.Debug : LogLevel.Info, + }, }) } public override async login(): Promise { - if (NODE_ENV === 'development') this.on('debug', console.info) - await this.chatBot.train(this) - - this.once('ready', client => { - function setStatus() { - client.user.setActivity({ - type: ActivityType.Custom, - name: 'ㅅ살려주세요..!', - }) - } - - setStatus() - setInterval(() => setStatus(), 600000) - - console.log(`먹힐 준ㅂ비 완료`) - }).on('messageCreate', async msg => { - if (msg.author.bot) return - if (msg.content.startsWith(this.prefix)) { - const args = msg.content.slice(this.prefix.length).trim().split(/ +/g) - - if (args[0].startsWith('dokdo') || args[0].startsWith('dok')) { - await this.dokdo.run(msg) - } else { - if (!this.stores.get('commands').get(args[0])) { - await msg.channel.sendTyping() - const response = await this.chatBot.getResponse(msg) - await msg.reply(response) - } - } - } - }) + await container.chatBot.train(this) return super.login(config.bot.token) } } -declare module 'discord.js' { - interface Client { - chatBot: ChatBot - prefix: string - dokdo: Dokdo - } -} - declare module '@sapphire/pieces' { interface Container { database: MaaDatabase diff --git a/src/listeners/debug.ts b/src/listeners/debug.ts new file mode 100644 index 0000000..4f73e61 --- /dev/null +++ b/src/listeners/debug.ts @@ -0,0 +1,7 @@ +import { Listener } from '@sapphire/framework' + +export default class extends Listener { + public async run(debug: string) { + this.container.logger.debug(debug) + } +} diff --git a/src/listeners/messageCreate.ts b/src/listeners/messageCreate.ts new file mode 100644 index 0000000..b7bc59e --- /dev/null +++ b/src/listeners/messageCreate.ts @@ -0,0 +1,32 @@ +import { Listener } from '@sapphire/framework' +import { type Message } from 'discord.js' +import { noPerm } from '../modules' +import Dokdo from 'dokdo' + +export default class extends Listener { + public async run(msg: Message) { + const dokdo = new Dokdo(this.container.client, { + aliases: ['dokdo', 'dok'], + owners: [this.container.config.bot.owner_ID], + noPerm, + prefix: this.container.prefix, + }) + if (msg.author.bot) return + if (msg.content.startsWith(this.container.prefix)) { + const args = msg.content + .slice(this.container.prefix.length) + .trim() + .split(/ +/g) + + if (args[0].startsWith('dokdo') || args[0].startsWith('dok')) { + await dokdo.run(msg) + } else { + if (!this.container.stores.get('commands').get(args[0])) { + await msg.channel.sendTyping() + const response = await this.container.chatBot.getResponse(msg) + await msg.reply(response) + } + } + } + } +} diff --git a/src/listeners/ready.ts b/src/listeners/ready.ts new file mode 100644 index 0000000..1d39733 --- /dev/null +++ b/src/listeners/ready.ts @@ -0,0 +1,20 @@ +import { ApplyOptions } from '@sapphire/decorators' +import { Listener } from '@sapphire/framework' +import { ActivityType, Client } from 'discord.js' + +@ApplyOptions({ once: true }) +export default class extends Listener { + public async run(client: Client) { + function setStatus() { + client.user.setActivity({ + type: ActivityType.Custom, + name: 'ㅅ살려주세요..!', + }) + } + + setStatus() + setInterval(() => setStatus(), 600000) + + this.container.logger.info(`[MuffinBotClient] 먹힐 준ㅂ비 완료`) + } +} diff --git a/src/modules/ChatBot.ts b/src/modules/ChatBot.ts index 1dbad86..0219c58 100644 --- a/src/modules/ChatBot.ts +++ b/src/modules/ChatBot.ts @@ -1,7 +1,6 @@ import type { Client, Message, TextChannel } from 'discord.js' +import { container } from '@sapphire/framework' import type { MaaDatabase } from './database' -import config from '../../config.json' -import { NODE_ENV } from './env' export default class ChatBot { public constructor(public db: MaaDatabase) { @@ -11,20 +10,19 @@ export default class ChatBot { } public async getResponse(msg: Message): Promise { - const prefix = msg.client.prefix + const prefix = container.prefix const data = await this.db.statement.all() const args = msg.content.slice(prefix.length).trim().split(/ +/g).join(' ') const learn = await this.db.learn.findOne(args) const learnData = learn[Math.floor(Math.random() * learn.length)] const randomNumber = Math.round(Math.random() * (2 - 1) + 1) - if (NODE_ENV === 'development') { - console.log(randomNumber) - console.log(learnData) - console.log(args) - } + container.logger.debug(`[ChatBot] command: ${args}`) if (randomNumber === 1 && learnData && args.startsWith(learnData.command)) { + container.logger.debug( + `[ChatBot] response: (learnData) ${learnData.result}`, + ) return `${learnData.result}\n\`${ (await msg.client.users.fetch(learnData.user_id)).username }님이 알려주셨어요.\`` @@ -38,15 +36,19 @@ export default class ChatBot { } else { response = data[Math.floor(Math.random() * data.length)].text } + if (!response) response = '살ㄹ려주세요' + + container.logger.debug(`[ChatBot] response: ${response}`) + return response } public async train(client: Client): Promise { - const prefix = client.prefix + const prefix = container.prefix client.on('messageCreate', async msg => { if (msg.author.bot) return - if (msg.author.id === config.train.user_ID) { + if (msg.author.id === container.config.train.user_ID) { const response = await this.getResponse(msg) await this.db.statement.insert({ text: msg.content, diff --git a/src/modules/database/database.ts b/src/modules/database/database.ts index 7086d4b..880f3bf 100644 --- a/src/modules/database/database.ts +++ b/src/modules/database/database.ts @@ -1,14 +1,20 @@ -import { createPool } from 'mysql2/promise' import { LearnTable, NSFWContentTable, StatementTable } from './model' -import config from '../../../config.json' +import { container } from '@sapphire/framework' +import { createPool } from 'mysql2/promise' import run from './run' export class MaaDatabase { private _database = createPool({ - ...config.mysql, + ...container.config.mysql, keepAliveInitialDelay: 10000, enableKeepAlive: true, }) + .on('release', conn => { + container.logger.debug(`[MaaDatabase] ${conn.threadId} Released.`) + }) + .on('connection', conn => { + container.logger.debug(`[MaaDatabase] ${conn.threadId} Connected.`) + }) public statement = new StatementTable(this._database) public nsfwContent = new NSFWContentTable(this._database) public learn = new LearnTable(this._database) diff --git a/tsup.config.ts b/tsup.config.ts index 47817fd..aa6589f 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -3,7 +3,7 @@ import { defineConfig } from 'tsup' export default defineConfig({ clean: true, format: ['cjs'], - entry: ['src/index.ts', 'src/Commands/*.ts'], + entry: ['src/index.ts', 'src/commands/*.ts', 'src/listeners/*.ts'], // minify: true, // splitting: true, })