koishi-plugin-monetary-command-link 0.0.2
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 +34 -0
- package/lib/index.js +124 -0
- package/lib/locales/en-US.d.ts +12 -0
- package/lib/locales/zh-CN.d.ts +12 -0
- package/package.json +22 -0
- package/readme.md +5 -0
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Computed, Context, Schema, Session } from 'koishi';
|
|
2
|
+
declare module 'koishi' {
|
|
3
|
+
interface Context {
|
|
4
|
+
monetary: {
|
|
5
|
+
cost(uid: number, cost: number, currency?: string): Promise<void>;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
interface Events {
|
|
9
|
+
'help/command'(output: string[], command: Command, session: Session<never, never>): void;
|
|
10
|
+
}
|
|
11
|
+
namespace Command {
|
|
12
|
+
interface Config {
|
|
13
|
+
monetaryCost?: Computed<number>;
|
|
14
|
+
monetaryCurrency?: Computed<string>;
|
|
15
|
+
monetaryLabel?: Computed<string>;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export declare const name = "monetary-command-link";
|
|
20
|
+
export declare const inject: string[];
|
|
21
|
+
export interface LinkConfig {
|
|
22
|
+
command: string;
|
|
23
|
+
cost: number;
|
|
24
|
+
currency: string;
|
|
25
|
+
inherit?: boolean;
|
|
26
|
+
insufficientMessage?: string;
|
|
27
|
+
helpText?: string;
|
|
28
|
+
}
|
|
29
|
+
export interface Config {
|
|
30
|
+
links: LinkConfig[];
|
|
31
|
+
currencyLabels: Record<string, string>;
|
|
32
|
+
}
|
|
33
|
+
export declare const Config: Schema<Config>;
|
|
34
|
+
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
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-command-link";
|
|
31
|
+
var inject = ["monetary"];
|
|
32
|
+
var Config = import_koishi.Schema.object({
|
|
33
|
+
links: import_koishi.Schema.array(import_koishi.Schema.object({
|
|
34
|
+
command: import_koishi.Schema.string().required().description("要扣费的指令名或别名。"),
|
|
35
|
+
cost: import_koishi.Schema.number().min(0).default(0).description("每次调用的消耗数量。"),
|
|
36
|
+
currency: import_koishi.Schema.string().default("default").description("使用的货币类型。"),
|
|
37
|
+
inherit: import_koishi.Schema.boolean().default(false).description("是否对子指令生效。"),
|
|
38
|
+
insufficientMessage: import_koishi.Schema.string().default("你没有足够的{label}。").description("余额不足时的提示模板。"),
|
|
39
|
+
helpText: import_koishi.Schema.string().default("花费:{cost} {label}").description("帮助中显示的扣费文本模板。")
|
|
40
|
+
})).default([]).description("指令扣费规则。"),
|
|
41
|
+
currencyLabels: import_koishi.Schema.dict(String).default({}).description("货币类型到显示名称的映射。")
|
|
42
|
+
});
|
|
43
|
+
function getDefaultLabel(currency, config) {
|
|
44
|
+
if (config.currencyLabels[currency]) return config.currencyLabels[currency];
|
|
45
|
+
if (currency === "default") return "点数";
|
|
46
|
+
return currency;
|
|
47
|
+
}
|
|
48
|
+
__name(getDefaultLabel, "getDefaultLabel");
|
|
49
|
+
function matchesRule(command, name2, inherit = false) {
|
|
50
|
+
if (command.name === name2 || name2 in command._aliases) return true;
|
|
51
|
+
if (!inherit) return false;
|
|
52
|
+
if (command.name.startsWith(`${name2}.`)) return true;
|
|
53
|
+
return Object.keys(command._aliases).some((alias) => alias.startsWith(`${name2}.`));
|
|
54
|
+
}
|
|
55
|
+
__name(matchesRule, "matchesRule");
|
|
56
|
+
function formatTemplate(template, command, charge) {
|
|
57
|
+
return template.replace(/\{cost\}/g, String(charge.cost)).replace(/\{currency\}/g, charge.currency).replace(/\{label\}/g, charge.label).replace(/\{command\}/g, command.displayName).replace(/\{name\}/g, command.name);
|
|
58
|
+
}
|
|
59
|
+
__name(formatTemplate, "formatTemplate");
|
|
60
|
+
function renderText(template, command, charge) {
|
|
61
|
+
return formatTemplate(template || "", command, charge);
|
|
62
|
+
}
|
|
63
|
+
__name(renderText, "renderText");
|
|
64
|
+
function resolveCharge(command, session, config) {
|
|
65
|
+
const link = config.links.find((item) => item.cost > 0 && matchesRule(command, item.command, item.inherit));
|
|
66
|
+
if (link) {
|
|
67
|
+
const label2 = getDefaultLabel(link.currency, config);
|
|
68
|
+
return {
|
|
69
|
+
cost: link.cost,
|
|
70
|
+
currency: link.currency,
|
|
71
|
+
label: label2,
|
|
72
|
+
insufficientMessage: link.insufficientMessage,
|
|
73
|
+
helpText: link.helpText
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const cost = session.resolve(command.config.monetaryCost) ?? 0;
|
|
77
|
+
if (cost <= 0) return null;
|
|
78
|
+
const currency = session.resolve(command.config.monetaryCurrency) || "default";
|
|
79
|
+
const label = session.resolve(command.config.monetaryLabel) || getDefaultLabel(currency, config);
|
|
80
|
+
return {
|
|
81
|
+
cost,
|
|
82
|
+
currency,
|
|
83
|
+
label,
|
|
84
|
+
insufficientMessage: "你没有足够的{label}。",
|
|
85
|
+
helpText: "花费:{cost} {label}"
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
__name(resolveCharge, "resolveCharge");
|
|
89
|
+
function apply(ctx, config) {
|
|
90
|
+
ctx.schema.extend("command", import_koishi.Schema.object({
|
|
91
|
+
monetaryCost: import_koishi.Schema.computed(Number).default(0).description("每次调用的货币消耗。"),
|
|
92
|
+
monetaryCurrency: import_koishi.Schema.computed(String).default("default").description("调用时消耗的货币类型。"),
|
|
93
|
+
monetaryLabel: import_koishi.Schema.computed(String).description("帮助中显示的货币名称。")
|
|
94
|
+
}), 850);
|
|
95
|
+
ctx.before("command/execute", async (argv) => {
|
|
96
|
+
const { session, options, command } = argv;
|
|
97
|
+
let isUsage = true;
|
|
98
|
+
for (const option of Object.values(command._options)) {
|
|
99
|
+
const { name: name2, notUsage } = option;
|
|
100
|
+
if (name2 in options && notUsage) isUsage = false;
|
|
101
|
+
}
|
|
102
|
+
if (!isUsage) return;
|
|
103
|
+
const charge = resolveCharge(command, session, config);
|
|
104
|
+
if (!charge) return;
|
|
105
|
+
try {
|
|
106
|
+
await ctx.monetary.cost(session.user.id, charge.cost, charge.currency);
|
|
107
|
+
} catch {
|
|
108
|
+
return renderText(charge.insufficientMessage, command, charge);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
ctx.on("help/command", (output, command, session) => {
|
|
112
|
+
const charge = resolveCharge(command, session, config);
|
|
113
|
+
if (!charge) return;
|
|
114
|
+
output.push(renderText(charge.helpText, command, charge));
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
__name(apply, "apply");
|
|
118
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
119
|
+
0 && (module.exports = {
|
|
120
|
+
Config,
|
|
121
|
+
apply,
|
|
122
|
+
inject,
|
|
123
|
+
name
|
|
124
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "koishi-plugin-monetary-command-link",
|
|
3
|
+
"description": "调用指令时扣费",
|
|
4
|
+
"version": "0.0.2",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"typings": "lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib",
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"scripts": {},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"chatbot",
|
|
15
|
+
"koishi",
|
|
16
|
+
"plugin"
|
|
17
|
+
],
|
|
18
|
+
"devDependencies": {},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"koishi": "4.18.11"
|
|
21
|
+
}
|
|
22
|
+
}
|