Skip to content

Commit

Permalink
feat(plugin-common): refactor options
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 18, 2020
1 parent 59052bb commit c5c4283
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 51 deletions.
10 changes: 7 additions & 3 deletions packages/plugin-common/src/admin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Context, User, userFlags, UserFlag, Meta, UserField, getTargetId, CommandConfig, GroupField, UserData, GroupData, GroupFlag, groupFlags, Group, userFields, groupFields } from 'koishi-core'
import { isInteger, difference, Observed, paramCase } from 'koishi-utils'
import {
Context, Meta, getTargetId,
User, UserData, userFlags, UserFlag, userFields, UserField,
Group, GroupData, groupFlags, GroupFlag, groupFields, GroupField,
} from 'koishi-core'

type ActionCallback <T extends {}, K extends keyof T> =
(this: Context, meta: Meta, target: Observed<Pick<T, K>>, ...args: string[]) => Promise<any>
Expand Down Expand Up @@ -104,11 +108,11 @@ registerGroupAction('unsetFlag', async (meta, group, ...flags) => {
return meta.$send('群信息已修改。')
}, ['flag'])

export default function apply (ctx: Context, options: CommandConfig) {
export default function apply (ctx: Context) {
const userActions = Object.keys(userActionMap).map(paramCase).join(', ')
const groupActions = Object.keys(groupActionMap).map(paramCase).join(', ')

ctx.command('admin <action> [...args]', '管理用户', { authority: 4, ...options })
ctx.command('admin <action> [...args]', '管理用户', { authority: 4 })
.option('-u, --user [user]', '指定目标用户')
.option('-g, --group [group]', '指定目标群')
.option('-G, --this-group', '指定目标群为本群')
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-common/src/authorize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function updateAuthority (database: Database, users: AuthorizedUser
}
}

export interface AuthorizeConfig {
export interface AuthorizeOptions {
authorizeUser?: Record<number, number>
authorizeGroup?: Record<number, number | Partial<Record<GroupRole, number>>>
}
Expand All @@ -28,7 +28,7 @@ interface AuthorizeInfo {
update: Set<number>
}

export default function apply (ctx: Context, config: AuthorizeConfig) {
export default function apply (ctx: Context, config: AuthorizeOptions) {
const { app, database } = ctx
const { authorizeUser = {}, authorizeGroup = {} } = config
const authorityMap: Record<number, AuthorizeInfo> = []
Expand Down
9 changes: 5 additions & 4 deletions packages/plugin-common/src/broadcast.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Context, appMap, CommandConfig } from 'koishi-core'
import { Context, appMap } from 'koishi-core'
import { sleep } from 'koishi-utils'

export interface BroadcastOptions extends CommandConfig {
export interface BroadcastOptions {
broadcastInterval?: number
}

const defaultOptions: BroadcastOptions = {
authority: 4,
broadcastInterval: 1000,
}

export default function apply (ctx: Context, options: BroadcastOptions = {}) {
ctx.command('broadcast <message...>', '全服广播', { ...defaultOptions, ...options })
options = { ...defaultOptions, ...options }

ctx.command('broadcast <message...>', '全服广播', { authority: 4 })
.action(async (_, message: string) => {
const groups = await ctx.database.getAllGroups(['id', 'assignee'])
const assignMap: Record<number, number[]> = {}
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-common/src/contextify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, CommandConfig } from 'koishi-core'
import { Context } from 'koishi-core'

export default function apply (ctx: Context, config: CommandConfig = {}) {
ctx.command('contextify <message...>', '在特定上下文中触发指令', { authority: 3, ...config })
export default function apply (ctx: Context) {
ctx.command('contextify <message...>', '在特定上下文中触发指令', { authority: 3 })
.alias('ctxf')
.option('-u, --user [id]', '使用私聊上下文')
.option('-d, --discuss [id]', '使用讨论组上下文')
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-common/src/echo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MetaTypeMap, Context, CommandConfig } from 'koishi-core'
import { MetaTypeMap, Context } from 'koishi-core'

export default function (ctx: Context, options: CommandConfig) {
ctx.command('echo <message...>', '向多个上下文发送广播', { authority: 2, ...options })
export default function (ctx: Context) {
ctx.command('echo <message...>', '向多个上下文发送广播', { authority: 2 })
.option('-u, --user <id>', '指定信息发送的目标 QQ 号', { isString: true, authority: 4 })
.option('-g, --group <id>', '指定信息发送的目标群号', { isString: true, authority: 4 })
.option('-d, --discuss <id>', '指定信息发送的目标讨论组号', { isString: true, authority: 4 })
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-common/src/exit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Context, CommandConfig } from 'koishi-core'
import { Context } from 'koishi-core'

const CODE_STOP = 0
const CODE_RESTART = 1

export default function apply (ctx: Context, options: CommandConfig) {
ctx.command('exit', '停止机器人运行', { authority: 4, ...options })
export default function apply (ctx: Context) {
ctx.command('exit', '停止机器人运行', { authority: 4 })
.option('-c, --code [code]', '设置 exit code', { default: 0 })
.shortcut('关机', { prefix: true, options: { code: CODE_STOP } })
.shortcut('重启', { prefix: true, options: { code: CODE_RESTART } })
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-common/src/help.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, Command, UserData, CommandConfig, MessageMeta } from 'koishi-core'
import { Context, Command, UserData, MessageMeta } from 'koishi-core'

export default function apply (ctx: Context, options: CommandConfig) {
ctx.command('help [command]', '显示帮助信息', { authority: 0, ...options })
export default function apply (ctx: Context) {
ctx.command('help [command]', '显示帮助信息', { authority: 0 })
.userFields(['authority', 'usage'])
.shortcut('帮助', { fuzzy: true })
.shortcut('全局指令', { options: { shortcut: true } })
Expand Down
54 changes: 27 additions & 27 deletions packages/plugin-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Context, CommandConfig } from 'koishi-core'
import { Context } from 'koishi-core'
import admin from './admin'
import authorize, { AuthorizeConfig } from './authorize'
import authorize, { AuthorizeOptions } from './authorize'
import broadcast, { BroadcastOptions } from './broadcast'
import contextify from './contextify'
import echo from './echo'
import exit from './exit'
import help from './help'
import info, { InfoOptions } from './info'
import repeater, { RepeaterOptions } from './repeater'
import requestHandler, { HandlerConfig } from './request-handler'
import requestHandler, { HandlerOptions } from './request-handler'
import respondent, { Respondent } from './respondent'
import welcome, { WelcomeMessage } from './welcome'

Expand All @@ -30,37 +30,37 @@ export {
welcome,
}

interface CommonPluginConfig extends HandlerConfig, AuthorizeConfig, InfoOptions {
admin?: false | CommandConfig
broadcast?: false | BroadcastOptions
contextify?: false | CommandConfig
echo?: false | CommandConfig
exit?: false | CommandConfig
help?: false | CommandConfig
info?: false | InfoOptions
repeater?: false | RepeaterOptions
interface CommonPluginConfig extends AuthorizeOptions, BroadcastOptions, HandlerOptions, InfoOptions {
admin?: boolean
broadcast?: boolean
contextify?: boolean
echo?: boolean
exit?: boolean
help?: boolean
info?: boolean
repeater?: RepeaterOptions
respondent?: Respondent[]
welcome?: false | WelcomeMessage
welcomeMessage?: WelcomeMessage
}

export const name = 'common'

export function apply (ctx: Context, options: CommonPluginConfig = {}) {
ctx
.plugin(contextify, options.contextify)
.plugin(echo, options.echo)
.plugin(exit, options.exit)
.plugin(help, options.help)
.plugin(repeater, options.repeater)
.plugin(requestHandler, options)
.plugin(respondent, options.respondent)
.plugin(welcome, options.welcome)
ctx.plugin(requestHandler, options)
ctx.plugin(respondent, options.respondent)
ctx.plugin(welcome, options.welcomeMessage)

if (options.contextify !== false) ctx.plugin(contextify)
if (options.echo !== false) ctx.plugin(echo)
if (options.exit !== false) ctx.plugin(exit)
if (options.help !== false) ctx.plugin(help)
if (options.repeater) ctx.plugin(repeater, options.repeater)

if (ctx.database) {
ctx
.plugin(admin, options.admin)
.plugin(authorize, options)
.plugin(broadcast, options.broadcast)
.plugin(info, options)
ctx.plugin(authorize, options)

if (options.admin !== false) ctx.plugin(admin)
if (options.broadcast !== false) ctx.plugin(broadcast, options)
if (options.info !== false) ctx.plugin(info, options)
}
}
1 change: 1 addition & 0 deletions packages/plugin-common/src/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const defaultConfig: InfoOptions = {

export default function apply (ctx: Context, config: InfoOptions = {}) {
config = { ...defaultConfig, ...config }

ctx.command('info', '查看用户信息', { authority: 0 })
.alias('i')
.shortcut('我的信息')
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-common/src/request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { App, Meta } from 'koishi-core'

type RequestHandler = string | boolean | ((meta: Meta<'request'>, app: App) => string | boolean | void | Promise<string | boolean | void>)

export interface HandlerConfig {
export interface HandlerOptions {
handleFriend?: RequestHandler
handleGroupAdd?: RequestHandler
handleGroupInvite?: RequestHandler
}

const defaultHandlers: HandlerConfig = {
const defaultHandlers: HandlerOptions = {
async handleFriend (meta, app) {
if (!app.database) return
const user = await app.database.getUser(meta.userId, ['authority'])
Expand Down Expand Up @@ -41,7 +41,7 @@ function setGroupResult (meta: Meta, result: string | boolean | void) {
}
}

export default function apply (ctx: App, options: HandlerConfig = {}) {
export default function apply (ctx: App, options: HandlerOptions = {}) {
const { handleFriend, handleGroupAdd, handleGroupInvite } = { ...defaultHandlers, ...options }

ctx.receiver.on('request/friend', async (meta) => {
Expand Down

0 comments on commit c5c4283

Please sign in to comment.