fix: remove hard coding part

This commit is contained in:
Siwoo Jeon 2024-08-14 22:44:39 +09:00
parent 6e21ffe0f2
commit 49152bcbd8
Signed by: migan
GPG key ID: 036E9A8C5E8E48DA
4 changed files with 25 additions and 26 deletions

View file

@ -1,6 +1,6 @@
{
"name": "muffinbot",
"version": "3.0.0-cake.p240803b",
"version": "3.0.0-cake.p240814a",
"main": "dist/index.js",
"private": true,
"dependencies": {

View file

@ -4,19 +4,19 @@ import { Snowflake } from 'discord.js'
import run from '../run'
export class LearnTable implements BaseTable<LearnData, string> {
public name = 'learn'
public readonly name = 'learn'
public constructor(private _database: Pool) {}
public async all(): Promise<LearnData[]> {
const [rows] = await this._database.execute<LearnData[]>(
'SELECT * FROM learn;',
`SELECT * FROM ${this.name};`,
)
return rows
}
public async findOne(key: string): Promise<LearnData[]> {
const [rows] = await this._database.execute<LearnData[]>(
'SELECT * FROM learn WHERE command = ?;',
`SELECT * FROM ${this.name} WHERE command = ?;`,
[key],
)
return rows
@ -31,7 +31,7 @@ export class LearnTable implements BaseTable<LearnData, string> {
await run(db, async () => {
await db.execute(
'INSERT INTO learn (command, result, user_id) VALUES (?, ?, ?);',
`INSERT INTO ${this.name} (command, result, user_id) VALUES (?, ?, ?);`,
[data.command, data.result, data.user_id],
)
})
@ -44,10 +44,10 @@ export class LearnTable implements BaseTable<LearnData, string> {
const db = await this._database.getConnection()
await run(db, async () => {
await db.execute('UPDATE learn SET result = ? WHERE command = ?;', [
data.command,
data.result,
])
await db.execute(
`UPDATE ${this.name} SET result = ? WHERE command = ?;`,
[data.command, data.result],
)
})
}
@ -55,7 +55,7 @@ export class LearnTable implements BaseTable<LearnData, string> {
const db = await this._database.getConnection()
await run(db, async () => {
await db.execute('DELETE FROM learn WHERE id = ?;', [key])
await db.execute(`DELETE FROM ${this.name} WHERE id = ?;`, [key])
})
}
@ -64,7 +64,7 @@ export class LearnTable implements BaseTable<LearnData, string> {
data: any,
): Promise<LearnData[]> {
const [rows] = await this._database.execute<LearnData[]>(
`SELECT * FROM learn WHERE ${key} = ?;`,
`SELECT * FROM ${this.name} WHERE ${key} = ?;`,
[data],
)
return rows

View file

@ -3,20 +3,19 @@ import { type Pool } from 'mysql2/promise'
import run from '../run'
export class NSFWContentTable implements BaseTable<NSFWData, number> {
public name = 'nsfw_content'
public readonly name = 'nsfw_content'
public constructor(private _database: Pool) {}
public async all(): Promise<NSFWData[]> {
const [rows] = await this._database.execute<NSFWData[]>(
'SELECT * FROM nsfw_content;',
`SELECT * FROM ${this.name};`,
)
return rows
}
public async findOne(key: number): Promise<NSFWData[]> {
const [rows] = await this._database.execute<NSFWData[]>(
'SELECT * FROM nsfw_content WHERE id = ?;',
`SELECT * FROM ${this.name} WHERE id = ?;`,
[key],
)
return rows
@ -27,7 +26,7 @@ export class NSFWContentTable implements BaseTable<NSFWData, number> {
await run(db, async () => {
await db.execute(
'INSERT INTO nsfw_content (text, persona) VALUES (?, ?);',
`INSERT INTO ${this.name} (text, persona) VALUES (?, ?);`,
[data.text, data.persona],
)
})
@ -37,7 +36,7 @@ export class NSFWContentTable implements BaseTable<NSFWData, number> {
const db = await this._database.getConnection()
await run(db, async () => {
await db.execute('UPDATE nsfw_content SET text = ? WHERE id = ?;', [
await db.execute(`UPDATE ${this.name} SET text = ? WHERE id = ?;`, [
data.text,
data.id,
])
@ -48,7 +47,7 @@ export class NSFWContentTable implements BaseTable<NSFWData, number> {
const db = await this._database.getConnection()
await run(db, async () => {
await db.execute('DELETE FROM nsfw_content WHERE id = ?;', [key])
await db.execute(`DELETE FROM ${this} WHERE id = ?;`, [key])
})
}
@ -58,7 +57,7 @@ export class NSFWContentTable implements BaseTable<NSFWData, number> {
): Promise<NSFWData[]> {
const [rows] = await this._database.execute<NSFWData[]>(
`SELECT *
FROM nsfw_content
FROM ${this.name}
WHERE ${key} = ?;`,
[data],
)

View file

@ -3,19 +3,19 @@ import { type Pool } from 'mysql2/promise'
import run from '../run'
export class StatementTable implements BaseTable<ResponseData, number> {
public name = 'statement'
public readonly name = 'statement'
public constructor(private _database: Pool) {}
public async all(): Promise<ResponseData[]> {
const [rows] = await this._database.execute<ResponseData[]>(
'SELECT * FROM statement;',
`SELECT * FROM ${this.name};`,
)
return rows
}
public async findOne(key: number): Promise<ResponseData[]> {
const [rows] = await this._database.execute<ResponseData[]>(
'SELECT * FROM statement WHERE id = ?;',
`SELECT * FROM ${this.name} WHERE id = ?;`,
[key],
)
return rows
@ -30,7 +30,7 @@ export class StatementTable implements BaseTable<ResponseData, number> {
await run(db, async () => {
await db.execute(
'INSERT INTO statement (text, persona, in_response_to) VALUES (?, ?, ?);',
`INSERT INTO ${this.name} (text, persona, in_response_to) VALUES (?, ?, ?);`,
[data.text, data.persona, data.in_response_to],
)
})
@ -40,7 +40,7 @@ export class StatementTable implements BaseTable<ResponseData, number> {
const db = await this._database.getConnection()
await run(db, async () => {
await db.execute('UPDATE statement SET text = ? WHERE id = ?;', [
await db.execute(`UPDATE ${this.name} SET text = ? WHERE id = ?;`, [
data.text,
data.id,
])
@ -51,7 +51,7 @@ export class StatementTable implements BaseTable<ResponseData, number> {
const db = await this._database.getConnection()
await run(db, async () => {
await db.execute('DELETE FROM statement WHERE id = ?;', [key])
await db.execute(`DELETE FROM ${this.name} WHERE id = ?;`, [key])
})
}
@ -68,7 +68,7 @@ export class StatementTable implements BaseTable<ResponseData, number> {
data: any,
): Promise<ResponseData[]> {
const [rows] = await this._database.execute<ResponseData[]>(
`SELECT * FROM statement WHERE ${key} = ?;`,
`SELECT * FROM ${this.name} WHERE ${key} = ?;`,
[data],
)
return rows