【问题标题】:How to make my discord.js-bot log the edit of a message?如何让我的 discord.js-bot 记录消息的编辑?
【发布时间】:2021-04-27 07:44:25
【问题描述】:

我正在尝试让我的机器人记录消息中的编辑时间和内容。

这是监听器的代码:

client.on('messageUpdate', (oldMessage, newMessage,message) => {
    client.on('messageUpdate', (oldMessage, newMessage,message) => {
const MessageLog = client.channels.cache.find(channel => channel.id ==='802262886624919572');
var embed = new Discord.MessageEmbed()
.setAuthor(message.author.username).catch(console.error)
.setTimestamp(new Date())
.setColor('#392B47')
.addFields(
    {name: 'original:',value: oldMessage},
    {name: 'edit:', value: newMessage}    );
MessageLog.send(embed);
 });

到目前为止,他在获取 message.author.username 时遇到问题 我试过用 oldmessage 和 newmessage 来拒绝消息,但同样的问题。

控制台日志:TypeError:无法读取未定义的属性“作者”

【问题讨论】:

    标签: javascript node.js discord discord.js bots


    【解决方案1】:

    第一件事,the 'messageUpdate' event has two arguments: oldMessage and newMessage
    第二件事,您不能在 MessageEmbed 上使用.catch。因此,我们将删除那部分代码并用用户检查替换它。
    第三件事,您不需要链接 messageUpdate 事件。
    第四件事,您可以调用 setTimestamp 为空,因为它将默认为 Date.now() (source)
    因此,让我们修复您的代码:

    client.on('messageUpdate', (oldMessage, newMessage) => { // Old message may be undefined
       if (!oldMessage.author) return;
       const MessageLog = client.channels.cache.find(channel => channel.id ==='802262886624919572');
    var embed = new Discord.MessageEmbed()
    .setAuthor(newMessage.author.username)
    .setTimestamp()
    .setColor('#392B47')
    .addFields(
        {name: 'original:',value: oldMessage},
        {name: 'edit:', value: newMessage}    );
    MessageLog.send(embed);
    }
    

    【讨论】:

    • 我不得不删除用户检查,但它现在正在向我的日志通道发送消息。它仍然没有在嵌入中显示 message.author 并且不知何故也没有获取 oldmessage 所以它们都不幸地显示“null”
    • 你能重新检查代码@Sparkly 吗?谢谢。
    【解决方案2】:

    错误消息显示message 未定义。 问题是messageUpdate 事件没有将最后一条消息作为参数(https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-messageUpdate) 要解决此问题,您可以将 message.author.username 中的消息替换为 newMessage。

    我还发现您的代码存在另一个问题,当您将字段添加到嵌入时,您应该使用 newMessage.content 作为值,而不是 newMessage

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      相关资源
      最近更新 更多