Commit ded6b8db authored by nanahira's avatar nanahira

support better param

parent da4a2e92
Pipeline #3115 passed with stages
in 10 minutes and 25 seconds
......@@ -26,10 +26,12 @@ export interface CommonResult {
reason?: string;
result?: number;
}
export interface RollResult extends CommonResult {
export interface DiceParam {
count: number;
size: number;
}
export interface RollResult extends CommonResult, DiceParam {
formula?: string;
results?: number[];
}
......@@ -196,6 +198,27 @@ export class AppService {
return { user, group, profile, banReason: null };
}
parseDiceParam(param: string): DiceParam {
if (!param) {
return null;
}
let match = param.match(/^d?(\d+)$/);
if (match) {
return {
size: parseInt(match[1]),
count: 1,
};
}
match = param.match(/^(\d+)d(\d+)$/);
if (match) {
return {
size: parseInt(match[2]),
count: parseInt(match[1]),
};
}
return null;
}
async rollDice(
rollResult: RollResult,
userData: KoishiSessionLike,
......
......@@ -87,14 +87,14 @@ export class BotController {
await this.appService.banForKicked(session.groupId, session.operatorId);
});
globalCtx
.command('rolldice', '投掷骰子')
.command('rolldice [param:string]', '投掷骰子')
.option('count', '-c <count:posint> 骰子数量', { fallback: 1 })
.option('size', '-s <count:posint> 骰子面数', { fallback: 6 })
.option('reason', '-r <reason:text> 骰子说明')
.alias('rd', 'roll', 'r')
.usage('也支持 .rd<size> 和 .r<count>d<size> [reason] 这样的传统语法。')
.example('.rolldice -c 2 -s 6 -r "行动判定"')
.action(async (argv, args) => {
.action(async (argv, param) => {
const session = argv.session;
const rollResult = {
name: session.username,
......@@ -102,6 +102,7 @@ export class BotController {
size: 6,
reason: null,
...argv.options,
...this.appService.parseDiceParam(param),
};
return await this.appService.rollDice(rollResult, session);
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment