Commit c5e90a3a authored by nanahira's avatar nanahira

add .set

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