Discord机器人消息编辑权限报错:如何编辑自身发送的消息?
解决Discord机器人编辑消息报错的问题
问题原因
你当前代码里用message.edit()修改的是用户发送的!ping消息,Discord不允许机器人编辑其他用户的消息,因此触发Cannot edit a message authorized by another user错误。要编辑的应该是机器人自身发送的回复消息。
修复方案
- 保存机器人回复的消息实例:调用
message.reply()会返回机器人发送的消息对象,将其存储下来。 - 使用该存储的消息对象调用
edit()方法,替代原用户消息对象。
修复后的完整代码
client.on('messageCreate', async (message) => { if (message.author.bot) return; if (message.content === '!ping') { let timecc = 10 * 60; // 保存机器人发送的回复消息对象 const botReply = await message.reply(`⏳${parseInt((timecc / 60)/60)}h${parseInt((timecc / 60)%60)}m${timecc % 60}s start`); const timer = setInterval(async () => { timecc -= 1; try { // 编辑机器人自身发送的消息 await botReply.edit(`⏳${parseInt((timecc / 60)/60)}h${parseInt((timecc / 60)%60)}m${timecc % 60}s`); } catch (err) { console.error('编辑消息失败:', err); clearInterval(timer); // 出错后及时停止定时器,避免内存泄漏 } if (timecc === 0) { clearInterval(timer); try { await botReply.edit('⌛time out'); } catch (err) { console.error('编辑超时消息失败:', err); } } }, 1000); } });
额外提示
- 定时器内的异步编辑操作建议包裹
try/catch,避免单次编辑失败导致后续逻辑异常。 - 出错时务必清理定时器,防止无用的定时任务占用资源。
内容的提问来源于stack exchange,提问作者needyou
相关产品推荐
相关产品推荐

