This commit is contained in:
Siwoo Jeon 2023-11-27 22:38:33 +09:00
parent 3cd80e646f
commit d11050f811
Signed by: migan
GPG key ID: C4151385FFD2082A
6 changed files with 124 additions and 21 deletions

22
.pnp.cjs generated
View file

@ -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],\

View file

@ -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",

View file

@ -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<T, V> {
name: string
all(): Promise<T[]>
findOne(key: V): Promise<T[]>
insert(data: T): Promise<void>
update(data: T): Promise<void>
delete(key: V): Promise<void>
}
async function run(db: PoolConnection, fn: () => Promise<void>) {
try {
await db.beginTransaction()
await fn()
await db.commit()
} catch (err) {
console.error(err)
await db.rollback()
} finally {
db.release()
}
}
class StatementTable implements BassTable<ResponseData, number> {
public name = 'statement'
public constructor(private _database: Pool) {}
public async all(): Promise<ResponseData[]> {
const [rows] = await this._database.execute<ResponseData[]>(
'SELECT * FROM statement;',
)
return rows
}
public async findOne(key: number): Promise<ResponseData[]> {
const [rows] = await this._database.execute<ResponseData[]>(
'SELECT * FROM statement WHERE id = ?;',
[key],
)
return rows
}
public async insert(data: ResponseData): Promise<void> {
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<void> {
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<void> {
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,

View file

@ -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,
}

View file

@ -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"