Discord.js RichEmbed字段值“空”,甚至认为它们不是。 messageUpdate

时间:2019-10-27 21:18:08

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

所以基本上我一直在为日志编辑日志。该功能的目的是在有人编辑消息时在modlog通道中编写。我也想让机器人在更新前后写消息。

以下是代码:

bot.on('messageUpdate', (oldMessage, newMessage) => {
    var msgup = new Discord.RichEmbed()
    .setTitle(`**MESSAGE EDIT**`)
    .addField(`Old Message:` , `${oldMessage.content}`)
    .addField(`New Message:` , `${newMessage.content}`)
    .addField(`In channel:` , oldMessage.channel)
    .addField(`By` , oldMessage.author)
    .setTimestamp()
    newMessage.channel.send(msgup).catch(console.error);

 });

控制台错误:

C:\Users\grofg\desktop\discordbot\node_modules\discord.js\src\structures\RichEmbed.js:166
    if (!/\S/.test(value)) throw new RangeError('RichEmbed field values may not be empty.');
                           ^

RangeError: RichEmbed field values may not be empty.
    at RichEmbed.addField (C:\Users\grofg\desktop\discordbot\node_modules\discord.js\src\structures\RichEmbed.js:166:34)
    at Client.bot.on (C:\Users\grofg\desktop\discordbot\index.js:455:6)
    at Client.emit (events.js:198:13)
    at MessageUpdateAction.handle (C:\Users\grofg\desktop\discordbot\node_modules\discord.js\src\client\actions\MessageUpdate.js:13:16)
    at MessageUpdateHandler.handle (C:\Users\grofg\desktop\discordbot\node_modules\discord.js\src\client\websocket\packets\handlers\MessageUpdate.js:7:34)
    at WebSocketPacketManager.handle (C:\Users\grofg\desktop\discordbot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
    at WebSocketConnection.onPacket (C:\Users\grofg\desktop\discordbot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\grofg\desktop\discordbot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\grofg\desktop\discordbot\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:198:13)

机器人的功能:

-Bot完全按计划执行功能。我具有嵌入的所有组件(标题,4xFields和时间戳)。在所有字段中,所有内容均正确写入(旧消息,新消息,频道和作者),但是会发生什么呢?

-即使bot执行了该功能,它也会因错误而停止。该bot崩溃了,并说Field是空的,即使它已经写了所有内容,但肯定不是空的。

我尝试了什么?

首先,我尝试从oldMessage.contentnewMessage.content删除内容。无论如何,它做同样的事情。

其次,我尝试使它像.addField('Old Message:' , oldMessage),而没有'${}'。 我也尝试过用.content做同样的事情。 它仍然做同样的事情,可以完成工作,但是会出错并崩溃。

由于控制台错误:

at Client.bot.on (C:\Users\grofg\desktop\discordbot\index.js:455:6) 我强调这是一个问题,因为第455行与.addField('Old Message:' , '${oldMessage.content}')

完全一致

感谢您通读完整的问题,对于解决该问题,我将不胜感激。

此致,     -卢克

1 个答案:

答案 0 :(得分:1)

bot.on('messageUpdate', (oldMessage, newMessage) => {

if (oldMessage.author.bot) return;
if (oldMessage.content === newMessage.content) return;
if(!oldMessage.partial) {

var msgup = new Discord.RichEmbed()
.setTitle(`**MESSAGE EDIT**`)
.addField(`Old Message:` , `${oldMessage.content.slice(0, 950)}\n\u200B`)
.addField(`New Message:` , `${newMessage.content.slice(0, 950)}\n\u200B`)
.addField(`In channel:` , oldMessage.channel.name)
.addField(`By` , oldMessage.author.tag)
.setTimestamp()
newMessage.channel.send(msgup).catch(console.error);
};    
});

我相信是oldMessage会导致此问题。这是我所做的,因此不能为空。 (还向您的.name添加了message.channel,以便它可以正确显示名称,与oldMessage.author相同,我添加了.tag。)希望这会有所帮助。