Skip to content

事件系统 ​

支持的入口 ​

入口触发时机
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:交互 ID
  • ctx.respond(code):ACK

不要使用 ​

js
ctx.on(...)       // 内置插件 API,用户插件没有
register(ctx)     // 内置插件 API
onButtonClick()   // 请用 onInteraction

短路 ​

只有确定这条消息完全由你处理时才返回 true。

返回 true 会阻止:

  • 其他用户插件
  • 内置插件
  • 关键词
  • 工作流
  • AI 聊天

薇薇官机平台 · ZL 系列插件