简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM