Skip to content

Commit 43de019

Browse files
committed
feat: dice plugin
1 parent 9c4f315 commit 43de019

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

apps/dice.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import PLUGIN_ID from '#gc.id';
2+
import { Plugin, Logger, Config } from '#gc';
3+
4+
import _ from 'lodash';
5+
6+
export class DicePlugin extends Plugin {
7+
constructor() {
8+
super({
9+
name: '骰娘插件',
10+
dsc: '随机数生成器,支持多种骰子类型与复杂表达式',
11+
event: 'message',
12+
priority: '98',
13+
rule: [
14+
{
15+
// Basic dice: [m]d/D[n] (e.g. 1d6, 2d10, 3D20)
16+
reg: '^([1-9]\\d?)[dD]([1-9]\\d{0,5})$',
17+
fnc: 'basic_dice'
18+
}
19+
]
20+
});
21+
}
22+
23+
async basic_dice() {
24+
const msg = this.e.msg;
25+
const dicePattern = /^([1-9]\d?)[dD]([1-9]\d{0,5})$/;
26+
const match = msg.match(dicePattern);
27+
28+
if (!match) return; // should not happen due to rule
29+
30+
const numDice = parseInt(match[1]);
31+
const numSides = parseInt(match[2]);
32+
33+
if (numDice >= 1 && numDice < 100 && numSides >= 1) {
34+
const rolls: number[] = [];
35+
for (let i = 0; i < numDice; i++) {
36+
rolls.push(_.random(1, numSides));
37+
}
38+
const total = rolls.reduce((a, b) => a + b, 0);
39+
const response = `你掷出了 ${numDice}d${numSides}:\n点数:${rolls.join(', ')}\n总和:${total}`;
40+
await this.e.reply(response, true);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)