Skip to content

BotCommandSystem ​

通过 bot.command 访问。命令系统会自动监听群聊和私聊消息。

ts
bot.command.command('ping', async (ctx) => {
    await ctx.reply(Message.text('pong'))
})

默认前缀为 #,可通过 BotConfig.commandPrefix 修改:

ts
const bot = new BotClient({
    // ...
    commandPrefix: '!',
    // 或配置多个前缀
    // commandPrefix: ['#', '/'],
})

也可以在运行时直接配置命令系统:

ts
bot.command.configure({ prefix: '!', cooldownReply: true })
bot.command.setPrefix(['#', '/'])
console.log(bot.command.getPrefixes())

BotConfig.commandPrefix 只是创建时的快捷方式,最终仍由 bot.command 管理。

方法 ​

configure(options) ​

ts
bot.command.configure({ prefix: ['#', '/'], cooldownReply: true })

运行时修改前缀和冷却提示。

setPrefix(prefix) ​

ts
bot.command.setPrefix('!')
bot.command.setPrefix(['#', '/'])

设置命令前缀,空值回退为 #。

getPrefixes() ​

ts
const prefixes = bot.command.getPrefixes()

获取当前前缀列表。

setCooldownReply(enabled) ​

ts
bot.command.setCooldownReply(true)

设置冷却时是否回复提示。

command(names, handler, options?) ​

ts
command(names: string | string[], handler: (ctx: CommandContext) => unknown, options?: CommandOptions): this

注册命令,names 第一个为主命令,其余为别名。

选项说明
description命令描述
permissioneveryone / owner / admin,默认 everyone
cooldown冷却时间(ms)
scopeall / group / private
ts
bot.command.command(['ban', 'b'], handler, {
    permission: 'admin',
    cooldown: 3000,
    scope: 'group',
})

use(middleware) ​

注册全局中间件,按顺序执行。中间件不调用 next() 即可拦截。

ts
bot.command.use(async (ctx, next) => {
    if (!ctx.isOwner) return
    await next()
})

remove(name) ​

ts
bot.command.remove('ping')

移除命令及其别名。

list() ​

ts
const commands = bot.command.list()
for (const cmd of commands) {
    console.log(cmd.name, cmd.permission, cmd.description)
}

返回已注册命令列表。

attach() ​

ts
bot.command.attach()

开始监听消息事件,bot.start() 会自动调用。

detach() ​

ts
bot.command.detach()

停止监听消息事件,bot.stop() 会自动调用。

CommandContext ​

继承 MessageContext,额外提供:

属性说明
command命中的主命令名
args参数数组
argText参数字符串
rawText原始文本

基于 MIT 许可发布