Commit 09456eec authored by nanahira's avatar nanahira

improve shortcut grammer and .st

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