koishi-plugin-message-recall
v0.1.0
Published
Reusable message recall helper for Koishi plugins
Readme
koishi-plugin-message-recall
共享的 Koishi 消息撤回工具,提供统一的撤回逻辑 / 配置,并且可以在多个插件之间复用。
特性
- 控制台统一配置:默认开关、延迟、每个 action 的独立设置。
- 提供
ctx.recall服务,支持:wrap(actionKey, handler):一行包裹指令,自动接管session.send与返回值。send(session, actionKey, fragment):直接发送并安排撤回。withSession(session, actionKey, callback):在任意异步流程中临时接管session.send。
- 日志跟踪:可在 info / debug 两种级别查看发送、调度与撤回情况。
安装
npm i koishi-plugin-message-recallkoishi.yml 中启用插件即可在控制台看到配置入口。
基本用法
import { Context } from 'koishi'
export function apply(ctx: Context) {
ctx.command('foo').action(
ctx.recall.wrap('foo', async ({ session }) => {
// 普通返回值会被自动发送并加入撤回队列
return '这条消息会在默认延迟后撤回'
}),
)
}如果希望手动控制发送:
ctx.command('bar').action(async ({ session }) => {
await ctx.recall.withSession(session, 'bar', async (send) => {
await send('第一条')
await send('第二条')
})
})或直接调用 ctx.recall.send(session, 'baz', fragment)。
配置项
| 选项 | 说明 |
| --- | --- |
| enabled | 全局开关 |
| delay | 撤回延迟(秒) |
| autoReply | wrap 时是否自动发送返回值 |
| logLevel | info / debug |
| actions | 通过 action key 定制 enabled / delay / autoReply |
约定
actionKey由业务插件自定义,例如指令名、场景名等,只要保证唯一即可。- 插件会在
ctx.recall.wrap()中临时覆盖session.send,执行完毕后恢复。 - 如果动作被禁用,
wrap会直接退化为原 handler,不做任何注入。
开源协议
MIT © magisk317
