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

Discord机器人suggestion命令无法读取channels问题求助

问题排查与解决建议

1. 修复execute函数参数顺序错误

你的execute函数参数顺序不符合discord.js的Slash命令规范,标准参数应为(interaction, client),但你多写了一个message参数,导致client被错误赋值(实际为undefined),这是无法读取频道的核心原因。

修正后的函数开头:

async execute(interaction, client) { // 移除多余的message参数
  const { options } = interaction;
  const suggestion = options.getString('suggestion');
  // 后续逻辑不变
}

2. 替换缓存读取为API拉取(避免缓存未加载)

依赖channels.cache.get()可能因为频道未被缓存而失败,改用channels.fetch()直接从Discord API获取频道,可靠性更高:

// Discord建议处理逻辑
if (interaction.options.getString('plateforme') === 'discord') {
  try {
    const discordsuggChannel = await client.channels.fetch(process.env.suggestionsServer);
    await discordsuggChannel.send(`DISCORD : ${suggestion}`);
    await interaction.reply({ content: '你的Discord建议已提交!', ephemeral: true });
  } catch (error) {
    console.error('提交Discord建议失败:', error);
    await interaction.reply({ content: '提交失败,请联系管理员', ephemeral: true });
  }
}

// Minecraft建议处理逻辑
if (interaction.options.getString('plateforme') === 'minecraft') {
  try {
    const mcsuggChannel = await client.channels.fetch(process.env.suggestionsMC);
    await mcsuggChannel.send(`MINECRAFT : ${suggestion}`);
    await interaction.reply({ content: '你的Minecraft建议已提交!', ephemeral: true });
  } catch (error) {
    console.error('提交Minecraft建议失败:', error);
    await interaction.reply({ content: '提交失败,请联系管理员', ephemeral: true });
  }
}

3. 验证环境变量正确性

  • 检查process.env.suggestionsServer和process.env.suggestionsMC是否为目标频道的纯数字ID,无多余空格或字符
  • 可临时添加日志确认值:console.log('Discord建议频道ID:', process.env.suggestionsServer)

4. 确认机器人频道权限

确保机器人在#suggestion-discord和#suggestion-minecraft频道拥有查看频道和发送消息的权限,即使其他命令正常,也需确认这两个频道未单独限制机器人权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 17:06:28