【问题标题】:Discord.js RichEmbed field values "empty" even thought they are not. messageUpdateDiscord.js RichEmbed 字段值“空”,甚至认为它们不是。消息更新
【发布时间】:2020-02-23 06:39:58
【问题描述】:

所以基本上我一直在为消息编辑制作日志。该功能的目的是在有人编辑消息时写入 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 执行该功能,它也会因错误而停止。机器人崩溃了,它说 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}') 完全一致@

感谢您通读整个问题,对于解决此问题的任何帮助或提示,我将不胜感激。

真诚地, -卢克

【问题讨论】:

  • 你确定oldMessage.content 不是空的吗?如果您将console.log(oldMessage) 作为第一行,它会打印什么?
  • 我的意思是其中的内容肯定不是空的,因为我在编辑前得到了充满消息的字段,编辑后是一条消息。但它仍然崩溃。

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


【解决方案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)希望这会有所帮助。

【讨论】:

  • 嘿,我已经尝试使用您的建议,但并没有按照我的预期进行。因此,基本上在实现此代码后,您提供的机器人不会崩溃。但他按计划正确发送了一个嵌入,但继续发送包含旧消息和新消息空白的垃圾邮件嵌入。在我关闭机器人之前,它会发送垃圾邮件。
  • 是的,很抱歉,添加了一些可以阻止这种情况的检查。查看上面的代码 sn-p。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-24
相关资源
最近更新 更多