This commit is contained in:
Siwoo Jeon 2023-02-06 20:21:58 +09:00
parent 81dc4367aa
commit 011467782d
Signed by: migan
GPG key ID: C4151385FFD2082A
5 changed files with 36 additions and 37 deletions

View file

@ -2,4 +2,5 @@ DISCORD_TOKEN='BOT_TOKEN'
MYSQL_USER='MYSQL_USER' MYSQL_USER='MYSQL_USER'
MYSQL_HOST='MYSQL_HOST' MYSQL_HOST='MYSQL_HOST'
MYSQL_PASSWORD='MYSQL_PASSWORD' MYSQL_PASSWORD='MYSQL_PASSWORD'
MYSQL_DATABASE='MYSQL_DATABASE' MYSQL_DATABASE='MYSQL_DATABASE'
MYSQL_PORT='MYSQL_PORT'

View file

@ -35,8 +35,8 @@ export default class MuffinAI extends Client {
this.#modules.set(b.name, b) this.#modules.set(b.name, b)
}) })
this.once('ready', client => { this.once('ready', () => {
client.user!.setActivity({ this.user!.setActivity({
type: ActivityType.Playing, type: ActivityType.Playing,
name: 'ㅅ살려주세요..!', name: 'ㅅ살려주세요..!',
}) })

View file

@ -7,16 +7,13 @@ export default class extends Command {
} }
public async execute(msg: Message, args: string[]) { public async execute(msg: Message, args: string[]) {
const conn = await database.getConnection() const conn = await database.getConnection()
await conn const [rows] = await conn.query('SELECT * FROM statement;')
.query('SELECT * FROM statement;') // const muffin: ResponseData[] = []
.then(rows => { ;(rows as ResponseData[]).forEach(row => {
const muffin: ResponseData[] = [] if (row.persona === 'muffin') muffin.push(row)
;(rows[0] as ResponseData[]).forEach(row => { else return
if (row.persona === 'muffin') muffin.push(row) })
else return msg.channel.send(`머핀 데이터: ${muffin.length}`)
})
msg.channel.send(`머핀 데이터: ${muffin.length}`)
})
conn.release() conn.release()
} }
} }

View file

@ -5,39 +5,39 @@ export default class ChatBot {
public async getResponse(msg: Message): Promise<string> { public async getResponse(msg: Message): Promise<string> {
const conn = await database.getConnection() const conn = await database.getConnection()
const request = msg.content.replace('머핀아 ', '') const request = msg.content.replace('머핀아 ', '')
const result = await conn.query('SELECT * FROM statement;')
const rows = result[0] as ResponseData[]
let response = rows[Math.floor(Math.random() * rows.length)].text
if (!response) response = '살ㄹ려주세요'
console.log(`⌨️ㅣ${request}`) console.log(`⌨️ㅣ${request}`)
const [rows] = await conn.query('SELECT * FROM statement;')
let response = (rows as ResponseData[])[
Math.floor(Math.random() * (rows as ResponseData[]).length)
].text
if (!response) response = '살ㄹ려주세요'
console.log(`🍰ㅣ${response}`) console.log(`🍰ㅣ${response}`)
conn.release() conn.release()
return response return response
} }
public async train(client: Client): Promise<ChatBot> { public async train(client: Client): Promise<ChatBot> {
const conn = await database.getConnection() client.on('messageCreate', async msg => {
client.on('messageCreate', msg => { const conn = await database.getConnection()
if (msg.author.bot) return if (msg.author.bot) return
if (msg.author.id !== '1026185545837191238') return if (msg.author.id !== '1026185545837191238') return
this.getResponse(msg) // const response = this.getResponse(msg)
.then(async response => { const result = await conn.query('SELECT * FROM statement;')
const result = await conn.query('SELECT * FROM statement;') const rows = result[0] as ResponseData[]
const rows = result[0] as ResponseData[] await conn.beginTransaction()
try { try {
await conn.beginTransaction() await conn.execute(
await conn.execute( `INSERT INTO statement(id, text, persona, in_response_to) VALUES(?, ?, ?, ?);`,
`INSERT INTO statement(id, text, persona, in_response_to) VALUES(?, ?, ?, ?);`, [++rows[rows.length - 1].id, msg.content, 'muffin', response]
[++rows[rows.length - 1].id, msg.content, 'muffin', response] )
) await conn.commit()
await conn.commit() } catch (err) {
} catch (err) { console.log(err)
console.log(err) await conn.rollback()
await conn.rollback() } finally {
} finally { conn.release()
conn.release() }
}
})
}) })
return this return this
} }

View file

@ -17,4 +17,5 @@ export default createPool({
user: process.env.MYSQL_USER, user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD, password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE, database: process.env.MYSQL_DATABASE,
port: (process.env.MYSQL_PORT as unknown as number) || 3306,
}) })