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

DiscordJS v14 guildMemberAdd事件未触发,无法发送欢迎嵌入消息求助

DiscordJS v14 guildMemberAdd事件未触发,无法发送欢迎嵌入消息求助

Hey there! Let's troubleshoot why your welcome message isn't working when someone joins your server. I've spotted a few key issues in your code and setup that are likely causing this problem:

1. 未正确处理异步的频道获取操作

channels.fetch()方法返回的是一个Promise对象,但你直接把它赋值给了wcChannel,没有等待它解析完成。这意味着当你调用.send()时,wcChannel并不是一个有效的频道对象,会直接抛出错误(如果没加错误捕获的话,你甚至看不到这个错误!)。

解决方法是把execute函数改成异步函数,并在fetch前加上await:

async execute(member) {
  try {
    const wcChannel = await member.guild.channels.fetch("1155899900161171586")
    // 后续代码放在这里
  } catch (err) {
    console.error("获取欢迎频道或发送消息失败:", err)
  }
}

加上try/catch块能帮你排查权限不足、频道ID无效这类问题。

2. 未启用服务器成员意图(Guild Members Intent)

DiscordJS v14要求你显式启用服务器成员意图才能监听guildMemberAdd事件。你需要做两件事:

  • 在主文件的客户端初始化代码中添加这个意图:
    import { Client, GatewayIntentBits } from 'discord.js';
    
    const client = new Client({
      intents: [
        GatewayIntentBits.Guilds, // 服务器相关事件必备
        GatewayIntentBits.GuildMembers // guildMemberAdd/Remove事件必备
        // 按需添加其他你需要的意图
      ]
    });
    
  • 打开Discord开发者后台,选择你的机器人,找到「特权网关意图」板块,开启「Server Members Intent」选项。

3. 检查事件注册是否正确

确认你的主文件已经正确加载了guildMemberAdd事件文件。如果用了事件处理器,要确保它正确导入了模块并绑定到客户端;如果是手动注册事件,要保证代码类似这样:

client.on('guildMemberAdd', (member) => {
  // 在这里调用你的execute函数,或者直接写逻辑
});

4. 日志和嵌入内容的小优化

你现在的日志会输出类似Usernamehas joined!的内容(缺少空格),而且member.name可能显示的是用户昵称而非真实用户名。换成member.user.tag可以获取完整的「用户名+识别码」:

console.log(`${member.user.tag} has joined!`)
// 嵌入内容里也同步修改:
.setDescription(`${member.user.tag} has joined the \`SeaMystics\`-Server!\n\n**Welcome!**`)

修复后的事件文件示例

这是整合了所有修复的完整代码:

const { EmbedBuilder } = require('discord.js')

module.exports = {
  name: "guildMemberAdd",
  /**
  * @param {import('discord.js').GuildMember} member
  */
  async execute(member) {
    try {
      const wcChannel = await member.guild.channels.fetch("1155899900161171586")
      console.log(`${member.user.tag} has joined!`)

      const wcEmbed = new EmbedBuilder()
        .setTitle("Welcome")
        .setDescription(`${member.user.tag} has joined the \`SeaMystics\`-Server!\n\n**Welcome!**`)
        .setColor("#4ba511")
        .setTimestamp()

      await wcChannel.send({ embeds: [wcEmbed] })
    } catch (error) {
      console.error("处理成员加入事件出错:", error)
    }
  }
}

按照上面的步骤逐一排查,你的欢迎消息应该就能正常工作了!

备注:内容来源于stack exchange,提问作者TheWuschelGang

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.21 12:09:34