如何让我的 discord.js-bot 记录消息的编辑?

时间:2021-01-22 20:00:12

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

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

这是监听器的代码:

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 来定义消息,但同样的问题。

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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);
}