File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ? ) [ d D ] ( [ 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+ }
You can’t perform that action at this time.
0 commit comments