rollback
This commit is contained in:
parent
da3e32a99f
commit
72ac25f773
6 changed files with 101 additions and 28 deletions
|
@ -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`)
|
||||||
);
|
);
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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('UPDATE learn SET result = ? WHERE command = ?;', [
|
||||||
|
result,
|
||||||
|
command,
|
||||||
|
])
|
||||||
|
await msg.channel.send(`${command}을/를 다시 배ㅂ웠어요.`)
|
||||||
|
} else {
|
||||||
await db.execute(
|
await db.execute(
|
||||||
'INSERT INTO learn (id, command, result, user_id) VALUES (?, ?, ?, ?);',
|
'INSERT INTO learn (command, result, user_id) VALUES (?, ?, ?);',
|
||||||
[++learn[learn.length - 1].id, command, result, msg.author.id],
|
[command, result, msg.author.id],
|
||||||
)
|
)
|
||||||
await msg.channel.send(`${command}을/를 배웠ㅇ어요.`)
|
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
41
src/Commands/list.ts
Normal 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(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -30,16 +30,6 @@ 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)) {
|
|
||||||
const response =
|
|
||||||
learn[Math.floor(Math.random() * learn.length)].result
|
|
||||||
db.release()
|
|
||||||
return `${response}\n\`${
|
|
||||||
(await msg.client.users.fetch(learn[0].user_id)).username
|
|
||||||
}님이 알려주셨어요.\``
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (args.startsWith(learn[0].command)) {
|
if (args.startsWith(learn[0].command)) {
|
||||||
db.release()
|
db.release()
|
||||||
return `${learn[0].result}\n\`${
|
return `${learn[0].result}\n\`${
|
||||||
|
@ -48,7 +38,6 @@ export default class ChatBot {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let response: string
|
let response: string
|
||||||
if ((msg.channel as TextChannel).nsfw) {
|
if ((msg.channel as TextChannel).nsfw) {
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue