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

如何修复Discord Bot JS发送欢迎Embed消息时channel未定义的问题

错误原因

代码中未声明channel变量,直接调用channel.send()才会触发channel is not defined报错。

修复方案

根据你想要发送消息的目标位置,选择对应实现方式:

  • 给新入群成员发送私信
    直接调用member.send()方法即可,无需额外获取频道
    示例代码:
    client.on('guildMemberAdd', async member => {
      const embed = new Discord.MessageEmbed()
        .setTitle("Welcome")
        .setDescription(`Hi ${member}, Welcome to server`)
        .setImage('https://image.ibb.co/dNGVKz/Screenshot_1.png');
      await member.send({ embeds: [embed] }).catch(err => console.log('用户私信未开启,发送失败'));
    });
    
  • 发送到服务器指定欢迎频道
    先通过频道ID获取频道实例,再调用发送方法,更推荐用ID而非频道名获取,避免重名问题
    示例代码:
    client.on('guildMemberAdd', member => {
      const embed = new Discord.MessageEmbed()
        .setTitle("Welcome")
        .setDescription(`Hi ${member}, Welcome to server`)
        .setImage('https://image.ibb.co/dNGVKz/Screenshot_1.png');
      // 替换为你自己的欢迎频道ID
      const welcomeChannel = member.guild.channels.cache.get('你的频道ID');
      // 提前判断频道是否存在,避免后续报错
      if (welcomeChannel) {
        welcomeChannel.send({ embeds: [embed] });
      }
    });
    

注意事项

  • 若使用discord.js v13及以上版本,发送embed的参数格式为{ embeds: [embed实例] },旧版本的{embed}写法已不生效
  • 机器人需要拥有目标频道的「发送消息」「嵌入链接」权限,否则会发送失败
  • 给用户发私信时如果用户关闭了陌生人私信权限,会触发报错,建议加catch捕获异常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 05:15:04