user13605748
user13605748

Reputation:

Discord.js send messages in order

I have a discord bot that, on command, will send multiple messages to a channel. Some of the messages are images, and they always get sent last.

msg.channel.send({files: ["image.png"]});
for (var i in rules) {
    msg.channel.send({embed: rules[i]});
}
msg.channel.send({files: ["image2.png"]});
msg.channel.send({embed: embed1});
msg.channel.send({files: ["image3.png"]});
msg.channel.send({embed: embed2});

I want the messages to be sent in the order of the code.

Upvotes: 0

Views: 1535

Answers (2)

Vasco Fortuna
Vasco Fortuna

Reputation: 1

Because you are using asynchronous functions, the messages will not sent by order of coding, but rather, they will be sent according to their processing time.

Since images are much bigger in size than text messages, they will be sent last in an asynchronous enviroment.

You need to use callback to turn those functions synchronous, but I recommend searching for a discord command that is synchronous by nature.

Upvotes: 0

Lioness100
Lioness100

Reputation: 8412

TextChannel.send() returns a promise, so you can just use async/await

// make sure your function is async
await msg.channel.send({files: ["image.png"]});
await msg.channel.send({files: ["image2.png"]});
await msg.channel.send({embed: embed1});
await msg.channel.send({files: ["image3.png"]});
await msg.channel.send({embed: embed2});

Upvotes: 2

Related Questions