Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My bot is always sending the same random message (as an interaction)

I’m more or less new to coding, and I’m trying to code a bot by myself, to play Rock, Paper Scissors.

This RPS game works with Discord buttons, when you click on one of the 3 buttons, the bot randomly replies with one of the 3 different options.

I tested the bot, and everything works fine, except for the random reply. My bot sends one of the 3 options, but until I restart it through the console, it will always send the same option no matter which utton I click.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is the part of the code where the options are defined, as well as the interaction:

const emoji = [
    "✊", 
    "✋", 
    "✌"
];

const randomemoji = emoji[Math.floor(Math.random() * emoji.length)];

client.on("interactionCreate", async interaction => {
    if(interaction.isButton()){
    if(interaction.customId === "Pierre" || "Papier" || "Ciseaux"){
       await interaction.reply(randomemoji);
    }```

What could I do to make my bot not send the same emoji every time, without restarting it every time?

Thanks in advance !

>Solution :

You should get a random emoji inside your event listener.
Your code should look something like this:

const emoji = [
  "✊", 
  "✋", 
  "✌"
];

client.on("interactionCreate", async interaction => {
  if(interaction.isButton()){
  if(interaction.customId === "Pierre" || "Papier" || "Ciseaux"){
     const randomEmoji = emoji[Math.floor(Math.random() * emoji.length)];
     await interaction.reply(randomEmoji);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading