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

discord.js v13跨服务器发送频道消息报错:TypeError问题求助

解决discord.js v13跨服务器发送消息时的TypeError问题

错误原因

你当前代码用interaction.guild.channels.cache.get()获取频道,但interaction.guild指向的是触发斜杠命令的服务器,而目标频道在另一台服务器,所以这个方法找不到对应频道,返回undefined,调用send()时就会抛出Cannot read properties of undefined (reading 'send')错误。

解决方法

1. 基础修正(从全局缓存获取频道)

直接通过机器人客户端的全局频道缓存获取目标频道,替换原代码:

// 替换为实际的目标频道ID
const targetChannelId = "目标频道的ID";
const channel = client.channels.cache.get(targetChannelId);

// 先判断频道是否存在
if (!channel) {
  return interaction.reply({ 
    content: "找不到目标频道,请检查频道ID是否正确,或确认机器人已加入目标服务器", 
    ephemeral: true 
  });
}

channel.send({
  content: "<@&890249127907172407>",
  embeds: [Embed],
  components: [row]
});

2. 兼容缓存未加载的情况(强制获取频道)

如果目标频道不在机器人的缓存中(比如机器人刚启动),用fetch()方法强制获取:

const targetChannelId = "目标频道的ID";

try {
  const channel = await client.channels.fetch(targetChannelId);
  
  // 确认是文本频道才能发送消息
  if (!channel.isTextBased()) {
    return interaction.reply({ 
      content: "目标ID对应的不是可发送消息的文本频道", 
      ephemeral: true 
    });
  }

  channel.send({
    content: "<@&890249127907172407>",
    embeds: [Embed],
    components: [row]
  });
  
  interaction.reply({ content: "消息已成功发送", ephemeral: true });
} catch (error) {
  console.error("发送消息失败:", error);
  interaction.reply({ 
    content: "发送失败,请检查频道ID是否正确,或确认机器人在目标服务器有发送消息的权限", 
    ephemeral: true 
  });
}

额外注意事项

  • 确保机器人已经加入目标服务器,并且拥有发送消息、嵌入链接等必要权限。
  • 频道ID必须是字符串格式,不要遗漏引号。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 01:15:11