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_HOST='MYSQL_HOST'
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.once('ready', client => {
client.user!.setActivity({
this.once('ready', () => {
this.user!.setActivity({
type: ActivityType.Playing,
name: 'ㅅ살려주세요..!',
})

View file

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

View file

@ -5,39 +5,39 @@ export default class ChatBot {
public async getResponse(msg: Message): Promise<string> {
const conn = await database.getConnection()
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}`)
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}`)
conn.release()
return response
}
public async train(client: Client): Promise<ChatBot> {
const conn = await database.getConnection()
client.on('messageCreate', msg => {
client.on('messageCreate', async msg => {
const conn = await database.getConnection()
if (msg.author.bot) return
if (msg.author.id !== '1026185545837191238') return
this.getResponse(msg) //
.then(async response => {
const result = await conn.query('SELECT * FROM statement;')
const rows = result[0] as ResponseData[]
try {
await conn.beginTransaction()
await conn.execute(
`INSERT INTO statement(id, text, persona, in_response_to) VALUES(?, ?, ?, ?);`,
[++rows[rows.length - 1].id, msg.content, 'muffin', response]
)
await conn.commit()
} catch (err) {
console.log(err)
await conn.rollback()
} finally {
conn.release()
}
})
const response = this.getResponse(msg)
const result = await conn.query('SELECT * FROM statement;')
const rows = result[0] as ResponseData[]
await conn.beginTransaction()
try {
await conn.execute(
`INSERT INTO statement(id, text, persona, in_response_to) VALUES(?, ?, ?, ?);`,
[++rows[rows.length - 1].id, msg.content, 'muffin', response]
)
await conn.commit()
} catch (err) {
console.log(err)
await conn.rollback()
} finally {
conn.release()
}
})
return this
}

View file

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