You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord自动封禁新成员Bot无响应问题排查求助

Discord Bot 不触发guildMemberAdd事件、无法自动封禁新成员的解决方案

问题根源

你的Bot完全没响应成员加入操作,核心原因有两个:

  • 缺少GuildMembers网关意图,discord.js v14中必须显式声明该意图才能接收成员加入事件
  • 未在Discord开发者后台开启Server Members Intent特权网关意图

修复步骤

1. 补充必要的网关意图

修改Client初始化代码,添加监听成员事件和私信所需的意图:

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMembers, // 必须添加才能触发guildMemberAdd事件
    GatewayIntentBits.DirectMessages // 用于给成员发送私信通知
  ]
});

2. 开启特权网关意图

登录Discord开发者平台,进入你的Bot应用后台:

  • 切换到Bot选项卡
  • 找到Privileged Gateway Intents区域
  • 开启Server Members Intent选项
  • 保存更改

3. 完善代码的异步处理与错误捕获

原代码中member.ban()是异步操作,未加await可能导致执行异常;同时建议先发私信再封禁(封禁后无法再给用户发消息),并统一捕获错误:

client.on('guildMemberAdd', async member => { // 改为async函数支持await
  console.log(`${member.user.username} 加入了服务器。`);
  const currentTime = Date.now();
  const creationTime = member.user.createdAt.getTime();
  const ageInDays = (currentTime - creationTime) / 1000 / 60 / 60 / 24;

  if (ageInDays < 30) {
    console.log(`${member.user.username} 账号未满30天,执行封禁并发送通知...`);
    try {
      await member.send('抱歉,你的账号创建时间不足30天,无法加入本服务器。');
      await member.ban({ reason: '账号创建时间不足30天' });
      console.log(`已封禁 ${member.user.username} 并发送通知。`);
    } catch (error) {
      console.error(`处理 ${member.user.username} 时出错: ${error.message}`);
    }
  }    
});

client.login('你的Bot令牌')
  .then(() => console.log('Bot已登录。'))
  .catch(error => console.error(`登录失败: ${error}`));

验证方法

重启Bot后,用测试账号再次加入服务器,此时控制台会输出成员加入日志,符合条件的账号会被自动封禁并收到私信通知。

内容的提问来源于stack exchange,提问作者Klepacs Adam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 11:46:02