简体   繁体   English

尝试在 Discord.js 中实例化新消息 Object 时出错

[英]Error when trying to instantiate a new Message Object in Discord.js

Problem问题

I am trying to create a [message object][1] in [discordjs][1] in order to send a message from my account. 我正在尝试在 [discordjs][1] 中创建一个 [message object][1],以便从我的帐户发送消息。 When I do this, I always get this error: 当我这样做时,我总是得到这个错误:
{ author: message.author, content: message.content },

Code代码

Here is my code: 这是我的代码:
 client.on("message", async message => { if (message.author && message.author.bot) { return; } try { const myMessage = new Discord.Message( client, { author: { id: "myID" }, content: "My new Message" }, message.channel ); console.log(myMessage);

Additional Information附加信息

I have logged Discord, client and message, all exist and are valid objects. 我已经记录了 Discord、客户端和消息,都存在并且是有效对象。 I also replaced the data part of the constructor with this: 我还用这个替换了构造函数的数据部分:
 { author: message.author, content: message.content },

but to no avail.但无济于事。 The error was the same.错误是一样的。 So what am I doing wrong here?那么我在这里做错了什么?

Solution解决方案

After hacking the source code of discord.js I discovered the culprit in the Message.js file.在破解discord.js的源代码后,我在Message.js文件中发现了罪魁祸首。 It is this line:就是这一行:

this.createdTimestamp = SnowflakeUtil.deconstruct(this.id).timestamp;

The problem is that I never passed any id to the constructor function in my data object.问题是我从未在我的data object 中将任何id传递给构造函数 function。 Just passing a fake id (a string of random numbers) did the trick.只需传递一个假 id(一串随机数)就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM