-3

Pretty much all in the title. I want my bot to respond with a random reply from a pool of replies. This is what I have so far:

const Discord = require('discord.js');
const bot = new Discord.Client();

bot.on('ready', () =>{
    console.log('Just Chillin');
    bot.user.setActivity('Working' , { type: 'BotOn'}).catch(console.error);

}); 

function randomMessage(){
    var randomNumber = Math.round(Math.random()*2); // 0, 1 or 2
    switch(randomNumber){
        case 0: return 'Hello!';
        case 1: return 'Hi!';
        case 2: return 'Hola';
    }
}

bot.on('message' , message => {
    if(message.content.toLowerCase().includes('ping'))
        message.reply('pong')
    else if(message.content.toLowerCase().includes('Hello'))
            message.reply(randomMessage());
});

Also, I do not have much experience in Java, only R, so I was also wondering where you would put the first chunk of code. Would you put it below or above the bot.on('message', message=>? Also, when I have that code entered, nothing happens and everything else runs fine.

Thank you in advance. I apologize for my lack of Java knowledge.

rwHam
  • 1
  • 2
  • Please could you provide a minimum working example? – Lewis Farnworth Feb 03 '21 at 00:00
  • Thanks for reaching out. Could you elaborate on what you mean by a "working example?" – rwHam Feb 03 '21 at 00:05
  • A minimum working example is basically just giving the community enough code to reproduce your project in their own environment, without divulging too much code, and without us (the community) having to guess on the specifics. – Lewis Farnworth Feb 03 '21 at 00:14
  • 1
    Sure, I will update it momentarily. – rwHam Feb 03 '21 at 00:15
  • Maybe just try changing the `else if` to just: `else` ? – Lewis Farnworth Feb 03 '21 at 00:28
  • That worked!! Thank you! – rwHam Feb 03 '21 at 00:36
  • No problem, happy to help. Please mark my comment(s) up and close the question :) – Lewis Farnworth Feb 03 '21 at 00:38
  • Do have another slight problem actually. When someone says "hello" it will work perfectly and gives a random response. But if i say ping, it will reply with pong and a random response. Any clue as to why it may be doing this? – rwHam Feb 03 '21 at 01:56
  • Does this answer your question? [How can I make a random response in Discord.js](https://stackoverflow.com/questions/63894234/how-can-i-make-a-random-response-in-discord-js) – Jakye Feb 03 '21 at 04:13

1 Answers1

0

When I want pick a random message (like 8ball), I always do it this way:

Define different options:

let options = ["Hey", "Hello there!", "Hey, it's me!", "NOT NOW IM BUSY"];

Pick a random message:

let result = options[Math.floor(Math.random() * options.length)];

Log the result:

console.log(result);
Fyxren
  • 250
  • 1
  • 10