feat: 머핀이 한말 DB에 저장
This commit is contained in:
parent
7280ac7f35
commit
f7b666c5b3
2 changed files with 25 additions and 7 deletions
|
@ -1,6 +1,6 @@
|
||||||
import sqlite3 from 'sqlite3'
|
import sqlite3 from 'sqlite3'
|
||||||
import { ResponseData } from './types'
|
import { ResponseData } from './types'
|
||||||
import { Message } from 'discord.js'
|
import type { Client, Message } from 'discord.js'
|
||||||
|
|
||||||
function arrayShuffle<T>(array: T[]): T[] {
|
function arrayShuffle<T>(array: T[]): T[] {
|
||||||
array = [...array]
|
array = [...array]
|
||||||
|
@ -21,19 +21,36 @@ export default class ChatBot {
|
||||||
this.db = new sqlite3.Database(dbPath)
|
this.db = new sqlite3.Database(dbPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
public getResponse(msg: Message) {
|
public getResponse(msg: Message, sendMsg?: boolean): ChatBot {
|
||||||
this.db.all('select * from statement', [], (err, rows: ResponseData[]) => {
|
this.db.all('SELECT * FROM statement;', [], (err, rows: ResponseData[]) => {
|
||||||
if (err) throw err
|
if (err) throw err
|
||||||
const a = msg.content.replace('머핀아', '')
|
const a = msg.content.replace('머핀아', '')
|
||||||
const data = arrayShuffle([...rows])
|
const data = arrayShuffle([...rows])
|
||||||
console.log(data)
|
|
||||||
let r = data[0].text
|
let r = data[0].text
|
||||||
if (!r) r = '살ㄹ려주세요'
|
if (!r) r = '살ㄹ려주세요'
|
||||||
console.log(`⌨️ㅣ${a}`)
|
console.log(`⌨️ㅣ${a}`)
|
||||||
console.log(`🍰ㅣ${r}`)
|
console.log(`🍰ㅣ${r}`)
|
||||||
|
if (sendMsg) {
|
||||||
msg.channel.sendTyping()
|
msg.channel.sendTyping()
|
||||||
setTimeout(() => msg.channel.send(r), 1000)
|
setTimeout(() => msg.channel.send(r), 1000)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
return this
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ export default class MuffinAI extends Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
public override login(): Promise<string> {
|
public override login(): Promise<string> {
|
||||||
|
this.chatBot.train(this)
|
||||||
this.once('ready', client => {
|
this.once('ready', client => {
|
||||||
client.user!.setActivity({
|
client.user!.setActivity({
|
||||||
type: ActivityType.Playing,
|
type: ActivityType.Playing,
|
||||||
|
@ -26,7 +27,7 @@ export default class MuffinAI extends Client {
|
||||||
if (msg.author.bot) return
|
if (msg.author.bot) return
|
||||||
if (!msg.content.startsWith('머핀아 ')) return
|
if (!msg.content.startsWith('머핀아 ')) return
|
||||||
|
|
||||||
this.chatBot.getResponse(msg)
|
this.chatBot.getResponse(msg, true)
|
||||||
})
|
})
|
||||||
return super.login()
|
return super.login()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue