Gh05d
Gh05d

Reputation: 8962

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. When I do this, I always get this error:
Cannot read property 'slice' of undefined

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. 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?

Upvotes: 1

Views: 213

Answers (1)

Gh05d
Gh05d

Reputation: 8962

Solution

After hacking the source code of discord.js I discovered the culprit in the Message.js file. 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. Just passing a fake id (a string of random numbers) did the trick.

Upvotes: 1

Related Questions