Rock paper scissor array not working discord.js v12

1
Asked by : melany wijngaard 2020-04-29 12:22

I am trying to build an RPS game in my Discord bot. I want to add the functionality that if the word you choose does not exist in the list, it will return a message. This is my code so far:

 async execute(message, args, cmd, client, Discord, profileData) {
  if (!args.length)
    return message.channel.send('To keep it fair, also send your pick!'); //If you don't
  //send a second
  //argument.

  const list = ['rock', 'paper', 'scissor']; // What the bot accepts.

  const rps = ['Rock! - ⛰', 'Paper! - 📄', 'Scissor! - ✂']; // Where the bot can
  // randomly pick from
  const random = rps[Math.floor(Math.random() * rps.length)];

  for (var i = 0; i < list.length; i++) {
    if (message.content.includes(list[i])) {
      // If it does exits,
      // just execute the
      // command.
      message.channel.send(random);
      break;
    } else {
      message.channel.send(`\`${args}\` is not a valid option.`); //Return if the
      //argument you are
      //sending does not exist
      //in the list.
      break;
    }
  }
},

I thought this would work, and already did many other variants of this one. But still, even though for example paper is in the list, it still sends me ".. is not a valid option". It only works with rock. So what I want here is that if you send anything other than the values inside the array, it returns.

To make it short, I have two questions:

  1. How can I fix this issue?
  2. If I capitalize the values in the list ( Rock), will it still work if I send rock?

Here is the link to my problem: https://imgur.com/rhESIZM

1 Answers

0 votes
0
nerea mendez
Answer by : nerea mendez

You don't have to use a for loop. Instead of checking the whole message again, you only need to check the first argument ( args[0]). You can simply use Array#includes and to make it case insensitive, convert this argument to lowercase. Your list already includes lowercase letters only, so the following will work:

 async execute(message, args, cmd, client, Discord, profileData) {
  if (!args.length)
    return message.channel.send('To keep it fair, also send your pick!');

  const list = ['rock', 'paper', 'scissors'];
  const rps = ['Rock! - ⛰', 'Paper! - 📄', 'Scissors! - ✂'];
  const random = rps[Math.floor(Math.random() * rps.length)];

  if (!list.includes(args[0].toLowerCase())) {
    return message.channel.send(`\`${args[0]}\` is not a valid option.`);
  }
  // args[0] is valid
  message.channel.send(random);
},

enter image description here


Related Posts
Related Posts
1
Animation delayed because of scroll boun...
jeanne nelson jeanne nelson
2020-04-05  3:34am 2020-04-05 3:34am
2
Move multi-select list with JavaScript...
noélie roux noélie roux
2020-03-20  11:41pm 2020-03-20 11:41pm
3
Reset form method not working in timepic...
joan vega joan vega
2021-01-05  5:36am 2021-01-05 5:36am
4
Using JS, how to implement an e-commerce...
marcus andersen marcus andersen
2021-09-27  8:22am 2021-09-27 8:22am
5
How to wait for cypress command to finis...
sara johansen sara johansen
2020-07-06  5:40pm 2020-07-06 5:40pm