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

discord.js编辑机器人发送给用户DM的消息时报message.author.edit不是函数错误

错误根因

message.author是Discord.js中的用户对象,该对象本身不存在edit方法,你调用的edit属于消息对象的方法,只有拿到机器人之前发送的私信消息实例才能调用该方法编辑内容。

修复方案

场景1:需要编辑机器人之前发给用户的那条私信

你需要先存储首次发送私信返回的消息实例,后续调用该实例的edit方法即可:

// 注意外层处理函数需要加async关键字,否则无法使用await
if (message.content === "#مقابلة") {
    message.channel.send(eembed);   
    if (!(authorId in userApplications)) {
        // 存储发送成功的私信消息实例
        const firstDmMsg = await message.author.send(embed1);
        userApplications[authorId] = { 
            "step" : 1,
            "sentDmMsg": firstDmMsg
        }
    }
} else {
    if (message.channel.type === "dm" && authorId in userApplications) {
        let authorApplication = userApplications[authorId];
        if (authorApplication.step == 1 ) {
            authorApplication.answer1 = message.content;
            // 编辑之前发送的私信
            await authorApplication.sentDmMsg.edit(embed2);
            authorApplication.step ++;
        }
    }
}

场景2:不需要编辑旧消息,只需要给用户发新的私信

直接把错误的message.author.edit(embed2)替换为新发送消息的逻辑即可:

message.author.send(embed2);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 16:12:02