事件系统
支持的入口
| 入口 | 触发时机 |
|---|---|
| onMessage | 群消息、@机器人、私聊 |
| onGroupMemberAdd | 新成员入群 |
| onGroupJoinRequest | 收到入群申请 |
| onInteraction | 用户点击按钮回调 |
| onLoad | 插件首次运行前 |
onMessage
js
module.exports = {
async onMessage(ctx) {
if (ctx.surface === 'group' && ctx.content === '帮助') {
await ctx.reply('群聊帮助');
return true;
}
if (ctx.surface === 'c2c' && ctx.content === '帮助') {
await ctx.reply('私聊帮助');
return true;
}
return false;
},
};onGroupMemberAdd
js
async onGroupMemberAdd(ctx) {
ctx.log('新成员', ctx.userId, '加入', ctx.groupId);
await ctx.sendGroup(ctx.groupId, '欢迎新成员');
return false;
}返回 false 表示继续交给入群审核等内置流程。
onGroupJoinRequest
js
async onGroupJoinRequest(ctx) {
const list = await ctx.getJoinRequests();
ctx.log('入群申请数', list.list?.length ?? 0);
return false;
}onInteraction
按钮回调:
js
async onInteraction(ctx) {
if (ctx.buttonData === '签到') {
await ctx.reply('签到成功');
await ctx.respond(0);
return true;
}
return false;
}ctx.buttonData:按钮数据ctx.interactionId:交互 IDctx.respond(code):ACK
不要使用
js
ctx.on(...) // 内置插件 API,用户插件没有
register(ctx) // 内置插件 API
onButtonClick() // 请用 onInteraction短路
只有确定这条消息完全由你处理时才返回 true。
返回 true 会阻止:
- 其他用户插件
- 内置插件
- 关键词
- 工作流
- AI 聊天
