Discord.js V14机器人无法编辑消息(私信场景报ChannelNotCached错误)
解决Discord私信场景下井字棋机器人ChannelNotCached错误
问题描述
我开发了一款仅需授权用户账号即可使用的Discord井字棋机器人,基于discord-gamecord模块实现。机器人在服务器内运行正常,但在私信场景下,用户尝试接受或拒绝决斗时,机器人无响应,控制台抛出ChannelNotCached错误。
报错信息
C:\Users\USER\Desktop\Andy's Bot\node_modules\discord.js\src\structures\Message.js:749 if (!this.channel) return Promise.reject(new DiscordjsError(ErrorCodes.ChannelNotCached)); ^ Error [ChannelNotCached]: Could not find the channel where this message came from in the cache! at Message.edit (C:\Users\USER\Desktop\Andy's Bot\node_modules\discord.js\src\structures\Message.js:749:46) at TicTacToe.TicTacToeGame (C:\Users\USER\Desktop\Andy's Bot\node_modules\discord-gamecord\src\TicTacToe.js:96:15) at TicTacToe.startGame (C:\Users\USER\Desktop\Andy's Bot\node_modules\discord-gamecord\src\TicTacToe.js:84:23) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { code: 'ChannelNotCached' } Node.js v20.17.0
机器人代码
const { Client, GatewayIntentBits, REST, Routes } = require('discord.js'); const { TicTacToe } = require('discord-gamecord'); const clientID = ''; const token = ''; const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessageReactions ] }); client.once('ready', () => { console.log(`Logged in as ${client.user.tag}`); }); client.on('interactionCreate', async interaction => { if (!interaction.isCommand()) return; const { commandName } = interaction; if (commandName === 'tic-tac-toe') { const game = new TicTacToe({ message: interaction, isSlashGame: true, opponent: interaction.options.getUser('opponent'), embed: { title: 'Tic Tac Toe', color: '#5865F2', footer: 'Have fun!', }, emojis: { xButton: '❌', oButton: '⭕', blankButton: '➖', }, mentionUser: true, timeoutTime: 60000, xButtonStyle: 'DANGER', oButtonStyle: 'PRIMARY', turnMessage: '{emoji} | It\'s now **{player}**\'s turn!', winMessage: '{emoji} | **{player}** won the Tic Tac Toe game!', tieMessage: 'The game ended in a tie!', timeoutMessage: 'The game went unfinished :(', playerOnlyMessage: 'Only {player} and {opponent} can use these buttons.' }); game.startGame(); } }); client.login(token); const commands = [ { name: 'tic-tac-toe', description: 'Starts a game of tic tac toe!', type: 1, "contexts": [0, 2], "integration_types": [0, 1], options: [ { name: 'opponent', description: 'The user to play against', type: 6, required: true, } ], } ]; const rest = new REST({ version: '10' }).setToken(token); (async () => { try { console.log('Started refreshing application (/) commands.'); const data = await rest.put( Routes.applicationCommands(clientID), { body: commands }, ); console.log('Successfully reloaded application (/) commands.'); console.log(data); } catch (error) { console.error('Error while registering commands: ', error); } })();
解决方案
1. 添加私信相关的网关权限(Intents)
当前客户端仅配置了服务器相关的Intents,缺少私信频道的缓存权限。修改客户端初始化代码,添加以下Intents:
const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessageReactions, // 添加私信相关权限 GatewayIntentBits.DirectMessages, GatewayIntentBits.DirectMessageReactions, GatewayIntentBits.DirectMessageTyping // 可选,用于监听私信输入状态 ] });
2. 原理说明
报错的核心原因是:在私信场景下,客户端未缓存私信频道对象,导致discord-gamecord模块在尝试编辑游戏消息时,找不到对应的频道。添加上述Intents后,客户端会主动监听并缓存私信频道,模块就能正常获取到频道对象,解决ChannelNotCached错误。
3. 验证命令配置
你的命令已经设置了contexts: [0, 2](0代表服务器,2代表私信),这部分配置正确,无需修改。
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

