diff --git a/.pnp.cjs b/.pnp.cjs index e777fb6..90b2b20 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -27,7 +27,7 @@ const RAW_RUNTIME_STATE = "packageLocation": "./",\ "packageDependencies": [\ ["@migan/prettier-config", "virtual:5f78f1a36e0466de94f43ea735dd5701f3e6c031837169942eed8df0d4675e735ccc3da5b972171546318c35e60c2b4bc775227176dd2024d72fef38e36a9bf1#npm:1.0.0"],\ - ["@types/node", "npm:20.9.4"],\ + ["@types/node", "npm:20.10.0"],\ ["cross-env", "npm:7.0.3"],\ ["discord.js", "npm:14.14.1"],\ ["dotenv", "npm:16.3.1"],\ @@ -682,20 +682,20 @@ const RAW_RUNTIME_STATE = }]\ ]],\ ["@types/node", [\ + ["npm:20.10.0", {\ + "packageLocation": "./.yarn/cache/@types-node-npm-20.10.0-22577c30ff-c7d5ddbdbf.zip/node_modules/@types/node/",\ + "packageDependencies": [\ + ["@types/node", "npm:20.10.0"],\ + ["undici-types", "npm:5.26.5"]\ + ],\ + "linkType": "HARD"\ + }],\ ["npm:20.6.3", {\ "packageLocation": "./.yarn/cache/@types-node-npm-20.6.3-7542470c42-2ccd82424c.zip/node_modules/@types/node/",\ "packageDependencies": [\ ["@types/node", "npm:20.6.3"]\ ],\ "linkType": "HARD"\ - }],\ - ["npm:20.9.4", {\ - "packageLocation": "./.yarn/cache/@types-node-npm-20.9.4-a4808c97df-d567855b48.zip/node_modules/@types/node/",\ - "packageDependencies": [\ - ["@types/node", "npm:20.9.4"],\ - ["undici-types", "npm:5.26.5"]\ - ],\ - "linkType": "HARD"\ }]\ ]],\ ["@types/ws", [\ @@ -2048,7 +2048,7 @@ const RAW_RUNTIME_STATE = "packageDependencies": [\ ["muffin-ai-arujak", "workspace:."],\ ["@migan/prettier-config", "virtual:5f78f1a36e0466de94f43ea735dd5701f3e6c031837169942eed8df0d4675e735ccc3da5b972171546318c35e60c2b4bc775227176dd2024d72fef38e36a9bf1#npm:1.0.0"],\ - ["@types/node", "npm:20.9.4"],\ + ["@types/node", "npm:20.10.0"],\ ["cross-env", "npm:7.0.3"],\ ["discord.js", "npm:14.14.1"],\ ["dotenv", "npm:16.3.1"],\ @@ -2750,7 +2750,7 @@ const RAW_RUNTIME_STATE = ["@tsconfig/node12", "npm:1.0.11"],\ ["@tsconfig/node14", "npm:1.0.3"],\ ["@tsconfig/node16", "npm:1.0.4"],\ - ["@types/node", "npm:20.9.4"],\ + ["@types/node", "npm:20.10.0"],\ ["@types/swc__core", null],\ ["@types/swc__wasm", null],\ ["@types/typescript", null],\ diff --git a/.yarn/cache/@types-node-npm-20.9.4-a4808c97df-d567855b48.zip b/.yarn/cache/@types-node-npm-20.10.0-22577c30ff-c7d5ddbdbf.zip similarity index 79% rename from .yarn/cache/@types-node-npm-20.9.4-a4808c97df-d567855b48.zip rename to .yarn/cache/@types-node-npm-20.10.0-22577c30ff-c7d5ddbdbf.zip index 4c6757e..f554ca5 100644 Binary files a/.yarn/cache/@types-node-npm-20.9.4-a4808c97df-d567855b48.zip and b/.yarn/cache/@types-node-npm-20.10.0-22577c30ff-c7d5ddbdbf.zip differ diff --git a/package.json b/package.json index b008af3..441fcf3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "muffin-ai-arujak", - "version": "1.0.0", + "version": "2.0.0-72-oreo", "main": "dist/index.js", "private": true, "dependencies": { @@ -10,7 +10,7 @@ }, "devDependencies": { "@migan/prettier-config": "^1.0.0", - "@types/node": "^20.9.4", + "@types/node": "^20.10.0", "cross-env": "^7.0.3", "prettier": "^3.1.0", "ts-node": "^10.9.1", diff --git a/src/modules/database.ts b/src/modules/database.ts index da19b7a..01f81e7 100644 --- a/src/modules/database.ts +++ b/src/modules/database.ts @@ -1,4 +1,9 @@ -import { type RowDataPacket, createPool } from 'mysql2/promise' +import { + type RowDataPacket, + createPool, + Pool, + PoolConnection, +} from 'mysql2/promise' import config from '../../config.json' import { type Snowflake } from 'discord.js' @@ -25,6 +30,89 @@ export interface LearnData extends RowDataPacket { export { BaseData as NSFWData } +interface BassTable { + name: string + all(): Promise + findOne(key: V): Promise + insert(data: T): Promise + update(data: T): Promise + delete(key: V): Promise +} + +async function run(db: PoolConnection, fn: () => Promise) { + try { + await db.beginTransaction() + await fn() + await db.commit() + } catch (err) { + console.error(err) + await db.rollback() + } finally { + db.release() + } +} + +class StatementTable implements BassTable { + public name = 'statement' + public constructor(private _database: Pool) {} + + public async all(): Promise { + const [rows] = await this._database.execute( + 'SELECT * FROM statement;', + ) + return rows + } + + public async findOne(key: number): Promise { + const [rows] = await this._database.execute( + 'SELECT * FROM statement WHERE id = ?;', + [key], + ) + return rows + } + + public async insert(data: ResponseData): Promise { + const db = await this._database.getConnection() + await run(db, async () => { + await db.execute( + 'INSERT INTO statement (id, text, persona, in_response_to) VALUES (?, ?, ?, ?);', + [data.id, data.text, data.persona, data.in_response_to], + ) + }) + } + + public async update(data: { id: number; text: string }): Promise { + const db = await this._database.getConnection() + + await run(db, async () => { + await db.execute('UPDATE statement SET text = ? WHERE id = ?;', [ + data.text, + data.id, + ]) + }) + } + + public async delete(key: number): Promise { + const db = await this._database.getConnection() + + await run(db, async () => { + await db.execute('DELETE FROM statement WHERE id = ?;', [key]) + }) + } +} + +export class MaaDatabase { + private _database = createPool({ + ...config.mysql, + keepAliveInitialDelay: 10000, + enableKeepAlive: true, + }) + + public get statement() { + return new StatementTable(this._database) + } +} + const database = createPool({ ...config.mysql, keepAliveInitialDelay: 10000, diff --git a/src/modules/index.ts b/src/modules/index.ts index 0d8af77..2a595fe 100644 --- a/src/modules/index.ts +++ b/src/modules/index.ts @@ -1,6 +1,21 @@ import ChatBot from './ChatBot' import Command from './Command' -import database, { ResponseData, NSFWData, LearnData } from './database' +import database, { + ResponseData, + NSFWData, + LearnData, + MaaDatabase, +} from './database' import noPerm from './noPerm' import { NODE_ENV } from './env' -export { ChatBot, Command, database, noPerm, ResponseData, NODE_ENV, NSFWData, LearnData } +export { + ChatBot, + Command, + database, + noPerm, + ResponseData, + NODE_ENV, + NSFWData, + LearnData, + MaaDatabase, +} diff --git a/yarn.lock b/yarn.lock index fd0b0db..efbe95d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -522,12 +522,12 @@ __metadata: languageName: node linkType: hard -"@types/node@npm:^20.9.4": - version: 20.9.4 - resolution: "@types/node@npm:20.9.4" +"@types/node@npm:^20.10.0": + version: 20.10.0 + resolution: "@types/node@npm:20.10.0" dependencies: undici-types: "npm:~5.26.4" - checksum: d567855b48e453b443499c17fc6c939d154732b54319a05b9b31db6e475e6458f053838635b201b1bb493d349d9b1af0aecc58b28fd6062e564e9fbf593199eb + checksum: c7d5ddbdbf3491e2363135c9611eb6bfae90eda2957279237fa232bcb29cd0df1cc3ee149d6de9915b754262a531ee2d57d33c9ecd58d763e8ad4856113822f3 languageName: node linkType: hard @@ -1763,7 +1763,7 @@ __metadata: resolution: "muffin-ai-arujak@workspace:." dependencies: "@migan/prettier-config": "npm:^1.0.0" - "@types/node": "npm:^20.9.4" + "@types/node": "npm:^20.10.0" cross-env: "npm:^7.0.3" discord.js: "npm:^14.14.1" dotenv: "npm:^16.3.1"