koishi-plugin-maple-dice-v2 0.0.4 → 0.0.5
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 +24 -0
- package/lib/index.js +107 -51
- package/package.json +1 -1
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
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 {};
|
package/lib/index.js
CHANGED
|
@@ -173,7 +173,8 @@ var Config = import_koishi.Schema.object({
|
|
|
173
173
|
import_koishi.Schema.const("global").description("全局: 调用所有自定义回复"),
|
|
174
174
|
import_koishi.Schema.const("group").description("群组: 仅调用当前群组内添加的所有自定义回复(私聊时,仅调用本人私聊添加的所有自定义回复)"),
|
|
175
175
|
import_koishi.Schema.const("personal").description("个人: 仅调用由本人添加的所有自定义回复")
|
|
176
|
-
]).default("global").description("回复模式")
|
|
176
|
+
]).default("global").description("回复模式"),
|
|
177
|
+
commandPrefixes: import_koishi.Schema.array(String).default([".", "。"]).description("指令识别符(如[., 。],使用.r、。ra等触发掷骰指令)")
|
|
177
178
|
});
|
|
178
179
|
var keyToLevelName = {
|
|
179
180
|
"greatSuccess": "大成功",
|
|
@@ -243,9 +244,44 @@ function apply(ctx, config) {
|
|
|
243
244
|
primary: "id",
|
|
244
245
|
autoInc: true
|
|
245
246
|
});
|
|
246
|
-
ctx.
|
|
247
|
+
ctx.middleware(async (session, next) => {
|
|
248
|
+
const prefixes = config.commandPrefixes;
|
|
249
|
+
const content = session.content;
|
|
250
|
+
if (!prefixes || prefixes.length === 0 || !content || content.length === 0) {
|
|
251
|
+
return next();
|
|
252
|
+
}
|
|
253
|
+
const escapedPrefixes = prefixes.map(
|
|
254
|
+
(p) => p.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
|
|
255
|
+
).join("|");
|
|
256
|
+
const regex = new RegExp(`(?:${escapedPrefixes})([rR][aApPbB]?)\\s*([^\\s]*)?`, "g");
|
|
257
|
+
const matches = Array.from(content.matchAll(regex));
|
|
258
|
+
if (matches.length > 0) {
|
|
259
|
+
const match = matches[0];
|
|
260
|
+
const commandName = match[1].toLowerCase();
|
|
261
|
+
const args = match[2] || "";
|
|
262
|
+
const diceCommands = ["r", "ra", "rb", "rp"];
|
|
263
|
+
if (diceCommands.includes(commandName)) {
|
|
264
|
+
try {
|
|
265
|
+
switch (commandName) {
|
|
266
|
+
case "r":
|
|
267
|
+
return await handleRCommand(session, args);
|
|
268
|
+
case "ra":
|
|
269
|
+
return await handleRACommand(session, args);
|
|
270
|
+
case "rb":
|
|
271
|
+
return await handleRBCommand(session, args);
|
|
272
|
+
case "rp":
|
|
273
|
+
return await handleRPCommand(session, args);
|
|
274
|
+
}
|
|
275
|
+
} catch (error) {
|
|
276
|
+
return `错误: ${error.message}`;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return next();
|
|
281
|
+
}, true);
|
|
282
|
+
async function handleRCommand(session, args) {
|
|
247
283
|
try {
|
|
248
|
-
const result = DiceRoller.parseDice(
|
|
284
|
+
const result = DiceRoller.parseDice(args || void 0);
|
|
249
285
|
let output = `r ${result.expression} = `;
|
|
250
286
|
if (!result.operator) {
|
|
251
287
|
if (result.count === 1) {
|
|
@@ -264,9 +300,16 @@ function apply(ctx, config) {
|
|
|
264
300
|
} catch (error) {
|
|
265
301
|
return `错误: ${error.message}`;
|
|
266
302
|
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
|
|
303
|
+
}
|
|
304
|
+
__name(handleRCommand, "handleRCommand");
|
|
305
|
+
async function handleRACommand(session, args) {
|
|
306
|
+
let skillValue = config.defaultSkill;
|
|
307
|
+
if (args) {
|
|
308
|
+
const parsedSkill = parseInt(args);
|
|
309
|
+
if (!isNaN(parsedSkill)) {
|
|
310
|
+
skillValue = parsedSkill;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
270
313
|
if (skillValue < 1 || skillValue > 100) {
|
|
271
314
|
return "技能值必须在1-100之间";
|
|
272
315
|
}
|
|
@@ -275,56 +318,69 @@ function apply(ctx, config) {
|
|
|
275
318
|
const levelText = DiceRoller.getSuccessLevelText(result.successLevel);
|
|
276
319
|
return `ra ${skillValue}=${result.diceValue}/${skillValue} ${levelText}
|
|
277
320
|
${response}`;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
321
|
+
}
|
|
322
|
+
__name(handleRACommand, "handleRACommand");
|
|
323
|
+
async function handleRBCommand(session, args) {
|
|
324
|
+
if (!args) {
|
|
325
|
+
return "请指定奖励骰数量和技能值,格式: .rb 3/70 或 .rb 2";
|
|
326
|
+
}
|
|
327
|
+
const parts = args.split("/");
|
|
328
|
+
const tensCount = parseInt(parts[0]);
|
|
329
|
+
if (isNaN(tensCount) || tensCount < 1) {
|
|
330
|
+
return "奖励骰数量必须是正整数";
|
|
331
|
+
}
|
|
332
|
+
let skillValue = config.defaultSkill;
|
|
333
|
+
if (parts.length > 1) {
|
|
334
|
+
skillValue = parseInt(parts[1]);
|
|
335
|
+
if (isNaN(skillValue) || skillValue < 1 || skillValue > 100) {
|
|
336
|
+
return "技能值必须在1-100之间";
|
|
292
337
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
338
|
+
}
|
|
339
|
+
const result = DiceRoller.bonusPenaltyCheck(tensCount, skillValue, true);
|
|
340
|
+
const response = await getRandomResponse(ctx, session, config, result.successLevel, result.diceValue, skillValue);
|
|
341
|
+
const levelText = DiceRoller.getSuccessLevelText(result.successLevel);
|
|
342
|
+
const tensDisplay = `[${result.tens.join(",")}]`;
|
|
343
|
+
const unitDisplay = `[${result.unit}]`;
|
|
344
|
+
return `rb ${tensCount}/${skillValue}=${tensDisplay}+${unitDisplay}=${result.diceValue}/${skillValue} ${levelText}
|
|
299
345
|
${response}`;
|
|
300
|
-
|
|
301
|
-
|
|
346
|
+
}
|
|
347
|
+
__name(handleRBCommand, "handleRBCommand");
|
|
348
|
+
async function handleRPCommand(session, args) {
|
|
349
|
+
if (!args) {
|
|
350
|
+
return "请指定惩罚骰数量和技能值,格式: .rp 3/70 或 .rp 2";
|
|
351
|
+
}
|
|
352
|
+
const parts = args.split("/");
|
|
353
|
+
const tensCount = parseInt(parts[0]);
|
|
354
|
+
if (isNaN(tensCount) || tensCount < 1) {
|
|
355
|
+
return "惩罚骰数量必须是正整数";
|
|
356
|
+
}
|
|
357
|
+
let skillValue = config.defaultSkill;
|
|
358
|
+
if (parts.length > 1) {
|
|
359
|
+
skillValue = parseInt(parts[1]);
|
|
360
|
+
if (isNaN(skillValue) || skillValue < 1 || skillValue > 100) {
|
|
361
|
+
return "技能值必须在1-100之间";
|
|
362
|
+
}
|
|
302
363
|
}
|
|
364
|
+
const result = DiceRoller.bonusPenaltyCheck(tensCount, skillValue, false);
|
|
365
|
+
const response = await getRandomResponse(ctx, session, config, result.successLevel, result.diceValue, skillValue);
|
|
366
|
+
const levelText = DiceRoller.getSuccessLevelText(result.successLevel);
|
|
367
|
+
const tensDisplay = `[${result.tens.join(",")}]`;
|
|
368
|
+
const unitDisplay = `[${result.unit}]`;
|
|
369
|
+
return `rp ${tensCount}/${skillValue}=${tensDisplay}+${unitDisplay}=${result.diceValue}/${skillValue} ${levelText}
|
|
370
|
+
${response}`;
|
|
371
|
+
}
|
|
372
|
+
__name(handleRPCommand, "handleRPCommand");
|
|
373
|
+
ctx.command("r [expression:string]", "基础掷骰(默认为1d100)").alias("roll").alias("掷骰").example("r 3d6 掷3个6面骰").example("r 2d20+5 掷2个20面骰并加5").action(async ({ session }, expression) => {
|
|
374
|
+
return await handleRCommand(session, expression);
|
|
375
|
+
});
|
|
376
|
+
ctx.command("ra [skill:string]", "技能检定").alias("检定").example("ra 使用默认技能值进行检定").example("ra 80 使用80点技能值进行检定").action(async ({ session }, skill) => {
|
|
377
|
+
return await handleRACommand(session, skill);
|
|
378
|
+
});
|
|
379
|
+
ctx.command("rb <input:string>", "奖励骰检定").example("rb 3/70 进行3个奖励骰的70点技能检定").example("rb 2 进行2个奖励骰,使用默认技能值").action(async ({ session }, input) => {
|
|
380
|
+
return await handleRBCommand(session, input);
|
|
303
381
|
});
|
|
304
382
|
ctx.command("rp <input:string>", "惩罚骰检定").example("rp 3/70 进行3个惩罚骰的70点技能检定").example("rp 2 进行2个惩罚骰,使用默认技能值").action(async ({ session }, input) => {
|
|
305
|
-
|
|
306
|
-
const parts = input.split("/");
|
|
307
|
-
const tensCount = parseInt(parts[0]);
|
|
308
|
-
if (isNaN(tensCount) || tensCount < 1) {
|
|
309
|
-
return "惩罚骰数量必须是正整数";
|
|
310
|
-
}
|
|
311
|
-
let skillValue = config.defaultSkill;
|
|
312
|
-
if (parts.length > 1) {
|
|
313
|
-
skillValue = parseInt(parts[1]);
|
|
314
|
-
if (isNaN(skillValue) || skillValue < 1 || skillValue > 100) {
|
|
315
|
-
return "技能值必须在1-100之间";
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
const result = DiceRoller.bonusPenaltyCheck(tensCount, skillValue, false);
|
|
319
|
-
const response = await getRandomResponse(ctx, session, config, result.successLevel, result.diceValue, skillValue);
|
|
320
|
-
const levelText = DiceRoller.getSuccessLevelText(result.successLevel);
|
|
321
|
-
const tensDisplay = `[${result.tens.join(",")}]`;
|
|
322
|
-
const unitDisplay = `[${result.unit}]`;
|
|
323
|
-
return `rp ${tensCount}/${skillValue}=${tensDisplay}+${unitDisplay}=${result.diceValue}/${skillValue} ${levelText}
|
|
324
|
-
${response}`;
|
|
325
|
-
} catch (error) {
|
|
326
|
-
return `错误: ${error.message}`;
|
|
327
|
-
}
|
|
383
|
+
return await handleRPCommand(session, input);
|
|
328
384
|
});
|
|
329
385
|
ctx.command("添加自定义回复 <level:string> <text:string>", "添加自定义回复(用&分隔多条回复)").alias("添加回复").example("添加自定义回复 大成功 太棒了!大成功!骰值:{result}").example("添加自定义回复 成功 成功!骰值:{result}&干得漂亮!骰值:{result}").action(async ({ session }, level, text) => {
|
|
330
386
|
const validLevels = ["大成功", "极难成功", "困难成功", "成功", "失败", "大失败"];
|