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

Discord机器人消息编辑权限报错:如何编辑自身发送的消息?

解决Discord机器人编辑消息报错的问题

问题原因

你当前代码里用message.edit()修改的是用户发送的!ping消息,Discord不允许机器人编辑其他用户的消息,因此触发Cannot edit a message authorized by another user错误。要编辑的应该是机器人自身发送的回复消息。

修复方案

  1. 保存机器人回复的消息实例:调用message.reply()会返回机器人发送的消息对象,将其存储下来。
  2. 使用该存储的消息对象调用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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 06:22:46