Ray
Ray

Reputation: 21

How to get multiple message IDs in discord.js?

My bot sends 3 messages following eachother and I need to save their IDs, because I need to edit them later. If I do something like this:

message.channel.send(`${acc[player].output.slot1}`).then((m) => {
  acc[player].ids.msg1 = m.id
})
  
message.channel.send(`${acc[player].output.slot2}`).then((n) => {
  acc[player].ids.msg2 = n.id
})
    
message.channel.send(`${acc[player].output.slot3}`).then((o) => {
  acc[player].ids.msg3 = o.id
})

all three will have the 3rd one's ID. Slowing down the process or doing it step by step didn't help.

Upvotes: 0

Views: 211

Answers (1)

Elitezen
Elitezen

Reputation: 6730

Not sure why all 3 entries would result in the same value, however maybe handling the promises differently could help. Try awaiting Promise.all() with an array of the send messages and map all the results to the id.

const { send } = message.channel;

try {
   (await Promise.all([
      send(`${acc[player].slot1}`),
      send(`${acc[player].slot2}`),
      send(`${acc[player].slot3}`)
   ])).forEach((msg, i) => {
      acc.player.ids[`msg${i + 1}`] = msg.id;
   });
} catch (err) {
   console.error(err);
}

Upvotes: 1

Related Questions