Home > Software design >  How to get multiple message IDs in discord.js?
How to get multiple message IDs in discord.js?

Time:10-13

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.

CodePudding user response:

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].output.slot1}`),
      send(`${acc[player].output.slot2}`),
      send(`${acc[player].output.slot3}`)
   ])).forEach((msg, i) => {
      acc.player.ids[`msg${i   1}`] = msg.id;
   });
} catch (err) {
   console.error(err);
}
  • Related