feat: turn on/off user train

This commit is contained in:
Siwoo Jeon 2023-01-25 20:23:01 +09:00
parent 91537e48eb
commit 26f5997687
Signed by: migan
GPG key ID: C4151385FFD2082A
2 changed files with 45 additions and 17 deletions

View file

@ -1,11 +1,16 @@
import sqlite3 from 'sqlite3' import sqlite3 from 'sqlite3'
import type { Client, Message } from 'discord.js' import type { Client, Message } from 'discord.js'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
type TrainType = 'muffinOnly' | 'All'
export default class ChatBot { export default class ChatBot {
private db: sqlite3.Database private db = new sqlite3.Database(
public constructor(dbPath: string) { join(dirname(fileURLToPath(import.meta.url)), '..', 'db', 'db.sqlite3')
this.db = new sqlite3.Database(dbPath) )
} private trainType: TrainType = 'All'
public constructor() {}
public getResponse(msg: Message, sendMsg?: boolean): ChatBot { public getResponse(msg: Message, sendMsg?: boolean): ChatBot {
this.db.all( this.db.all(
@ -26,7 +31,7 @@ export default class ChatBot {
return this return this
} }
public train(client: Client, user?: boolean): ChatBot { public train(client: Client): ChatBot {
client.on('messageCreate', msg => { client.on('messageCreate', msg => {
if (msg.author.bot) return if (msg.author.bot) return
if (msg.author.id === '1026185545837191238') { if (msg.author.id === '1026185545837191238') {
@ -38,7 +43,7 @@ export default class ChatBot {
} }
) )
} else { } else {
if (!user) return if (this.trainType !== 'All') return
if (!msg.content.startsWith('머핀아 ')) return if (!msg.content.startsWith('머핀아 ')) return
const sql = `INSERT INTO statement(text, persona) VALUES('${msg.content const sql = `INSERT INTO statement(text, persona) VALUES('${msg.content
.replace('머핀아 ', '') .replace('머핀아 ', '')
@ -51,7 +56,19 @@ export default class ChatBot {
return this return this
} }
public destroy() { public changeTrainType(): TrainType {
switch (this.trainType) {
case 'muffinOnly':
this.trainType = 'All'
break
case 'All':
this.trainType = 'muffinOnly'
break
}
return this.trainType
}
public destroy(): void {
this.db.close() this.db.close()
} }
} }

View file

@ -1,7 +1,5 @@
import { ActivityType, Client, GatewayIntentBits, Message } from 'discord.js' import { ActivityType, Client, GatewayIntentBits, Message } from 'discord.js'
import ChatBot from './ChatBot.js' import ChatBot from './ChatBot.js'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import Dokdo from 'dokdo' import Dokdo from 'dokdo'
import 'dotenv/config' import 'dotenv/config'
@ -17,10 +15,15 @@ function noPerm(msg: Message) {
}) })
} }
function isNotOwner(msg: Message): boolean {
if (msg.author.id !== '415135882006495242') {
noPerm(msg)
return false
} else return true
}
export default class MuffinAI extends Client { export default class MuffinAI extends Client {
private chatBot = new ChatBot( private chatBot = new ChatBot()
join(dirname(fileURLToPath(import.meta.url)), '..', 'db', 'db.sqlite3')
)
public constructor() { public constructor() {
super({ super({
intents: [ intents: [
@ -32,7 +35,7 @@ export default class MuffinAI extends Client {
} }
public override login(): Promise<string> { public override login(): Promise<string> {
this.chatBot.train(this, true) this.chatBot.train(this)
this.once('ready', client => { this.once('ready', client => {
client.user!.setActivity({ client.user!.setActivity({
type: ActivityType.Playing, type: ActivityType.Playing,
@ -49,11 +52,19 @@ export default class MuffinAI extends Client {
}).run(msg) }).run(msg)
if (msg.content.startsWith('머핀아 ')) this.chatBot.getResponse(msg, true) if (msg.content.startsWith('머핀아 ')) this.chatBot.getResponse(msg, true)
else if (msg.content.startsWith('멒힌아 봇꺼')) { else if (msg.content.startsWith('멒힌아 봇꺼')) {
if (msg.author.id !== '415135882006495242') { if (!isNotOwner(msg)) return
noPerm(msg)
return
}
this.destroy() this.destroy()
} else if (msg.content.startsWith('멒힌아 모드변경')) {
if (!isNotOwner(msg)) return
const a = this.chatBot.changeTrainType()
switch (a) {
case 'muffinOnly':
msg.channel.send('현재 모드: 머핀만 학습')
break
case 'All':
msg.channel.send('현재 모드: 전체 학습')
break
}
} else return } else return
}) })
return super.login() return super.login()