Issues with Discord Bot handling RichEmbeds and awaiting messages

I’m relatively inexperienced with JavaScript, so your patience is appreciated. I’m developing a command for my Discord Bot that triggers with ‘!records’. I want the Bot to respond with a RichEmbed that presents two selectable options. Here’s the code snippet I have:

if (message.content.startsWith(config.prefix + 'records')) {
  const richEmbed = new Discord.RichEmbed()
    .setTitle('D E I C I D E Records')
    .setAuthor('D E I C I D E', 'https://example.com/logo.png')
    .setColor(3447003)
    .setDescription('Please choose an option.')
    .setTimestamp()
    .addField('1. Raid Victories', 'Reply with "1" for Raid Victories.')
    .addField('2. Defense Victories', 'Reply with "2" for Defense Victories.');

  message.channel.send({embed: richEmbed});
  message.channel.awaitMessages(res => res.content === '1', {
    max: 1,
    time: 10000,
    errors: ['Time expired!'],
  });
  message.channel.send(drecords.RaidWins);
  message.channel.awaitMessages(res => res.content === '2', {
    max: 1,
    time: 10000,
    errors: ['Time expired!'],
  });
  message.channel.send(drecords.DefenseWins);
}

Currently, when ‘!records’ is executed, the Bot sends the RichEmbed but also immediately sends the records instead of waiting for a selection. Could someone explain what I’m doing wrong?

Additionally, I’d like to create a single command to update the records without manually editing the Bot’s files. My current code for updating the records looks like this:

if (message.content.startsWith(config.prefix + 'updateraids')) {
  let updatedRecord = message.content.split(" ").slice(1)[0];
  drecords.RaidWins = updatedRecord;
  fs.writeFile('./drecords.json', JSON.stringify(drecords), (err) => console.error);
}

if (message.content.startsWith(config.prefix + 'updatedefenses')) {
  let updatedRecord = message.content.split(" ").slice(1)[1];
  drecords.DefenseWins = updatedRecord;
  fs.writeFile('./drecords.json', JSON.stringify(drecords), (err) => console.error);
}

The issue is that when I use commands like ‘!updatedefenses DefenseWin2’, it replaces the existing entry instead of adding to it. How can I modify it so that entries can be either added or removed? Thanks for any advice, as I’ve been researching this extensively with no success.

Hey there, seems like you might’ve missed checking if a message is actually collected before proceeding. Use the then you have but also add checks before sending out the records. Also, for updating, you could split the message differently to handle adding/subtracting more intuitively. good luck!

One thing you could improve is the order and logic of awaiting messages. The reason it’s directly sending out the records is that it’s sequentially awaiting messages with the same conditions up front. You might want to try wrapping your awaitMessages in separate functions or implement Promises to ensure it properly awaits and executes after user input.

Regarding updating records, you should focus on parsing the values and using arithmetic operations. Convert values to numbers, modify them, and then convert back to strings before updating the JSON object. This approach would facilitate appending or subtracting without having manual overwriting.

hey, looks like in your awaitMessages, you need to restructure a bit. Try using .then for processing like, message.channel.awaitMessages(filter, options).then(collected => { // process based on answer }). Also, for updating, you might want to parse the current number, modify it, and save it back. Hope this helps!