【问题标题】:I am trying to get messageUpdate logs but sends null instead我正在尝试获取 messageUpdate 日志,但改为发送 null
【发布时间】:2021-05-26 16:15:26
【问题描述】:

我正在尝试构建一个机器人,如果用户在频道中编辑他的消息,它将记录下来!

我正在使用代码:

client.on('messageUpdate', async (oldMessage, newMessage) => {
  if (oldMessage.content === newMessage.content) {
    return;
  }
  const embed = new Discord.MessageEmbed()
    .setColor('RED')
    .setAuthor(newMessage.author)
    .setDescription(`Ένα μήνυμα του ${newMessage.author} επεργάστηκε στο κανάλι ${newMessage.channel} `)
    .addField('Παλιό Μήνυμα:', oldMessage.content, true)
    .addField('Καινούργιο Μήνυμα:', newMessage.content, true)
    
  const channel = client.channels.cache.find(channel => channel.name === 'test')
  channel.send(embed);
});

代码没有错误;它发送嵌入,但不显示消息作者和旧消息,而是发送 null 而不是消息作者。

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    您不能在嵌入标题中提及。任何类型的提及只会在字段值和描述中正确呈现。

    如果您想添加这些提及,您可以使用另一个 .addField().setDescription()。而且您不能将作者对象传递给.setAuthor()。您只能使用.setAuthor() 设置作者姓名、图标和 URL:

    client.on('messageUpdate', async (oldMessage, newMessage) => {
      if (oldMessage.content === newMessage.content) {
        return;
      }
      const embed = new MessageEmbed()
        .setColor('RED')
        .setAuthor(newMessage.author.username, newMessage.author.displayAvatarURL())
        .setDescription(
          `Ένα μήνυμα του ${newMessage.author} επεργάστηκε στο κανάλι ${newMessage.channel} `
        )
        .addField('Παλιό Μήνυμα:', oldMessage.content, true)
        .addField('Καινούργιο Μήνυμα:', newMessage.content, true);
    
      const channel = client.channels.cache.find(
        (channel) => channel.name === 'test-cat-2',
      );
      channel.send(embed);
    });
    

    【讨论】:

    • 感谢 Zsolt 先生的快速响应!我测试了你所说的,现在我得到了错误Cannot read property 'username' of null
    • 我对问题代码进行了更新。如果我将我的设置保留在代码中,它只需发送null for author
    • 标题为描述,我添加了newMessage.author,但发送的是空值。如果我添加newMessage.author.username 会向我发送TypeError: Cannot read property 'username' of null 的错误。在线 .addField('Παλιό Μήνυμα:', oldMessage.content, true) 再次发送给我 null !
    • 这很奇怪。这些以前有用吗?我刚刚尝试过,上面的代码似乎运行良好。
    • 刚刚重新启动了我的代码,一切正常。我不知道发生了什么。对不起,最后无缘无故耽误了您的时间!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 2022-08-02
    • 2021-05-31
    • 2017-01-11
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多