feat: add learn command

This commit is contained in:
Siwoo Jeon 2023-11-24 11:13:16 +09:00
parent 2665ccce7a
commit aa53e98130
Signed by: migan
GPG key ID: C4151385FFD2082A
2 changed files with 61 additions and 0 deletions

View file

@ -19,3 +19,12 @@ CREATE TABLE
`persona` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
);
CREATE TABLE
learn (
command varchar(255) NOT NULL,
result varchar(255) NOT NULL,
user_id varchar(255) NOT NULL,
created_at datetime NOT NULL current_timestamp(),
primary key(`command`)
);

52
src/Commands/learn.ts Normal file
View file

@ -0,0 +1,52 @@
import { Command } from '../modules'
import { Message } from 'discord.js'
export default class extends Command {
public constructor() {
super('배워')
}
public async execute(msg: Message, args: string[]) {
const command = args[0]
const result = args[1].replaceAll('_', ' ')
const ignore = [
'학습데이터량',
'봇꺼',
'테스트',
'미간',
'Migan',
'migan',
'간미',
]
const disallowed = ['@everyone', '@here']
const db = await msg.client.chatBot.db.getConnection()
ignore.forEach(ig => {
if (result.includes(ig)) {
return msg.channel.send('해ㄷ당 단어는 배울ㄹ 수 없어요.')
}
})
disallowed.forEach(di => {
if (result.includes(di)) {
return msg.channel.send('해당 단ㅇ어는 금지되어 있ㅅ어요.')
}
})
try {
await db.beginTransaction()
db.execute(
'INSERT INTO learn (command, result, user_id) VALUES (?, ?, ?);',
[command, result, msg.author.id],
)
await msg.channel.send(`${command}을/를 배웠ㅇ어요.`)
await db.commit()
} catch (err) {
console.error(err)
await db.rollback()
await msg.channel.send('배우는데 오류ㄹ가 생겨서 배우지 못했어ㅇ요.')
} finally {
db.release()
}
}
}