feat: nsfw only user train

This commit is contained in:
Siwoo Jeon 2023-02-11 15:03:25 +09:00
parent 72eb76e681
commit 6fc5eae5cd
Signed by: migan
GPG key ID: C4151385FFD2082A
2 changed files with 41 additions and 6 deletions

View file

@ -13,7 +13,10 @@ import 'dotenv/config'
const prefix = '멒힌아 ' const prefix = '멒힌아 '
export default class MuffinAI extends Client { export default class MuffinAI extends Client {
public chatBot = new ChatBot() get chatBot() {
return this.#chatBot
}
#chatBot = new ChatBot()
#modules: Collection<string, Command> = new Collection() #modules: Collection<string, Command> = new Collection()
public constructor() { public constructor() {
super({ super({
@ -46,7 +49,7 @@ export default class MuffinAI extends Client {
if (msg.author.bot) return if (msg.author.bot) return
if (msg.content.startsWith('머핀아 ')) { if (msg.content.startsWith('머핀아 ')) {
if (msg.channel instanceof TextChannel) { if (msg.channel instanceof TextChannel) {
if (msg.channel.nsfw) return // if (msg.channel.nsfw) return
await msg.channel.sendTyping() await msg.channel.sendTyping()
this.chatBot // this.chatBot //
.getResponse(msg) .getResponse(msg)
@ -81,6 +84,6 @@ export default class MuffinAI extends Client {
declare module 'discord.js' { declare module 'discord.js' {
interface Client { interface Client {
chatBot: ChatBot get chatBot(): ChatBot
} }
} }

View file

@ -1,15 +1,28 @@
import type { Client, Message } from 'discord.js' import type { Client, Message } from 'discord.js'
import database, { ResponseData } from './database' import database, { ResponseData } from './database'
import { TextChannel } from 'discord.js'
export default class ChatBot { export default class ChatBot {
public db = database get db() {
return this.#db
}
#db = database
public async getResponse(msg: Message): Promise<string> { public async getResponse(msg: Message): Promise<string> {
const db = await this.db const db = await this.db
const request = msg.content.replace('머핀아 ', '') const request = msg.content.replace('머핀아 ', '')
console.log(`req: ${request}`) console.log(`req: ${request}`)
const [rows] = await db.execute<ResponseData[]>('SELECT * FROM statement;') const [rows] = await db.execute<ResponseData[]>('SELECT * FROM statement;')
let response = rows[Math.floor(Math.random() * rows.length)].text let response: string
if ((msg.channel as TextChannel).nsfw) {
const [rows2] = await db.execute<ResponseData[]>(
'SELECT * FROM nsfw_content;'
)
response = [...rows, ...rows2][Math.floor(Math.random() * rows.length)]
.text
} else {
response = rows[Math.floor(Math.random() * rows.length)].text
if (!response) response = '살ㄹ려주세요' if (!response) response = '살ㄹ려주세요'
}
console.log(`res: ${response}`) console.log(`res: ${response}`)
return response return response
} }
@ -34,6 +47,25 @@ export default class ChatBot {
console.log(err) console.log(err)
await db.rollback() await db.rollback()
} }
} else {
if (!(msg.channel as TextChannel).nsfw) return
if (!msg.content.startsWith('머핀아 ')) return
const user = `user:${msg.author.username.slice(0, 50).toLowerCase()}`
const text = msg.content.replace('머핀아 ', '')
const [rows] = await db.execute<ResponseData[]>(
'SELECT * FROM nsfw_content;'
)
try {
await db.beginTransaction()
await db.execute(
`INSERT INTO nsfw_content (id, text, persona) VALUES (?, ?, ?);`,
[++rows[rows.length - 1].id, text, user]
)
await db.commit()
} catch (err) {
console.log(err)
await db.rollback()
}
} }
}) })
return this return this