Skip to content

Commit

Permalink
feat(core): add date & posint domain
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 7, 2021
1 parent e2bc856 commit c4d84b3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
2 changes: 2 additions & 0 deletions packages/koishi-core/src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ template.set('internal', {
// parser
'invalid-number': '请提供一个数字。',
'invalid-integer': '请提供一个整数。',
'invalid-posint': '请提供一个正整数。',
'invalid-date': '请输入合法的时间。',
'invalid-user': '请指定正确的用户。',
'invalid-channel': '请指定正确的频道。',

Expand Down
18 changes: 16 additions & 2 deletions packages/koishi-core/src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { camelCase, segment, escapeRegExp, paramCase, template } from 'koishi-utils'
import { camelCase, segment, escapeRegExp, paramCase, template, Time } from 'koishi-utils'
import { format } from 'util'
import { Platform } from './adapter'
import { Command } from './command'
Expand All @@ -15,6 +15,8 @@ export interface Domain {
user: string
channel: string
integer: number
posint: number
date: Date
}

export namespace Domain {
Expand Down Expand Up @@ -87,7 +89,19 @@ export namespace Domain {
create('integer', (source) => {
const value = +source
if (value * 0 === 0 && Math.floor(value) === value) return value
throw new Error(template('interval.invalid-integer'))
throw new Error(template('internal.invalid-integer'))
})

create('posint', (source) => {
const value = +source
if (value * 0 === 0 && Math.floor(value) === value && value > 0) return value
throw new Error(template('internal.invalid-posint'))
})

create('date', (source) => {
const timestamp = Time.parseDate(source)
if (+timestamp) return timestamp
throw new Error(template('internal.invalid-date'))
})

create('user', (source, session) => {
Expand Down
28 changes: 11 additions & 17 deletions packages/plugin-common/src/admin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isInteger, difference, observe, Time, enumKeys, Random, template, deduplicate } from 'koishi-utils'
import { difference, observe, Time, enumKeys, Random, template, deduplicate } from 'koishi-utils'
import { Context, User, Channel, Command, Argv, Platform, Session } from 'koishi-core'

type AdminAction<U extends User.Field, G extends Channel.Field, A extends any[], O extends {}, T>
Expand Down Expand Up @@ -61,7 +61,7 @@ template.set('bind', {
})

template.set('usage', {
'one': '今日 {0} 功能的调用次数为:{1}',
'present': '今日 {0} 功能的调用次数为:{1}',
'list': '今日各功能的调用次数为:',
'none': '今日没有调用过消耗次数的功能。',
})
Expand Down Expand Up @@ -288,11 +288,9 @@ export default function apply(ctx: Context, config: AdminConfig = {}) {
}
}, true)

ctx.command('user/authorize <value>', '权限信息', { authority: 4 })
ctx.command('user/authorize <value:posint>', '权限信息', { authority: 4 })
.alias('auth')
.adminUser(async ({ session, target }, value) => {
const authority = Number(value)
if (!isInteger(authority) || authority < 0) return '参数错误。'
.adminUser(async ({ session, target }, authority) => {
if (authority >= session.user.authority) return template('internal.low-authority')
if (authority === target.authority) return template('admin.user-unchanged')
await ctx.database.createUser(session.platform, target[session.platform], { authority })
Expand All @@ -307,25 +305,23 @@ export default function apply(ctx: Context, config: AdminConfig = {}) {
.option('unset', '-S 删除标记', { authority: 4 })
.adminUser(flagAction.bind(null, User.Flag))

ctx.command('user.usage [key] [value]', '调用次数信息', { authority: 1 })
ctx.command('user.usage [key] [value:posint]', '调用次数信息', { authority: 1 })
.userFields(['usage'])
.option('set', '-s 设置调用次数', { authority: 4 })
.option('clear', '-c 清空调用次数', { authority: 4 })
.adminUser(({ target, options }, name, value) => {
.adminUser(({ target, options }, name, count) => {
if (options.clear) {
name ? delete target.usage[name] : target.usage = {}
return
}

if (options.set) {
if (value === undefined) return template('internal.insufficient-arguments')
const count = +value
if (!isInteger(count) || count < 0) return '参数错误。'
if (!count) return template('internal.insufficient-arguments')
target.usage[name] = count
return
}

if (name) return template('usage.one', name, target.usage[name] || 0)
if (name) return template('usage.present', name, target.usage[name] || 0)
const output: string[] = []
for (const name of Object.keys(target.usage).sort()) {
if (name.startsWith('$')) continue
Expand All @@ -336,7 +332,7 @@ export default function apply(ctx: Context, config: AdminConfig = {}) {
return output.join('\n')
})

ctx.command('user.timer [key] [value]', '定时器信息', { authority: 1 })
ctx.command('user.timer [key] [value:date]', '定时器信息', { authority: 1 })
.userFields(['timers'])
.option('set', '-s 设置定时器', { authority: 4 })
.option('clear', '-c 清空定时器', { authority: 4 })
Expand All @@ -347,10 +343,8 @@ export default function apply(ctx: Context, config: AdminConfig = {}) {
}

if (options.set) {
if (value === undefined) return template('internal.insufficient-arguments')
const timestamp = +Time.parseDate(value)
if (!timestamp) return '请输入合法的时间。'
target.timers[name] = timestamp
if (!value) return template('internal.insufficient-arguments')
target.timers[name] = +value
return
}

Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-common/tests/admin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Admin Commands', () => {
await session.shouldReply('authorize -t nan', '选项 target 输入无效,请指定正确的用户。')
await session.shouldReply('authorize -t @321', '未找到指定的用户。')
await session.shouldReply('authorize -t @789', '权限不足。')
await session.shouldReply('authorize -t @456 -1', '参数错误。')
await session.shouldReply('authorize -t @456 1.5', '参数 value 输入无效,请提供一个正整数。')
await session.shouldReply('authorize -t @456 3', '用户数据未改动。')
await session.shouldReply('authorize -t @456 4', '权限不足。')
await session.shouldReply('authorize -t @456 2', '用户数据已修改。')
Expand All @@ -64,7 +64,7 @@ describe('Admin Commands', () => {
await session.shouldReply('user.usage -c foo', '用户数据已修改。')
await session.shouldReply('user.usage', '今日没有调用过消耗次数的功能。')
await session.shouldReply('user.usage -s bar', '缺少参数,输入帮助以查看用法。')
await session.shouldReply('user.usage -s bar nan', '参数错误。')
await session.shouldReply('user.usage -s bar nan', '参数 value 输入无效,请提供一个正整数。')
await session.shouldReply('user.usage -s bar 2', '用户数据已修改。')
await session.shouldReply('user.usage bar', '今日 bar 功能的调用次数为:2')
await session.shouldReply('user.usage baz', '今日 baz 功能的调用次数为:0')
Expand All @@ -79,7 +79,7 @@ describe('Admin Commands', () => {
await session.shouldReply('user.timer -c bar', '用户数据已修改。')
await session.shouldReply('user.timer', '当前没有生效的定时器。')
await session.shouldReply('user.timer -s foo', '缺少参数,输入帮助以查看用法。')
await session.shouldReply('user.timer -s foo nan', '请输入合法的时间。')
await session.shouldReply('user.timer -s foo nan', '参数 value 输入无效,请输入合法的时间。')
await session.shouldReply('user.timer -s foo 2min', '用户数据已修改。')
await session.shouldReply('user.timer foo', '定时器 foo 的生效时间为:剩余 2 分钟')
await session.shouldReply('user.timer fox', '定时器 fox 当前并未生效。')
Expand Down

0 comments on commit c4d84b3

Please sign in to comment.