diff --git a/package.json b/package.json index f2d7119..24a39f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "muffinbot", - "version": "2.0.0-oreo.p240608a", + "version": "2.1.0-oreo.d240609a", "main": "dist/index.js", "private": true, "dependencies": { diff --git a/src/Client.ts b/src/Client.ts index f239551..303c336 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -14,22 +14,20 @@ import Dokdo from 'dokdo' const prefix = '머핀아 ' export default class MuffinBot extends Client { + #modules: Collection = new Collection() 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 dokdo: Dokdo = new Dokdo(this, { + aliases: ['dokdo', 'dok'], + owners: [config.bot.owner_ID], + noPerm, + prefix, + }) + public prefix = prefix public constructor() { super({ intents: [ @@ -75,19 +73,17 @@ export default class MuffinBot extends Client { if (args[0].startsWith('dokdo') || args[0].startsWith('dok')) { await this.dokdo.run(msg) } else { - if (msg.channel instanceof TextChannel) { - await msg.channel.sendTyping() - const command = this.modules.get(args.shift()!.toLowerCase()) + await msg.channel.sendTyping() + const command = this.modules.get(args.shift()!.toLowerCase()) - if (command) { - if (command.noPerm && msg.author.id !== config.bot.owner_ID) - return await noPerm(msg) + if (command) { + if (command.noPerm && msg.author.id !== config.bot.owner_ID) + return await noPerm(msg) - await command.execute(msg, args) - } else { - const response = await this.chatBot.getResponse(msg) - await msg.reply(response) - } + await command.execute(msg, args) + } else { + const response = await this.chatBot.getResponse(msg) + await msg.reply(response) } } } @@ -100,5 +96,7 @@ declare module 'discord.js' { interface Client { get chatBot(): ChatBot get modules(): Collection + dokdo: Dokdo + prefix: string } } diff --git a/src/modules/ChatBot.ts b/src/modules/ChatBot.ts index 0376735..a6c2884 100644 --- a/src/modules/ChatBot.ts +++ b/src/modules/ChatBot.ts @@ -9,12 +9,9 @@ export default class ChatBot { return new MaaDatabase() } public async getResponse(msg: Message): Promise { + const prefix = msg.client.prefix const data = await this.db.statement.all() - const args = msg.content - .slice('머핀아 '.length) - .trim() - .split(/ +/g) - .join(' ') + const args = msg.content.slice(prefix.length).trim().split(/ +/g).join(' ') const learnData = await this.db.learn.findOne(args) const randomNumber = Math.round(Math.random() * (2 - 1) + 1) @@ -23,14 +20,14 @@ export default class ChatBot { console.log(args) } - if (randomNumber === 1) { - if (learnData[0]) { - if (args.startsWith(learnData[0].command)) { - return `${learnData[0].result}\n\`${ - (await msg.client.users.fetch(learnData[0].user_id)).username - }님이 알려주셨어요.\`` - } - } + if ( + randomNumber === 1 && + learnData[0] && + args.startsWith(learnData[0].command) + ) { + return `${learnData[0].result}\n\`${ + (await msg.client.users.fetch(learnData[0].user_id)).username + }님이 알려주셨어요.\`` } let response: string @@ -46,27 +43,24 @@ export default class ChatBot { } public async train(client: Client): Promise { + const prefix = client.prefix client.on('messageCreate', async msg => { if (msg.author.bot) return if (msg.author.id === config.train.user_ID) { const response = await this.getResponse(msg) - const data = await this.db.statement.all() await this.db.statement.insert({ - id: ++data[data.length - 1].id, text: msg.content, persona: 'muffin', in_response_to: response, }) } else { if (!(msg.channel as TextChannel).nsfw) return - if (!msg.content.startsWith('머핀아 ')) return - const user = `user:${msg.author.username.slice(0, 50).toLowerCase()}` + if (!msg.content.startsWith(prefix)) return + const persona = `user:${msg.author.username.slice(0, 50).toLowerCase()}` const text = msg.content.replace('머핀아 ', '') - const data = await this.db.nsfwContent.all() await this.db.nsfwContent.insert({ - id: ++data[data.length - 1].id, text, - persona: user, + persona, }) } }) diff --git a/src/modules/database/model/learn.ts b/src/modules/database/model/learn.ts index 2cc4035..c6c27ec 100644 --- a/src/modules/database/model/learn.ts +++ b/src/modules/database/model/learn.ts @@ -60,7 +60,7 @@ export class LearnTable implements BaseTable { } public async findOneAnotherKey( - key: 'command' | 'result' | 'user_id' | 'created_at', + key: 'id' | 'command' | 'result' | 'user_id' | 'created_at', data: any, ): Promise { const [rows] = await this._database.execute( diff --git a/src/modules/database/model/nsfwContent.ts b/src/modules/database/model/nsfwContent.ts index ceff214..af08ffa 100644 --- a/src/modules/database/model/nsfwContent.ts +++ b/src/modules/database/model/nsfwContent.ts @@ -21,17 +21,13 @@ export class NSFWContentTable implements BaseTable { return rows } - public async insert(data: { - id: number - text: string - persona: string - }): Promise { + public async insert(data: { text: string; persona: string }): Promise { const db = await this._database.getConnection() await run(db, async () => { await db.execute( - 'INSERT INTO nsfw_content (id, text, persona) VALUES (?, ?, ?);', - [data.id, data.text, data.persona], + 'INSERT INTO nsfw_content (text, persona) VALUES (?, ?);', + [data.text, data.persona], ) }) } diff --git a/src/modules/database/model/statement.ts b/src/modules/database/model/statement.ts index 9262d23..f2a07bc 100644 --- a/src/modules/database/model/statement.ts +++ b/src/modules/database/model/statement.ts @@ -22,7 +22,6 @@ export class StatementTable implements BaseTable { } public async insert(data: { - id: number text: string persona: string in_response_to: string @@ -31,8 +30,8 @@ export class StatementTable implements BaseTable { await run(db, async () => { await db.execute( - 'INSERT INTO statement (id, text, persona, in_response_to) VALUES (?, ?, ?, ?);', - [data.id, data.text, data.persona, data.in_response_to], + 'INSERT INTO statement (text, persona, in_response_to) VALUES (?, ?, ?);', + [data.text, data.persona, data.in_response_to], ) }) } diff --git a/src/modules/database/type.ts b/src/modules/database/type.ts index 8a1b3e9..2f42527 100644 --- a/src/modules/database/type.ts +++ b/src/modules/database/type.ts @@ -16,6 +16,7 @@ export interface ResponseData extends BaseData { } export interface LearnData extends RowDataPacket { + id: number command: string result: string user_id: Snowflake