koishi-plugin-maple-dice-v2 0.0.5 → 0.0.7
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.js +35 -11
- package/package.json +1 -1
- package/lib/index.d.ts +0 -24
package/lib/index.js
CHANGED
|
@@ -34,14 +34,30 @@ var DiceRoller = class {
|
|
|
34
34
|
__name(this, "DiceRoller");
|
|
35
35
|
}
|
|
36
36
|
static parseDice(expression) {
|
|
37
|
-
if (!expression) {
|
|
37
|
+
if (!expression || expression.trim() === "") {
|
|
38
38
|
expression = "1d100";
|
|
39
39
|
}
|
|
40
40
|
expression = expression.replace(/\s+/g, "").toLowerCase();
|
|
41
41
|
const diceRegex = /^(\d+)?d(\d+)(?:([+\-*/])(\d+))?$/;
|
|
42
|
-
|
|
42
|
+
let match = expression.match(diceRegex);
|
|
43
43
|
if (!match) {
|
|
44
|
-
|
|
44
|
+
if (/^\d+d$/.test(expression)) {
|
|
45
|
+
expression = expression + "100";
|
|
46
|
+
match = expression.match(diceRegex);
|
|
47
|
+
} else if (/^d\d+$/.test(expression)) {
|
|
48
|
+
expression = "1" + expression;
|
|
49
|
+
match = expression.match(diceRegex);
|
|
50
|
+
} else if (/^\d+$/.test(expression)) {
|
|
51
|
+
expression = expression + "d100";
|
|
52
|
+
match = expression.match(diceRegex);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!match) {
|
|
56
|
+
expression = "1d100";
|
|
57
|
+
match = expression.match(diceRegex);
|
|
58
|
+
if (!match) {
|
|
59
|
+
throw new Error("无效的掷骰表达式。正确格式: r xdy[+-*/]z");
|
|
60
|
+
}
|
|
45
61
|
}
|
|
46
62
|
const count = match[1] ? parseInt(match[1]) : 1;
|
|
47
63
|
const sides = parseInt(match[2]);
|
|
@@ -53,6 +69,9 @@ var DiceRoller = class {
|
|
|
53
69
|
if (count > 100) {
|
|
54
70
|
throw new Error("一次最多只能掷 100 个骰子");
|
|
55
71
|
}
|
|
72
|
+
if (operator === "/" && modifier === 0) {
|
|
73
|
+
throw new Error("除数不能为0");
|
|
74
|
+
}
|
|
56
75
|
const rolls = [];
|
|
57
76
|
for (let i = 0; i < count; i++) {
|
|
58
77
|
rolls.push(Math.floor(Math.random() * sides) + 1);
|
|
@@ -70,9 +89,6 @@ var DiceRoller = class {
|
|
|
70
89
|
total = diceTotal * modifier;
|
|
71
90
|
break;
|
|
72
91
|
case "/":
|
|
73
|
-
if (modifier === 0) {
|
|
74
|
-
throw new Error("除数不能为0");
|
|
75
|
-
}
|
|
76
92
|
total = Math.floor(diceTotal / modifier);
|
|
77
93
|
break;
|
|
78
94
|
default:
|
|
@@ -262,18 +278,26 @@ function apply(ctx, config) {
|
|
|
262
278
|
const diceCommands = ["r", "ra", "rb", "rp"];
|
|
263
279
|
if (diceCommands.includes(commandName)) {
|
|
264
280
|
try {
|
|
281
|
+
let result = "";
|
|
265
282
|
switch (commandName) {
|
|
266
283
|
case "r":
|
|
267
|
-
|
|
284
|
+
result = await handleRCommand(session, args);
|
|
285
|
+
break;
|
|
268
286
|
case "ra":
|
|
269
|
-
|
|
287
|
+
result = await handleRACommand(session, args);
|
|
288
|
+
break;
|
|
270
289
|
case "rb":
|
|
271
|
-
|
|
290
|
+
result = await handleRBCommand(session, args);
|
|
291
|
+
break;
|
|
272
292
|
case "rp":
|
|
273
|
-
|
|
293
|
+
result = await handleRPCommand(session, args);
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
if (result) {
|
|
297
|
+
session.send(result);
|
|
274
298
|
}
|
|
275
299
|
} catch (error) {
|
|
276
|
-
|
|
300
|
+
session.send(`错误: ${error.message}`);
|
|
277
301
|
}
|
|
278
302
|
}
|
|
279
303
|
}
|
package/package.json
CHANGED
package/lib/index.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Context, Schema } from 'koishi';
|
|
2
|
-
export declare const name = "maple-dice-v2";
|
|
3
|
-
export declare const using: readonly ["database"];
|
|
4
|
-
declare module 'koishi' {
|
|
5
|
-
interface Tables {
|
|
6
|
-
'maple-dice-responses': DiceResponse;
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
interface DiceResponse {
|
|
10
|
-
id: number;
|
|
11
|
-
level: string;
|
|
12
|
-
content: string;
|
|
13
|
-
author: string;
|
|
14
|
-
group: string;
|
|
15
|
-
created: Date;
|
|
16
|
-
}
|
|
17
|
-
export interface Config {
|
|
18
|
-
defaultSkill: number;
|
|
19
|
-
replyMode: 'global' | 'group' | 'personal';
|
|
20
|
-
commandPrefixes: string[];
|
|
21
|
-
}
|
|
22
|
-
export declare const Config: Schema<Config>;
|
|
23
|
-
export declare function apply(ctx: Context, config: Config): void;
|
|
24
|
-
export {};
|