Search code examples
javascriptasynchronousdiscorddiscord.jsembed

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.


Solution

  • 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});