This commit is contained in:
Siwoo Jeon 2023-12-02 23:59:31 +09:00
parent da3e32a99f
commit 72ac25f773
Signed by: migan
GPG key ID: C4151385FFD2082A
6 changed files with 101 additions and 28 deletions

View file

@ -22,10 +22,9 @@ CREATE TABLE
CREATE TABLE CREATE TABLE
learn ( learn (
id int(11) NOT NULL,
command varchar(255) NOT NULL, command varchar(255) NOT NULL,
result varchar(255) NOT NULL, result varchar(255) NOT NULL,
user_id varchar(255) NOT NULL, user_id varchar(255) NOT NULL,
created_at datetime NOT NULL DEFAULT current_timestamp(), created_at datetime NOT NULL DEFAULT current_timestamp(),
primary key(`id`) primary key(`command`)
); );

View file

@ -1,13 +1,40 @@
import { Message } from 'discord.js' import { Message } from 'discord.js'
import { Command } from '../modules' import { Command, LearnData } from '../modules'
export default class extends Command { export default class extends Command {
public constructor() { public constructor() {
super('삭제') super('삭제')
} }
public async execute(msg: Message, args: string[]) { public async execute(msg: Message, args: string[]) {
msg.channel.send( if (!args[0]) {
'현재는 사용할 수 없는 기능이예요. 만약 삭제를 원할 경우 개발자에게 문의해주세요.', return await msg.channel.send('```멒힌아 삭제 (지울 단어)```')
}
const db = await msg.client.chatBot.db.getConnection()
const command = args[0]
const [rows] = await db.execute<LearnData[]>(
'SELECT * FROM learn WHERE command = ?;',
[command],
) )
if (!rows[0]) {
return await msg.channel.send('해당하는 걸 찾ㅈ을 수 없어요.')
}
if (rows[0].user_id !== msg.author.id) {
return await msg.channel.send(
'당신ㄴ은 해당 지식을 안가르ㄹ쳐 주셨ㅅ는데요?',
)
}
try {
await db.beginTransaction()
await db.execute('DELETE FROM learn WHERE command = ?;', [command])
await msg.channel.send('해당 단어를 삭ㄱ제했어요.')
await db.commit()
} catch (err) {
console.error(err)
} finally {
db.release()
}
} }
} }

View file

@ -33,6 +33,16 @@ export default class extends Command {
[command], [command],
) )
if (learn[0]) {
if (msg.author.id !== learn[0].user_id) {
return msg.channel.send(
`해ㄷ당 단어는 이미 ${
(await msg.client.users.fetch(learn[0].user_id)).username
} .`,
)
}
}
for (const ig of ignore) { for (const ig of ignore) {
if (command.includes(ig)) { if (command.includes(ig)) {
return msg.channel.send('해ㄷ당 단어는 배울ㄹ 수 없어요.') return msg.channel.send('해ㄷ당 단어는 배울ㄹ 수 없어요.')
@ -46,12 +56,20 @@ export default class extends Command {
} }
try { try {
await db.beginTransaction() if (learn[0] && msg.author.id === learn[0].user_id) {
await db.execute( await db.execute('UPDATE learn SET result = ? WHERE command = ?;', [
'INSERT INTO learn (id, command, result, user_id) VALUES (?, ?, ?, ?);', result,
[++learn[learn.length - 1].id, command, result, msg.author.id], command,
) ])
await msg.channel.send(`${command}을/를 배웠ㅇ어요.`) await msg.channel.send(`${command}을/를 다시 배ㅂ웠어요.`)
} else {
await db.execute(
'INSERT INTO learn (command, result, user_id) VALUES (?, ?, ?);',
[command, result, msg.author.id],
)
await msg.channel.send(`${command}을/를 배웠ㅇ어요.`)
}
await db.commit() await db.commit()
} catch (err) { } catch (err) {
console.error(err) console.error(err)

41
src/Commands/list.ts Normal file
View file

@ -0,0 +1,41 @@
import { Message, codeBlock } from 'discord.js'
import { Command, LearnData } from '../modules'
export default class extends Command {
public constructor() {
super('리스트')
}
public async execute(msg: Message<boolean>, args: string[]) {
const db = await msg.client.chatBot.db.getConnection()
const [rows] = await db.execute<LearnData[]>(
'SELECT * FROM learn WHERE user_id = ?;',
[msg.author.id],
)
const list: string[] = []
if (!rows) {
return await msg.channel.send(
'당신ㄴ은 단어를 가르쳐준 기억이 없ㅅ는데요.',
)
}
for (const data of rows) {
list.push(data.command)
}
await msg.channel.send({
embeds: [
{
title: '지식',
description: codeBlock(
'md',
list.map(item => `- ${item}`).join('\n'),
),
color: 0x0000ff,
timestamp: new Date().toISOString(),
},
],
})
}
}

View file

@ -30,22 +30,11 @@ export default class ChatBot {
if (a === 1) { if (a === 1) {
if (learn[0]) { if (learn[0]) {
if (learn[1]) { if (args.startsWith(learn[0].command)) {
if (args.startsWith(learn[0].command)) { db.release()
const response = return `${learn[0].result}\n\`${
learn[Math.floor(Math.random() * learn.length)].result (await msg.client.users.fetch(learn[0].user_id)).username
db.release() } .\``
return `${response}\n\`${
(await msg.client.users.fetch(learn[0].user_id)).username
} .\``
}
} else {
if (args.startsWith(learn[0].command)) {
db.release()
return `${learn[0].result}\n\`${
(await msg.client.users.fetch(learn[0].user_id)).username
} .\``
}
} }
} }
} }

View file

@ -17,7 +17,6 @@ export interface ResponseData extends BaseData {
} }
export interface LearnData extends RowDataPacket { export interface LearnData extends RowDataPacket {
id: number
command: string command: string
result: string result: string
user_id: Snowflake user_id: Snowflake