Sky
Sky

Reputation: 47

How to make the bot not sent multiple responses depending on what's said (Discord.js)

Right now I'm working on a small rolling bot that will allow a user to send 'Roll' and then it'll prompt them for a number. It uses that number and chooses a random number between 0 and whatever number they inputted. Right now I'm trying to have it say "the roll has been stopped" after someone types stop instead of putting a number which it does, but it always has either "You Rolled a NaN" or "Please provide a valid number!". Is there any way to block that from coming up If the stop is inputted?

code

const Discord = require('discord.js');

const client = new Discord.Client();

function getRandomInt(max){
    return Math.floor(Math.random() * Math.floor(max));
}

const prefix = 'roll';

client.once('ready', () => {
    console.log('your bot name is online!');
    client.user.setActivity("Type 'Role Help'", {type: 3}); 
});

client.on('message', message =>{
    if(!message.content.toLowerCase().startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();

    if(command === ''){
       message.channel.send("What's the **Max** role number you want?");
       message.channel.awaitMessages(m => m.author.id == message.author.id,
        {max: 1, time: 10000}).then(collected => {
            if(collected.first().content == 'stop'  || 'Stop' || 'STOP'){
                message.channel.send('*Roll has been stopped.*');
            };
            console.log('collected :' + collected.first().content);
            if (isNaN(collected.first().content) != (collected.first().content == 'stop')){
                return message.channel.send("Please provide a valid number!")};
            
            var n = (getRandomInt(collected.first().content));  
            message.channel.send("You Rolled a " + (n + 1));
            }).catch(() => {
                message.channel.send("*Roll has been cancelled.*");
     
            })} else if(command === 'Help' || 'help'){

                message.channel.send("**How to use Role Bot**");
                message.channel.send("*1. Type Role to begin*");
                message.channel.send("*2. Input a number or type stop to end the roll.*");
                message.channel.send("**Have Fun!**");
        }
});

Upvotes: 0

Views: 64

Answers (1)

Elitezen
Elitezen

Reputation: 6720

The code continues because you never implemented an end to the file if 'stop' was collected. Use return to end/exit functions.

In the following line, return exits the function with the message being sent as the last output.

Tip: To detect strings with different capitalization just do .toLowerCase() and compare it to a lowercase string so that no matter which letters are capitalized it will still return true

if (collected.first().content.toLowerCase() === 'stop') return message.channel.send('*Roll Has Been Stopped*');

Upvotes: 1

Related Questions