koishi-plugin-monetary-bank 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +33 -0
- package/lib/index.js +307 -0
- package/package.json +52 -0
- package/readme.md +17 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Context, Schema } from 'koishi';
|
|
2
|
+
export declare const name = "monetary-bank";
|
|
3
|
+
export interface Config {
|
|
4
|
+
defaultCurrency?: string;
|
|
5
|
+
debug?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare const Config: Schema<Config>;
|
|
8
|
+
export declare const inject: {
|
|
9
|
+
required: string[];
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* 数据库表结构声明(仅作类型提示)
|
|
13
|
+
* 表 `monetary_bank` 用于记录插件内的银行账户余额信息。
|
|
14
|
+
* 字段约定:
|
|
15
|
+
* - uid: 用户唯一 ID(由上层用户系统提供)
|
|
16
|
+
* - currency: 货币标识(例如 "coin")
|
|
17
|
+
* - value: 存款数值(数值类型)
|
|
18
|
+
* 注意:此处为类型声明与文档说明,不会主动修改数据库结构。
|
|
19
|
+
*/
|
|
20
|
+
declare module 'koishi' {
|
|
21
|
+
interface Tables {
|
|
22
|
+
monetary_bank: MonetaryBank;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export interface MonetaryBank {
|
|
26
|
+
uid: number;
|
|
27
|
+
currency: string;
|
|
28
|
+
value: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 插件主函数
|
|
32
|
+
*/
|
|
33
|
+
export declare function apply(ctx: Context, config: Config): Promise<void>;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name2 in all)
|
|
8
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
Config: () => Config,
|
|
24
|
+
apply: () => apply,
|
|
25
|
+
inject: () => inject,
|
|
26
|
+
name: () => name
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(src_exports);
|
|
29
|
+
var import_koishi = require("koishi");
|
|
30
|
+
var name = "monetary-bank";
|
|
31
|
+
var Config = import_koishi.Schema.object({
|
|
32
|
+
defaultCurrency: import_koishi.Schema.string().description("默认货币名称").default("coin"),
|
|
33
|
+
debug: import_koishi.Schema.boolean().description("开启调试日志(显示 info/success 等非 error/warn 日志)").default(true)
|
|
34
|
+
});
|
|
35
|
+
var inject = {
|
|
36
|
+
required: ["database"]
|
|
37
|
+
// 必须依赖数据库服务
|
|
38
|
+
};
|
|
39
|
+
var logger = new import_koishi.Logger("[monetary-bank]");
|
|
40
|
+
var debugEnabled = true;
|
|
41
|
+
function logInfo(...args) {
|
|
42
|
+
if (debugEnabled) {
|
|
43
|
+
;
|
|
44
|
+
logger.info && logger.info.apply(logger, args);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
__name(logInfo, "logInfo");
|
|
48
|
+
function logSuccess(...args) {
|
|
49
|
+
if (!debugEnabled) return;
|
|
50
|
+
if (typeof logger.success === "function") logger.success.apply(logger, args);
|
|
51
|
+
else logger.info && logger.info.apply(logger, args);
|
|
52
|
+
}
|
|
53
|
+
__name(logSuccess, "logSuccess");
|
|
54
|
+
async function initDatabase(ctx) {
|
|
55
|
+
try {
|
|
56
|
+
const tables = await ctx.database.tables;
|
|
57
|
+
if (tables && "monetary_bank" in tables) {
|
|
58
|
+
logInfo("检测到 monetary_bank 表已存在,正在验证表结构...");
|
|
59
|
+
const tableFields = tables["monetary_bank"];
|
|
60
|
+
const normalizeColumns = /* @__PURE__ */ __name((raw) => {
|
|
61
|
+
const map = {};
|
|
62
|
+
if (!raw) return map;
|
|
63
|
+
if (Array.isArray(raw)) {
|
|
64
|
+
for (const col of raw) {
|
|
65
|
+
if (!col) continue;
|
|
66
|
+
if (col.name && col.type) map[col.name] = String(col.type);
|
|
67
|
+
else if (col.field && col.type) map[col.field] = String(col.type);
|
|
68
|
+
else if (typeof col === "string") map[col] = "unknown";
|
|
69
|
+
}
|
|
70
|
+
return map;
|
|
71
|
+
}
|
|
72
|
+
if (raw.columns && typeof raw.columns === "object") {
|
|
73
|
+
for (const [k, v] of Object.entries(raw.columns)) {
|
|
74
|
+
if (v && typeof v === "object") {
|
|
75
|
+
const vv = v;
|
|
76
|
+
map[k] = (vv.type || vv.dataType || vv.typeName || JSON.stringify(vv)).toString();
|
|
77
|
+
} else {
|
|
78
|
+
map[k] = String(v);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return map;
|
|
82
|
+
}
|
|
83
|
+
if (typeof raw === "object") {
|
|
84
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
85
|
+
if (v && typeof v === "object") {
|
|
86
|
+
if (v.type && typeof v.type === "string") map[k] = v.type;
|
|
87
|
+
else if (v.type && typeof v.type === "object") map[k] = JSON.stringify(v.type);
|
|
88
|
+
else if (v.dataType) map[k] = v.dataType;
|
|
89
|
+
else map[k] = JSON.stringify(v);
|
|
90
|
+
} else {
|
|
91
|
+
map[k] = String(v);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return map;
|
|
95
|
+
}
|
|
96
|
+
return map;
|
|
97
|
+
}, "normalizeColumns");
|
|
98
|
+
const columns = normalizeColumns(tableFields);
|
|
99
|
+
logInfo("monetary_bank 表结构(规范化):", columns);
|
|
100
|
+
let structureValid = true;
|
|
101
|
+
const missingFields = [];
|
|
102
|
+
const incompatibleFields = [];
|
|
103
|
+
if (!structureValid) {
|
|
104
|
+
logger.error("❌ monetary_bank 表结构与插件要求不一致!");
|
|
105
|
+
if (missingFields.length > 0) {
|
|
106
|
+
logger.error(` 缺少字段: ${missingFields.join(", ")}`);
|
|
107
|
+
}
|
|
108
|
+
if (incompatibleFields.length > 0) {
|
|
109
|
+
logger.error(` 字段类型不兼容: ${incompatibleFields.join(", ")}`);
|
|
110
|
+
}
|
|
111
|
+
logger.error(" 插件将不会加载以防止数据损坏。");
|
|
112
|
+
logger.error(" 请检查数据库表结构或删除现有表后重启插件。");
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
logSuccess("✓ 表结构验证通过");
|
|
116
|
+
return true;
|
|
117
|
+
} else {
|
|
118
|
+
logInfo("monetary_bank 表不存在,正在创建...");
|
|
119
|
+
ctx.model.extend("monetary_bank", {
|
|
120
|
+
uid: {
|
|
121
|
+
type: "unsigned",
|
|
122
|
+
nullable: false
|
|
123
|
+
},
|
|
124
|
+
currency: {
|
|
125
|
+
type: "string",
|
|
126
|
+
nullable: false
|
|
127
|
+
},
|
|
128
|
+
value: {
|
|
129
|
+
type: "unsigned",
|
|
130
|
+
nullable: false
|
|
131
|
+
}
|
|
132
|
+
}, {
|
|
133
|
+
primary: ["uid", "currency"],
|
|
134
|
+
// 联合主键:用户ID + 货币类型
|
|
135
|
+
autoInc: false
|
|
136
|
+
// 不自增
|
|
137
|
+
});
|
|
138
|
+
logSuccess("✓ monetary_bank 表创建成功");
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
} catch (error) {
|
|
142
|
+
logger.error("初始化数据库时发生错误:", error);
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
__name(initDatabase, "initDatabase");
|
|
147
|
+
async function findMonetaryRecord(ctx, uid, currency) {
|
|
148
|
+
try {
|
|
149
|
+
const queryKeys = [{ uid }, { id: uid }];
|
|
150
|
+
for (const key of queryKeys) {
|
|
151
|
+
const recs = await ctx.database.get("monetary", key);
|
|
152
|
+
if (recs && recs.length > 0) {
|
|
153
|
+
const record = recs.find((r) => r.currency == currency) || recs[0];
|
|
154
|
+
const numFields = ["value", "balance", "amount", "coin", "money"];
|
|
155
|
+
const valueField = numFields.find((f) => typeof record[f] === "number") || Object.keys(record).find((k) => typeof record[k] === "number") || "value";
|
|
156
|
+
return { keyQuery: key, record, valueField };
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return null;
|
|
160
|
+
} catch (err) {
|
|
161
|
+
logger.warn("读取 monetary 表记录时出错:", err);
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
__name(findMonetaryRecord, "findMonetaryRecord");
|
|
166
|
+
async function createMonetaryUser(ctx, uid, currency) {
|
|
167
|
+
const attempts = [
|
|
168
|
+
{ uid, currency, value: 0 },
|
|
169
|
+
{ id: uid, currency, value: 0 }
|
|
170
|
+
];
|
|
171
|
+
for (const data of attempts) {
|
|
172
|
+
try {
|
|
173
|
+
await ctx.database.create("monetary", data);
|
|
174
|
+
logInfo(`在 monetary 表中创建了新用户记录`, data);
|
|
175
|
+
return true;
|
|
176
|
+
} catch (err) {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
logger.warn(`无法自动在 monetary 表中创建用户 uid=${uid},请手动添加`);
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
__name(createMonetaryUser, "createMonetaryUser");
|
|
184
|
+
async function getMonetaryBalance(ctx, uid, currency) {
|
|
185
|
+
const entry = await findMonetaryRecord(ctx, uid, currency);
|
|
186
|
+
if (!entry) return null;
|
|
187
|
+
const val = Number(entry.record[entry.valueField] || 0);
|
|
188
|
+
return Number.isNaN(val) ? null : val;
|
|
189
|
+
}
|
|
190
|
+
__name(getMonetaryBalance, "getMonetaryBalance");
|
|
191
|
+
async function changeMonetary(ctx, uid, currency, delta) {
|
|
192
|
+
try {
|
|
193
|
+
let entry = await findMonetaryRecord(ctx, uid, currency);
|
|
194
|
+
if (!entry) {
|
|
195
|
+
const created = await createMonetaryUser(ctx, uid, currency);
|
|
196
|
+
if (!created) return null;
|
|
197
|
+
entry = await findMonetaryRecord(ctx, uid, currency);
|
|
198
|
+
if (!entry) return null;
|
|
199
|
+
}
|
|
200
|
+
const { keyQuery, record, valueField } = entry;
|
|
201
|
+
const current = Number(record[valueField] || 0);
|
|
202
|
+
const newVal = current + delta;
|
|
203
|
+
if (newVal < 0) return null;
|
|
204
|
+
await ctx.database.set("monetary", keyQuery, { [valueField]: newVal });
|
|
205
|
+
return newVal;
|
|
206
|
+
} catch (err) {
|
|
207
|
+
logger.error("修改主货币余额失败:", err);
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
__name(changeMonetary, "changeMonetary");
|
|
212
|
+
async function apply(ctx, config) {
|
|
213
|
+
debugEnabled = typeof config?.debug === "boolean" ? config.debug : true;
|
|
214
|
+
const dbInitSuccess = await initDatabase(ctx);
|
|
215
|
+
if (!dbInitSuccess) {
|
|
216
|
+
logger.error("插件加载失败:数据库初始化未通过");
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
logSuccess("✓ monetary-bank 插件加载成功");
|
|
220
|
+
ctx.command("bank.bal", "查询银行存款").userFields(["id"]).option("currency", "-c <currency:string> 指定货币类型").action(async ({ session, options }) => {
|
|
221
|
+
const uid = session.user.id;
|
|
222
|
+
const currency = options?.currency || config.defaultCurrency || "coin";
|
|
223
|
+
try {
|
|
224
|
+
const records = await ctx.database.get("monetary_bank", {
|
|
225
|
+
uid,
|
|
226
|
+
currency
|
|
227
|
+
});
|
|
228
|
+
if (!records || records.length === 0) {
|
|
229
|
+
return `您在银行中还没有 ${currency} 存款。`;
|
|
230
|
+
}
|
|
231
|
+
const balance = records[0].value;
|
|
232
|
+
return `您的银行存款:${balance} ${currency}`;
|
|
233
|
+
} catch (error) {
|
|
234
|
+
logger.error("查询存款失败:", error);
|
|
235
|
+
return "查询失败,请稍后再试。";
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
ctx.command("bank.in <amount:number>", "存入货币到银行").userFields(["id"]).option("currency", "-c <currency:string> 指定货币类型").action(async ({ session, options }, amount) => {
|
|
239
|
+
if (!amount || amount <= 0) {
|
|
240
|
+
return "请输入有效的存款金额(正整数)。";
|
|
241
|
+
}
|
|
242
|
+
const uid = session.user.id;
|
|
243
|
+
const currency = options?.currency || config.defaultCurrency || "coin";
|
|
244
|
+
try {
|
|
245
|
+
let cash = await getMonetaryBalance(ctx, uid, currency);
|
|
246
|
+
if (cash === null) {
|
|
247
|
+
const created = await createMonetaryUser(ctx, uid, currency);
|
|
248
|
+
if (!created) return "无法验证/创建主货币账户(monetary),请联系管理员。";
|
|
249
|
+
cash = 0;
|
|
250
|
+
}
|
|
251
|
+
if (cash < amount) return `现金不足!当前现金:${cash} ${currency}`;
|
|
252
|
+
const newCash = await changeMonetary(ctx, uid, currency, -amount);
|
|
253
|
+
if (newCash === null) return "扣除现金失败,操作已中止。";
|
|
254
|
+
const records = await ctx.database.get("monetary_bank", { uid, currency });
|
|
255
|
+
let newValue;
|
|
256
|
+
if (!records || records.length === 0) {
|
|
257
|
+
await ctx.database.create("monetary_bank", { uid, currency, value: amount });
|
|
258
|
+
newValue = amount;
|
|
259
|
+
logInfo(`首次存款,创建 monetary_bank 记录 uid=${uid}, currency=${currency}, value=${amount}`);
|
|
260
|
+
} else {
|
|
261
|
+
const current = Number(records[0].value) || 0;
|
|
262
|
+
newValue = current + amount;
|
|
263
|
+
await ctx.database.set("monetary_bank", { uid, currency }, { value: newValue });
|
|
264
|
+
}
|
|
265
|
+
return `成功存入 ${amount} ${currency}!现金余额:${newCash} ${currency};银行存款:${newValue} ${currency}`;
|
|
266
|
+
} catch (error) {
|
|
267
|
+
logger.error("存款失败:", error);
|
|
268
|
+
return "存款失败,请稍后再试。";
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
ctx.command("bank.out <amount:number>", "从银行取出货币").userFields(["id"]).option("currency", "-c <currency:string> 指定货币类型").action(async ({ session, options }, amount) => {
|
|
272
|
+
if (!amount || amount <= 0) {
|
|
273
|
+
return "请输入有效的取款金额(正整数)。";
|
|
274
|
+
}
|
|
275
|
+
const uid = session.user.id;
|
|
276
|
+
const currency = options?.currency || config.defaultCurrency || "coin";
|
|
277
|
+
try {
|
|
278
|
+
const records = await ctx.database.get("monetary_bank", { uid, currency });
|
|
279
|
+
if (!records || records.length === 0) {
|
|
280
|
+
return `您在银行中没有 ${currency} 存款。`;
|
|
281
|
+
}
|
|
282
|
+
const currentBalance = Number(records[0].value || 0);
|
|
283
|
+
if (currentBalance < amount) {
|
|
284
|
+
return `余额不足!当前存款:${currentBalance} ${currency}`;
|
|
285
|
+
}
|
|
286
|
+
const newBankValue = currentBalance - amount;
|
|
287
|
+
await ctx.database.set("monetary_bank", { uid, currency }, { value: newBankValue });
|
|
288
|
+
const newCash = await changeMonetary(ctx, uid, currency, amount);
|
|
289
|
+
if (newCash === null) {
|
|
290
|
+
await ctx.database.set("monetary_bank", { uid, currency }, { value: currentBalance });
|
|
291
|
+
return "转账到现金失败,操作已回滚,请联系管理员。";
|
|
292
|
+
}
|
|
293
|
+
return `成功取出 ${amount} ${currency}!现金余额:${newCash} ${currency};银行剩余:${newBankValue} ${currency}`;
|
|
294
|
+
} catch (error) {
|
|
295
|
+
logger.error("取款失败:", error);
|
|
296
|
+
return "取款失败,请稍后再试。";
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
__name(apply, "apply");
|
|
301
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
302
|
+
0 && (module.exports = {
|
|
303
|
+
Config,
|
|
304
|
+
apply,
|
|
305
|
+
inject,
|
|
306
|
+
name
|
|
307
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "koishi-plugin-monetary-bank",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "lib/index.js",
|
|
5
|
+
"typings": "lib/index.d.ts",
|
|
6
|
+
"files": [
|
|
7
|
+
"lib",
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"koishi": {
|
|
11
|
+
"description": {
|
|
12
|
+
"en": "`Provides banking related services for monetary money`",
|
|
13
|
+
"zh": "为monetary货币提供银行相关服务"
|
|
14
|
+
},
|
|
15
|
+
"service": {
|
|
16
|
+
"required": [
|
|
17
|
+
"database",
|
|
18
|
+
"monetary"
|
|
19
|
+
]
|
|
20
|
+
},
|
|
21
|
+
"preview:": true
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"keywords": [
|
|
25
|
+
"chatbot",
|
|
26
|
+
"koishi",
|
|
27
|
+
"plugin",
|
|
28
|
+
"bank",
|
|
29
|
+
"银行",
|
|
30
|
+
"金融",
|
|
31
|
+
"monetary",
|
|
32
|
+
"货币"
|
|
33
|
+
],
|
|
34
|
+
"author": {
|
|
35
|
+
"name": "BYWled",
|
|
36
|
+
"email": "bywled@qq.com",
|
|
37
|
+
"url": "https://github.com/BYWled"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/BYWled/jrys-fix-ranks",
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/BYWled/jrys-fix-ranks"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"koishi": "4.18.9",
|
|
46
|
+
"koishi-plugin-monetary": "^1.0.0"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"koishi": "^4.18.0",
|
|
50
|
+
"koishi-plugin-monetary": "^1.0.0"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# koishi-plugin-monetary-bank | 货币银行
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/koishi-plugin-monetary-bank)
|
|
4
|
+
|
|
5
|
+
`Provides banking related services for monetary money`
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
`为monetary货币提供银行相关服务`
|
|
10
|
+
|
|
11
|
+
~~虽然对于虚拟金币来说做个银行有一种多此一举的美,但~~本插件可以提供迷你版的银行服务。
|
|
12
|
+
|
|
13
|
+
## 更新日志
|
|
14
|
+
|
|
15
|
+
### 1.0.0:
|
|
16
|
+
|
|
17
|
+
- 提供了查询、存取银行余额的服务
|