feat: 머핀이 한말 DB에 저장

This commit is contained in:
Siwoo Jeon 2023-01-24 15:59:07 +09:00
parent 7280ac7f35
commit f7b666c5b3
Signed by: migan
GPG key ID: C4151385FFD2082A
2 changed files with 25 additions and 7 deletions

View file

@ -1,6 +1,6 @@
import sqlite3 from 'sqlite3'
import { ResponseData } from './types'
import { Message } from 'discord.js'
import type { Client, Message } from 'discord.js'
function arrayShuffle<T>(array: T[]): T[] {
array = [...array]
@ -21,19 +21,36 @@ export default class ChatBot {
this.db = new sqlite3.Database(dbPath)
}
public getResponse(msg: Message) {
this.db.all('select * from statement', [], (err, rows: ResponseData[]) => {
public getResponse(msg: Message, sendMsg?: boolean): ChatBot {
this.db.all('SELECT * FROM statement;', [], (err, rows: ResponseData[]) => {
if (err) throw err
const a = msg.content.replace('머핀아', '')
const data = arrayShuffle([...rows])
console.log(data)
let r = data[0].text
if (!r) r = '살ㄹ려주세요'
console.log(`⌨️ㅣ${a}`)
console.log(`🍰ㅣ${r}`)
msg.channel.sendTyping()
setTimeout(() => msg.channel.send(r), 1000)
if (sendMsg) {
msg.channel.sendTyping()
setTimeout(() => msg.channel.send(r), 1000)
}
})
return this
}
public train(client: Client): ChatBot {
client.on('messageCreate', msg => {
if (msg.author.id === '1026185545837191238') {
this.db.run(
`INSERT INTO statement(text) VALUES('${msg.content}');`,
err => {
if (err) throw err
this.getResponse(msg)
}
)
}
})
return this
}
}

View file

@ -16,6 +16,7 @@ export default class MuffinAI extends Client {
}
public override login(): Promise<string> {
this.chatBot.train(this)
this.once('ready', client => {
client.user!.setActivity({
type: ActivityType.Playing,
@ -26,7 +27,7 @@ export default class MuffinAI extends Client {
if (msg.author.bot) return
if (!msg.content.startsWith('머핀아 ')) return
this.chatBot.getResponse(msg)
this.chatBot.getResponse(msg, true)
})
return super.login()
}