Kasper88
Kasper88

Reputation: 13

Discord.js: create Message object before sending

I'm trying to create a message object before actually sending it in the channel, is this possible? I keep getting weird errors on this code:

var new_message = new Discord.Message(channel, {
    author: client.user,
    content: 'Hello, ' + message.author.username + '!'
}, client);
console.info(new_message);
channel.send(new_message);

but that just gives me the error below. The reason i want the message object, is so that i can easily add a reaction to it right after sending.

React-o-matic Discord/node_modules/discord.js/src/structures/Message.js:86
  this.author = this.client.users.add(data.author, !data.webhook_id);
                                  ^
TypeError: Cannot read property 'add' of undefined
    at Message._patch (React-o-matic Discord/node_modules/discord.js/src/structures/Message.js:86:39)
    at new Message (React-o-matic Discord/node_modules/discord.js/src/structures/Message.js:44:20)
    at Client.<anonymous> (React-o-matic Discord/bot.js:18:35)
    at Client.emit (events.js:314:20)
    at MessageCreateAction.handle (React-o-matic Discord/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (React-o-matic Discord/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (React-o-matic Discord/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (React-o-matic Discord/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (React-o-matic Discord/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (React-o-matic Discord/node_modules/discord.js/node_modules/ws/lib/event-target.js:125:16)

Upvotes: 0

Views: 3560

Answers (2)

Y. Georgiev
Y. Georgiev

Reputation: 610

Try this:

var msg = new Discord.Message(client, {
    id: message.id,
    type: message.type,
    content: message.content,
    author: client.user,
    pinned: message.pinned,
    tts: message.tts,
    embeds: message.embeds,
    attachments: message.attachments,
    nonce: "123" // idfk
}, channel)

This is the minimum you need to create a Message object.

Upvotes: 1

Kasper88
Kasper88

Reputation: 13

I found another way to solve my problem: use the .then() functionality:

channel.send('Hello, ' + message.author.username + '!')
    .then(function (sent_message) {
        console.info("message sent");
    });

This allows me to do basically the same! There is no documentation about the .send() command, but i tried, and it works haha

Upvotes: 0

Related Questions