Skip to content

Commit

Permalink
feat(common): separate plugin entries
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 28, 2021
1 parent 58a290f commit 12346ab
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 42 deletions.
27 changes: 16 additions & 11 deletions packages/plugin-common/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ function onRepeat(options: RepeatHandler | StateCallback): StateCallback {
return ({ repeated, times, content }) => times >= minTimes && !repeated && Random.bool(probability) ? content : ''
}

export function repeater(ctx: Context, config: HandlerConfig) {
export interface RepeaterConfig {
onRepeat?: RepeatHandler | StateCallback
onInterrupt?: StateCallback
}

export function repeater(ctx: Context, config: RepeaterConfig = {}) {
ctx = ctx.group()

const states: Record<string, RepeatState> = {}
Expand Down Expand Up @@ -98,7 +103,13 @@ async function getHandlerResult(handler: RequestHandler, session: Session, prefe
}
}

export function verify(ctx: Context, config: HandlerConfig) {
export interface VerifierConfig {
onFriendRequest?: RequestHandler
onGroupMemberRequest?: RequestHandler
onGroupRequest?: RequestHandler
}

export function verifier(ctx: Context, config: VerifierConfig = {}) {
ctx.on('friend-request', async (session) => {
const result = await getHandlerResult(config.onFriendRequest, session, true)
if (result) return session.bot.handleFriendRequest(session.messageId, ...result)
Expand All @@ -115,15 +126,9 @@ export function verify(ctx: Context, config: HandlerConfig) {
})
}

export interface HandlerConfig {
onRepeat?: RepeatHandler | StateCallback
onInterrupt?: StateCallback
onFriendRequest?: RequestHandler
onGroupMemberRequest?: RequestHandler
onGroupRequest?: RequestHandler
}
export interface HandlerConfig extends RepeaterConfig, VerifierConfig {}

export default function apply(ctx: Context, config: HandlerConfig = {}) {
export default function apply(ctx: Context, config?: HandlerConfig) {
ctx.plugin(repeater, config)
ctx.plugin(verify, config)
ctx.plugin(verifier, config)
}
8 changes: 4 additions & 4 deletions packages/plugin-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Context } from 'koishi-core'
import { admin, AdminConfig } from './admin'
import basic, { BasicConfig } from './basic'
import handler, { HandlerConfig } from './handler'
import updater, { UpdaterConfig } from './updater'

export * from './admin'
export * from './basic'
export * from './handler'
export * from './updater'

export interface Config extends AdminConfig, HandlerConfig, BasicConfig {}
export interface Config extends HandlerConfig, BasicConfig, UpdaterConfig {}

export const name = 'common'

export function apply(ctx: Context, config: Config = {}) {
ctx.command('common', '基础功能')

ctx.plugin(admin, config)
ctx.plugin(basic, config)
ctx.plugin(handler, config)
ctx.plugin(updater, config)
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ function flagAction(map: FlagMap, { target, options }: FlagArgv, ...flags: strin
}

Command.prototype.adminUser = function (this: Command, callback, autoCreate) {
const { database } = this.app
this.config.checkUnknown = true

const command = this
.userFields(['authority'])
.option('target', '-t [user:user] 指定目标用户', { authority: 3 })
Expand All @@ -130,15 +131,15 @@ Command.prototype.adminUser = function (this: Command, callback, autoCreate) {
})

command.action(async (argv) => {
const { options, session, args } = argv
const fields = session.collect('user', argv)
let target: User.Observed<never>
const { options, args, session: { user, database } } = argv
const fields = argv.session.collect('user', argv)
let target: User.Observed, session = argv.session
if (!options.target) {
target = await session.observeUser(fields)
target = await argv.session.observeUser(fields)
} else {
const [platform, userId] = Argv.parsePid(options.target)
if (session.user[platform] === userId) {
target = await session.observeUser(fields)
if (user[platform] === userId) {
target = await argv.session.observeUser(fields)
} else {
const data = await database.getUser(platform, userId, [...fields])
if (!data) {
Expand All @@ -148,15 +149,22 @@ Command.prototype.adminUser = function (this: Command, callback, autoCreate) {
await database.createUser(platform, userId, fallback)
})
target = fallback
} else if (session.user.authority <= data.authority) {
} else if (user.authority <= data.authority) {
return template('internal.low-authority')
} else {
target = observe(data, diff => database.setUser(platform, userId, diff), `user ${options.target}`)
if (!autoCreate) {
session = Object.create(argv.session)
session.user = target
session.uid = options.target
session.userId = userId
session.platform = platform
}
}
}
}
const diffKeys = Object.keys(target._diff)
const result = await callback({ ...argv, target }, ...args)
const result = await callback({ ...argv, target, session }, ...args)
if (typeof result === 'string') return result
if (!difference(Object.keys(target._diff), diffKeys).length) {
return template('admin.user-unchanged')
Expand All @@ -169,17 +177,18 @@ Command.prototype.adminUser = function (this: Command, callback, autoCreate) {
}

Command.prototype.adminChannel = function (this: Command, callback, autoCreate) {
const { database } = this.app
this.config.checkUnknown = true

const command = this
.userFields(['authority'])
.option('target', '-t [channel:channel] 指定目标频道', { authority: 3 })

command.action(async (argv, ...args) => {
const { options, session } = argv
const fields = session.collect('channel', argv)
let target: Channel.Observed
if ((!options.target || options.target === session.cid) && session.subtype === 'group') {
target = await session.observeChannel(fields)
const { options, session: { cid, subtype, database } } = argv
const fields = argv.session.collect('channel', argv)
let target: Channel.Observed, session = argv.session
if ((!options.target || options.target === cid) && subtype === 'group') {
target = await argv.session.observeChannel(fields)
} else if (options.target) {
const [platform, channelId] = Argv.parsePid(options.target)
const data = await database.getChannel(platform, channelId, [...fields])
Expand All @@ -192,11 +201,18 @@ Command.prototype.adminChannel = function (this: Command, callback, autoCreate)
target = fallback
} else {
target = observe(data, diff => database.setChannel(platform, channelId, diff), `channel ${options.target}`)
if (!autoCreate) {
session = Object.create(argv.session)
session.channel = target
session.cid = options.target
session.channelId = channelId
session.platform = platform
}
}
} else {
return template('admin.not-in-group')
}
const result = await callback({ ...argv, target }, ...args)
const result = await callback({ ...argv, target, session }, ...args)
if (typeof result === 'string') return result
if (!Object.keys(target._diff).length) {
return template('admin.channel-unchanged')
Expand All @@ -208,18 +224,9 @@ Command.prototype.adminChannel = function (this: Command, callback, autoCreate)
return command
}

export interface AdminConfig {
admin?: boolean
generateToken?: () => string
}

export function admin(ctx: Context, config: AdminConfig = {}) {
if (config.admin === false) return
export function callme(ctx: Context) {
ctx = ctx.select('database')

ctx.command('common/user', '用户管理', { authority: 3 })
ctx.command('common/channel', '频道管理', { authority: 3 })

ctx.command('common/callme [name:text]', '修改自己的称呼')
.userFields(['id', 'name'])
.shortcut('叫我', { prefix: true, fuzzy: true })
Expand Down Expand Up @@ -255,6 +262,14 @@ export function admin(ctx: Context, config: AdminConfig = {}) {
}
}
})
}

export interface BindConfig {
generateToken?: () => string
}

export function bind(ctx: Context, config: BindConfig = {}) {
ctx = ctx.select('database')

// 1: group (1st step)
// 0: private
Expand All @@ -278,7 +293,7 @@ export function admin(ctx: Context, config: AdminConfig = {}) {
await user._update()
}

ctx.command('user/bind', '绑定到账号', { authority: 0 })
ctx.command('common/bind', '绑定到账号', { authority: 0 })
.action(({ session }) => {
const token = generate(session, +(session.subtype === 'group'))
return template('bind.generated-1', token)
Expand Down Expand Up @@ -307,6 +322,13 @@ export function admin(ctx: Context, config: AdminConfig = {}) {
}
}
}, true)
}

export function admin(ctx: Context) {
ctx = ctx.select('database')

ctx.command('common/user', '用户管理', { authority: 3 })
ctx.command('common/channel', '频道管理', { authority: 3 })

ctx.command('user/authorize <value:posint>', '权限信息', { authority: 4 })
.alias('auth')
Expand Down Expand Up @@ -434,3 +456,15 @@ export function admin(ctx: Context, config: AdminConfig = {}) {
.option('unset', '-S 删除标记', { authority: 4 })
.adminChannel(flagAction.bind(null, Channel.Flag))
}

export interface UpdaterConfig extends BindConfig {
admin?: boolean
bind?: boolean
callme?: boolean
}

export default function apply(ctx: Context, config: UpdaterConfig = {}) {
if (config.admin !== false) ctx.plugin(admin)
if (config.bind !== false) ctx.plugin(bind, config)
if (config.callme !== false) ctx.plugin(callme)
}

0 comments on commit 12346ab

Please sign in to comment.