Commit 09456eec authored by nanahira's avatar nanahira

improve shortcut grammer and .st

parent 12ae7d4f
...@@ -2,30 +2,26 @@ import { DiceModule } from '../utility/utility'; ...@@ -2,30 +2,26 @@ import { DiceModule } from '../utility/utility';
import { BaseModule } from '../utility/base-module'; import { BaseModule } from '../utility/base-module';
import { UseMiddleware } from 'koishi-thirdeye'; import { UseMiddleware } from 'koishi-thirdeye';
import { Next, Session } from 'koishi'; import { Next, Session } from 'koishi';
import { rcRegexp, rollRegexp } from '../utility/constant';
@DiceModule() @DiceModule()
export class CompatModule extends BaseModule { export class CompatModule extends BaseModule {
private checkCommands = ['st', 'rc', 'ra', 'r'] as const;
@UseMiddleware() @UseMiddleware()
onRollCompat(session: Session, next: Next) { onRollCompat(session: Session, next: Next) {
if (!session.parsed) { if (!session.parsed) {
return next(); return next();
} }
const { content, prefix } = session.parsed; const { content, prefix } = session.parsed;
if (!prefix || content[0] !== 'r') return next(); if (!prefix) {
const expr = content.slice(1); return next();
if (rollRegexp.test(expr)) {
return session.execute({ name: 'roll', args: [expr] });
} }
if (rcRegexp.test(expr)) { const matchingCommand = this.checkCommands.find((pattern) =>
const matching = expr.match(rcRegexp); content.startsWith(pattern),
return session.execute({ );
name: 'rc', if (matchingCommand) {
args: [ const rest = content.slice(matchingCommand.length);
matching[1], return session.execute(`${matchingCommand} ${rest}`, next);
...(matching[2] ? [parseInt(matching[2].trim())] : []),
],
});
} }
return next(); return next();
} }
......
...@@ -90,33 +90,52 @@ export class DbModule extends BaseModule implements LifecycleEvents { ...@@ -90,33 +90,52 @@ export class DbModule extends BaseModule implements LifecycleEvents {
return isGlobal ? '频道' : '用户'; return isGlobal ? '频道' : '用户';
} }
@UseCommand('dice/st <skill:string> <value:integer>') @UseCommand('dice/st <exprs:text>')
@CommandDescription({ zh: '设置能力数值', en: 'Set skill value' }) @CommandDescription({ zh: '设置能力数值', en: 'Set skill value' })
@CommandExample('st 潜行 50') @CommandExample('st 潜行 50')
async onSetSkill( async onSetSkill(@PutSession() session: Session, @PutArg(0) expr: string) {
@PutSession() session: Session, const sessionInfo = {
@PutArg(0) skillName: string, userId: session.userId,
@PutArg(1) value: number, channelId: session.channelId || 'priv',
) { };
if (!skillName) { const exprChain = expr.split(/\s/);
return '请输入技能名称。'; const datas: Partial<DiceSkill>[] = [];
for (let i = 0; i < exprChain.length; i++) {
let skillName: string, value: number;
const fullMatch = exprChain[i].match(/^(\D+)(\d{1,3})$/);
if (fullMatch) {
skillName = fullMatch[1];
value = parseInt(fullMatch[2], 10);
} else {
const nextValue = exprChain[i + 1];
if (nextValue.match(/^\d{1,3}$/)) {
skillName = exprChain[i];
value = parseInt(nextValue, 10);
i++;
} else {
return '语法错误。';
}
}
if (value == null || value < 0 || value > 100) {
return '数值必须在 0 到 100 之间。';
}
datas.push({
...sessionInfo,
skillName: fullMatch[1],
value: parseInt(fullMatch[2]),
});
} }
if (value == null || value < 0 || value > 100) { if (!datas.length) {
return '数值必须在 0 到 100 之间。'; return;
} }
await this.database.upsert( await this.database.upsert('diceSkill', datas, [
'diceSkill', 'userId',
[ 'channelId',
{ 'skillName',
userId: session.userId, ]);
channelId: session.channelId || 'priv', return `已设置能力数值: ${datas
skillName, .map(({ skillName, value }) => `${skillName}=${value}`)
value, .join(' ')}`;
},
],
['userId', 'channelId', 'skillName'],
);
return `已设置 ${skillName} 的能力数值为 ${value}。`;
} }
async getSkillValue(session: Session, skillName: string) { async getSkillValue(session: Session, skillName: string) {
......
...@@ -21,11 +21,9 @@ import { BaseModule } from '../utility/base-module'; ...@@ -21,11 +21,9 @@ import { BaseModule } from '../utility/base-module';
@DiceModule() @DiceModule()
export class RollModule extends BaseModule { export class RollModule extends BaseModule {
private getDefaultFaces(user: User, channel: Channel) {}
@UseCommand('dice/roll [expr:string]', '掷骰') @UseCommand('dice/roll [expr:string]', '掷骰')
@CommandDescription({ en: 'Roll dice' }) @CommandDescription({ en: 'Roll dice' })
@CommandAlias('rd') @CommandAlias('r')
@CommandShortcut('掷骰', { fuzzy: true }) @CommandShortcut('掷骰', { fuzzy: true })
@CommandExample('roll 2d6+d10') @CommandExample('roll 2d6+d10')
onRoll( onRoll(
......
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