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

Discord机器人编辑DM消息遇ChannelNotCached错误的解决问询

问题:Discord机器人编辑DM消息时遭遇ChannelNotCached错误

我尝试编辑一条发送在DM(私信)中的消息,使用的代码如下:

async execute(interaction){

// 省略部分函数代码...
// member 和 gameData 已提前定义

// 移除带按钮的消息并发送新消息
// 构建新的嵌入消息
const gmEmbed = new EmbedBuilder()
    .setTitle(`Approved Join Request`)
    .setDescription(`${member.user.tag} has successfully joined ${gameData.gameName}.`)
    .setColor(0x33ff88);
try {
    await interaction.message.edit({ embeds: [gmEmbed] });
}
catch (e) {
    console.log(`[WARN] Unable to edit a DM to ${interaction.user.tag} accepting a player:\n${e}`);
}

// 省略剩余函数代码...

}

运行该函数时出现以下错误:

[WARN] Unable to followup a DM to username redacted accepting a player:
Error [ChannelNotCached]: Could not find the channel where this message came from in the cache!

我已经在index.js中配置了客户端登录参数:const client = new Client({ intents: [..., GatewayIntentBits.DirectMessages] });,但不确定这是否和问题有关。

我试过用interaction.message.fetch().then(message => message.edit(...))但没用,interaction.channel返回null。奇怪的是这个功能偶尔能生效,似乎只在首次调用该交互时有效。请问怎么解决这个问题,成功编辑DM消息?


解决方案

1. 补充Partials配置确保缓存覆盖

你已经添加了DirectMessages意图,但DM频道和消息可能属于未完全缓存的"部分对象",需要在客户端初始化时补充Partials配置:

const client = new Client({
  intents: [/* 其他已配置意图, */ GatewayIntentBits.DirectMessages],
  partials: [Partials.Channel, Partials.Message] // 新增这两个配置
});

Partials能让机器人处理未被完整缓存的频道、消息对象,避免出现找不到缓存的报错。

2. 绕开交互缓存,直接通过用户ID获取DM频道

既然interaction.channel返回null,可以跳过交互对象的缓存依赖,直接通过用户打开DM频道并获取目标消息编辑:

try {
  // 获取当前用户的DM频道
  const dmChannel = await interaction.user.createDM();
  // 通过消息ID直接拉取目标消息
  const targetMessage = await dmChannel.messages.fetch(interaction.message.id);
  await targetMessage.edit({ embeds: [gmEmbed] });
} catch (e) {
  console.log(`[WARN] Unable to edit a DM to ${interaction.user.tag} accepting a player:\n${e}`);
}

这种方式不依赖交互对象的缓存数据,稳定性更高。

3. 使用交互的update方法更新响应

对于按钮这类组件触发的交互,Discord推荐用interaction.update()直接更新原消息,无需依赖频道缓存:

try {
  // 更新原消息,同时清空按钮组件
  await interaction.update({ embeds: [gmEmbed], components: [] });
} catch (e) {
  console.log(`[WARN] Unable to update DM interaction response:\n${e}`);
}

这个方法会直接修改触发交互的那条消息,逻辑更贴合组件交互的设计逻辑。

4. 检查是否有手动清理缓存的逻辑

如果你的机器人代码里有手动清理消息、频道缓存的逻辑,可能误删了DM相关的缓存数据,排查并调整这类清理逻辑,确保交互处理期间相关缓存不被删除。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 05:25:26