feat: rewrite deleteLearn.ts

This commit is contained in:
Siwoo Jeon 2024-06-18 13:10:42 +09:00
parent 54688e1db6
commit 38810a3f1b
Signed by: migan
GPG key ID: C4151385FFD2082A
7 changed files with 63 additions and 10 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "muffinbot", "name": "muffinbot",
"version": "2.1.0-oreo.b240611a", "version": "2.1.0-oreo.b240618a",
"main": "dist/index.js", "main": "dist/index.js",
"private": true, "private": true,
"dependencies": { "dependencies": {

View file

@ -1,5 +1,5 @@
import { Message } from 'discord.js' import { Message } from 'discord.js'
import { Command } from '../modules' import { Command, type LearnData } from '../modules'
export default class extends Command { export default class extends Command {
public constructor() { public constructor() {
@ -9,21 +9,43 @@ export default class extends Command {
if (!args[0]) { if (!args[0]) {
return await msg.channel.send('```머핀아 삭제 (지울 단어)```') return await msg.channel.send('```머핀아 삭제 (지울 단어)```')
} }
const command = args[0] const command = args[0]
const db = msg.client.chatBot.db const db = msg.client.chatBot.db
const data = await db.learn.findOne(command) const [data] = await db.learn.execute<LearnData[]>(
'SELECT * FROM learn WHERE command = ? AND user_id = ?;',
[command, msg.author.id],
)
console.log(data)
if (!data[0]) { if (!data[0]) {
return await msg.channel.send('해당하는 걸 찾ㅈ을 수 없어요.') return await msg.channel.send('해당하는 걸 찾ㅈ을 수 없어요.')
} }
if (data[0].user_id !== msg.author.id) { console.log(data.length)
return await msg.channel.send(
'어라 당ㅅ신은 언제 가르쳐 주셨죠?', if (data.length > 1) {
) console.log('a')
} else {
// await db.learn.delete(command)
// await msg.reply('어라 이제 그ㄱ게 기억이 안나요. 뭐ㅇ였죠?')
console.log('b')
} }
await db.learn.delete(command) await msg.reply('현재 이 기능은 재작성 중입니다.')
await msg.reply('어라 이제 그ㄱ게 기억이 안나요.')
// if (!data[0]) {
// return await msg.channel.send('해당하는 걸 찾ㅈ을 수 없어요.')
// }
//
// if (data[0].user_id !== msg.author.id) {
// return await msg.channel.send(
// '어라 당ㅅ신은 언제 가르쳐 주셨죠?',
// )
// }
//
// await db.learn.delete(command)
// await msg.reply('어라 이제 그ㄱ게 기억이 안나요.')
} }
} }

View file

@ -69,4 +69,14 @@ export class LearnTable implements BaseTable<LearnData, string> {
) )
return rows return rows
} }
public async execute<W>(sql: string, values?: any): Promise<W> {
const db = await this._database.getConnection()
let data: any
await run(db, async () => {
data = await db.execute(sql, [...values])
})
return data
}
} }

View file

@ -61,4 +61,14 @@ export class NSFWContentTable implements BaseTable<NSFWData, number> {
) )
return rows return rows
} }
public async execute<W>(sql: string, values?: any): Promise<W> {
const db = await this._database.getConnection()
let data: any
await run(db, async () => {
data = await db.execute(sql, [...values])
})
return data
}
} }

View file

@ -73,4 +73,14 @@ export class StatementTable implements BaseTable<ResponseData, number> {
) )
return rows return rows
} }
public async execute<W>(sql: string, values?: any): Promise<W> {
const db = await this._database.getConnection()
let data: any
await run(db, async () => {
data = await db.execute(sql, [...values])
})
return data
}
} }

View file

@ -1,6 +1,6 @@
import { type PoolConnection } from 'mysql2/promise' import { type PoolConnection } from 'mysql2/promise'
export default async function run(db: PoolConnection, fn: () => Promise<void>) { export default async function run(db: PoolConnection, fn: () => Promise<any>) {
try { try {
await db.beginTransaction() await db.beginTransaction()
await fn() await fn()

View file

@ -33,4 +33,5 @@ export interface BaseTable<T, V> {
update(data: any): Promise<void> update(data: any): Promise<void>
delete(key: V): Promise<void> delete(key: V): Promise<void>
findOneAnotherKey(key: string, data: any): Promise<T[]> findOneAnotherKey(key: string, data: any): Promise<T[]>
execute<W>(sql: string, values?: any): Promise<W>
} }