Commit c5e90a3a authored by nanahira's avatar nanahira

add .set

parent c155b2df
import { Context } from 'koishi'; import { Context } from 'koishi';
import DataviewPlugin from '@koishijs/plugin-dataview';
export default class ExtrasInDev { export default class ExtrasInDev {
constructor(ctx: Context) {} constructor(ctx: Context) {
ctx.plugin(DataviewPlugin);
}
} }
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
"@koishijs/plugin-cache-lru": "^1.0.0-rc.0", "@koishijs/plugin-cache-lru": "^1.0.0-rc.0",
"@koishijs/plugin-console": "^3.1.1", "@koishijs/plugin-console": "^3.1.1",
"@koishijs/plugin-database-memory": "^1.0.2", "@koishijs/plugin-database-memory": "^1.0.2",
"@koishijs/plugin-dataview": "^1.2.1",
"@koishijs/plugin-sandbox": "^1.0.0", "@koishijs/plugin-sandbox": "^1.0.0",
"@types/jest": "^27.4.0", "@types/jest": "^27.4.0",
"@types/node": "^17.0.4", "@types/node": "^17.0.4",
...@@ -1139,6 +1140,16 @@ ...@@ -1139,6 +1140,16 @@
"koishi": "^4.2.2" "koishi": "^4.2.2"
} }
}, },
"node_modules/@koishijs/plugin-dataview": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-dataview/-/plugin-dataview-1.2.1.tgz",
"integrity": "sha512-Ox4n7YoNmDm+EIpSVj9c7Y/FSnKgokw8qQaPgfIGUWiRmiznelq1b016VPdM47eixTkVzMKDUehZGWLwMbKk7w==",
"dev": true,
"peerDependencies": {
"@koishijs/plugin-console": "^3.1.0",
"koishi": "^4.2.2"
}
},
"node_modules/@koishijs/plugin-sandbox": { "node_modules/@koishijs/plugin-sandbox": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-sandbox/-/plugin-sandbox-1.0.0.tgz", "resolved": "https://registry.npmjs.org/@koishijs/plugin-sandbox/-/plugin-sandbox-1.0.0.tgz",
...@@ -8471,6 +8482,13 @@ ...@@ -8471,6 +8482,13 @@
"@koishijs/orm-utils": "^1.0.0" "@koishijs/orm-utils": "^1.0.0"
} }
}, },
"@koishijs/plugin-dataview": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-dataview/-/plugin-dataview-1.2.1.tgz",
"integrity": "sha512-Ox4n7YoNmDm+EIpSVj9c7Y/FSnKgokw8qQaPgfIGUWiRmiznelq1b016VPdM47eixTkVzMKDUehZGWLwMbKk7w==",
"dev": true,
"requires": {}
},
"@koishijs/plugin-sandbox": { "@koishijs/plugin-sandbox": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-sandbox/-/plugin-sandbox-1.0.0.tgz", "resolved": "https://registry.npmjs.org/@koishijs/plugin-sandbox/-/plugin-sandbox-1.0.0.tgz",
......
import { import {
CommandAlias,
CommandExample, CommandExample,
CommandUsage, CommandUsage,
Inject, Inject,
InjectLogger, InjectLogger,
OnApply, OnApply,
OnChannel,
Provide, Provide,
PutArg,
PutChannel, PutChannel,
PutOption, PutOption,
PutUser,
UseCommand, UseCommand,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { Channel, Database, Logger, Model } from 'koishi'; import { Channel, Database, Logger, Model, User } from 'koishi';
import { RcRuleList } from '../utility/rc-rules'; import { RcRuleList } from '../utility/rc-rules';
import { DiceModule } from '../utility/utility'; import {
DiceModule,
getDefaultRollFaces,
getRcMode,
PutIsGlobal,
} from '../utility/utility';
import { BaseModule } from '../utility/base-module'; import { BaseModule } from '../utility/base-module';
import { getProfileModel } from '../utility/dice-profile';
declare module 'koishi' { declare module 'koishi' {
interface Channel { interface Channel {
diceRcMode: number; diceRcMode: number;
diceDefaultRollFaces: number;
}
interface User {
diceRcMode: number;
diceDefaultRollFaces: number;
} }
} }
...@@ -34,13 +48,16 @@ export class DbModule extends BaseModule implements OnApply { ...@@ -34,13 +48,16 @@ export class DbModule extends BaseModule implements OnApply {
private logger: Logger; private logger: Logger;
onApply() { onApply() {
this.model.extend('channel', { const profileModel = getProfileModel();
diceRcMode: { type: 'integer', initial: 0 }, this.model.extend('channel', profileModel);
}); this.model.extend('user', profileModel);
this.logger.info(`Dice database module loaded.`); this.logger.info(`Dice database module loaded.`);
} }
@OnChannel() private targetPattern(isGlobal: boolean) {
return isGlobal ? '频道' : '用户';
}
@UseCommand('dice/rcmode', '设置检点规则') @UseCommand('dice/rcmode', '设置检点规则')
@CommandUsage( @CommandUsage(
`默认规则为0,规则序号如下:\n\n${RcRuleList.map( `默认规则为0,规则序号如下:\n\n${RcRuleList.map(
...@@ -48,19 +65,58 @@ export class DbModule extends BaseModule implements OnApply { ...@@ -48,19 +65,58 @@ export class DbModule extends BaseModule implements OnApply {
).join('\n')}`, ).join('\n')}`,
) )
@CommandExample('rcmode -s 1 设置当前频道的检点规则为1。') @CommandExample('rcmode -s 1 设置当前频道的检点规则为1。')
fetchRcMode( setRcMode(
@PutUser(['diceRcMode']) user: User,
@PutChannel(['diceRcMode']) channel: Channel, @PutChannel(['diceRcMode']) channel: Channel,
@PutOption('set', '-s <rule:integer> 设置规则') setRule: number, @PutOption('set', '-s <rule:integer> 设置规则') setRule: number,
@PutIsGlobal() isGlobal: boolean,
) { ) {
if (isGlobal && !channel) {
return '请在频道中使用该功能。';
}
if (setRule == null) { if (setRule == null) {
return `当前频道的检点规则如下:\n\n${ return `当前${this.targetPattern(isGlobal)}的检点规则如下:\n\n${
RcRuleList[channel.diceRcMode].text RcRuleList[getRcMode(isGlobal ? undefined : user, channel)].text
}`; }`;
} }
if (setRule < 0 || setRule >= RcRuleList.length) { if (setRule < 0 || setRule >= RcRuleList.length) {
return '规则序号不合法'; return '规则序号不合法';
} }
channel.diceRcMode = setRule; (isGlobal ? channel : user).diceRcMode = setRule;
return `已设置当前频道的检点规则为${setRule}。`; return `已设置当前${this.targetPattern(
isGlobal,
)}的检点规则为 ${setRule} 。`;
}
@UseCommand('dice/faces [faces:number]', '设置默认面数')
@CommandAlias('set')
@CommandUsage('默认面数为 6 。')
@CommandExample('faces -s 10 设置当前频道的默认面数为10。')
setFaces(
@PutUser(['diceDefaultRollFaces']) user: User,
@PutChannel(['diceDefaultRollFaces']) channel: Channel,
@PutArg(0) setFaceInArg: number,
@PutOption('set', '-s <rule:integer> 设置面数') setFaceInOpt: number,
@PutIsGlobal() isGlobal: boolean,
) {
if (isGlobal && !channel) {
return '请在频道中使用该功能。';
}
const setFace = setFaceInOpt || setFaceInArg;
if (setFace == null) {
return `当前${this.targetPattern(
isGlobal,
)}的默认掷骰面数为 ${getDefaultRollFaces(
isGlobal ? undefined : user,
channel,
)} 。`;
}
if (setFace < 2 || setFace > this.config.maxPoint) {
return `面数必须在 2 到 ${this.config.maxPoint} 之间。`;
}
(isGlobal ? channel : user).diceDefaultRollFaces = setFace;
return `已设置当前${this.targetPattern(
isGlobal,
)}的默认面数为 ${setFace} 。`;
} }
} }
...@@ -4,18 +4,19 @@ import { ...@@ -4,18 +4,19 @@ import {
CommandShortcut, CommandShortcut,
PutArg, PutArg,
PutChannel, PutChannel,
PutUser,
PutUserName, PutUserName,
UseCommand, UseCommand,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { Channel, Random } from 'koishi'; import { Channel, Random, User } from 'koishi';
import { DiceModule } from '../utility/utility'; import { DiceModule, getRcMode } from '../utility/utility';
import { BaseModule } from '../utility/base-module'; import { BaseModule } from '../utility/base-module';
import { RcResult, RcRuleList } from '../utility/rc-rules'; import { RcResult, RcRuleList } from '../utility/rc-rules';
@DiceModule() @DiceModule()
export class RcModule extends BaseModule { export class RcModule extends BaseModule {
private getRcRule(channel: Channel) { private getRcRule(user: User, channel: Channel) {
const index = channel?.diceRcMode || 0; const index = getRcMode(user, channel);
return RcRuleList[index]; return RcRuleList[index];
} }
...@@ -27,12 +28,13 @@ export class RcModule extends BaseModule { ...@@ -27,12 +28,13 @@ export class RcModule extends BaseModule {
@PutUserName(true) username: string, @PutUserName(true) username: string,
@PutArg(0) rate: number, @PutArg(0) rate: number,
@PutArg(1) reason: string, @PutArg(1) reason: string,
@PutUser(['diceRcMode']) user: User,
@PutChannel(['diceRcMode']) channel: Channel, @PutChannel(['diceRcMode']) channel: Channel,
) { ) {
if (!rate || rate < 0 || rate > 100) { if (!rate || rate < 0 || rate > 100) {
return '成功率必须在 0 到 100 之间。'; return '成功率必须在 0 到 100 之间。';
} }
const rule = this.getRcRule(channel); const rule = this.getRcRule(user, channel);
const value = Random.int(1, 101); const value = Random.int(1, 101);
const result = rule.check(rate, value); const result = rule.check(rate, value);
const resultText = const resultText =
......
import { import {
CommandAlias,
CommandExample, CommandExample,
CommandShortcut, CommandShortcut,
PutArg, PutArg,
PutChannel,
PutUser,
PutUserName, PutUserName,
UseCommand, UseCommand,
} from 'koishi-thirdeye'; } from 'koishi-thirdeye';
import { Random } from 'koishi'; import { Channel, Random, User } from 'koishi';
import { rollRegexp } from '../utility/constant'; import { rollRegexp } from '../utility/constant';
import { DiceModule } from '../utility/utility'; import { DiceModule, getDefaultRollFaces } from '../utility/utility';
import { BaseModule } from '../utility/base-module'; 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]', '掷骰')
@CommandAlias('rd')
@CommandShortcut('掷骰', { fuzzy: true }) @CommandShortcut('掷骰', { fuzzy: true })
@CommandExample('roll 2d6+d10') @CommandExample('roll 2d6+d10')
onRoll(@PutUserName(true) username: string, @PutArg(0) message = '1d6') { onRoll(
@PutUserName(true) username: string,
@PutArg(0) message: string,
@PutUser(['diceDefaultRollFaces']) user: User,
@PutChannel(['diceDefaultRollFaces']) channel: Channel,
) {
if (!message) {
message = `1d${getDefaultRollFaces(user, channel)}`;
}
if (!rollRegexp.test(message)) return '表达式语法错误。'; if (!rollRegexp.test(message)) return '表达式语法错误。';
const { maxPoint = 1 << 16, maxTimes = 64 } = this.config; const { maxPoint = 1 << 16, maxTimes = 64 } = this.config;
......
import { Model } from 'koishi';
export interface DiceProfile {
diceRcMode: number;
diceDefaultRollFaces: number;
}
export const DiceProfileFields: (keyof DiceProfile)[] = [
'diceRcMode',
'diceDefaultRollFaces',
];
export function getProfileModel(): Model.Field.Extension<DiceProfile> {
return {
diceRcMode: { type: 'integer' },
diceDefaultRollFaces: { type: 'integer' },
};
}
import { DefinePlugin } from 'koishi-thirdeye'; import { DefinePlugin, PutChannel, PutOption, PutUser } from 'koishi-thirdeye';
import { DicePluginConfig } from '../config'; import { DicePluginConfig } from '../config';
import { Channel, User } from 'koishi';
import { DiceProfile, DiceProfileFields } from './dice-profile';
export const DiceModule = () => DefinePlugin({ schema: DicePluginConfig }); export const DiceModule = () => DefinePlugin({ schema: DicePluginConfig });
export function getProfileProperty<K extends keyof DiceProfile>(
user: User,
channel: Channel,
property: K,
defaultValue: DiceProfile[K],
) {
if (user && user[property] != null) {
return user[property];
}
if (channel && channel[property] != null) {
return channel[property];
}
return defaultValue;
}
export const getRcMode = (user: User, channel: Channel) =>
getProfileProperty(user, channel, 'diceRcMode', 0);
export const getDefaultRollFaces = (user: User, channel: Channel) =>
getProfileProperty(user, channel, 'diceDefaultRollFaces', 6);
export const PutUserProfile = () => PutUser(DiceProfileFields);
export const PutChannelProfile = () => PutChannel(DiceProfileFields);
export const PutIsGlobal = () => PutOption('global', '-g 作用于本频道。');
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