0

I'm currently trying to make a command for my bot that gives a randomized answer out of the following array everytime the command is ran:

const valMapsList = ['Ascent', 'Bind', 'Split', 'Haven'];

I've tried doing this:

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

const prefix = '>!';

client.once('ready', () => {
    console.log('Bot is now ONLINE!')
});

let valMapsList = ['Ascent', 'Bind', 'Split', 'Haven'];
let map = valMapsList[Math.floor(Math.random() * valMapsList.length)];

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

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

    if(command === 'map'){
        message.channel.send("Selected Map: " + map);
    } else if (command == 'ping') {
        message.channel.send('Pong!');
    }
});

This works but will only give the same answer always as the code is just executed on launch. So I need a function I can call in the

if(command === 'map'){
            message.channel.send("Selected Map: " + map);

part that will re-run the randomize.

1 Answer 1

1

It's always the same value because you have the let out of the message listener.

You need to have this:

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

const prefix = '>!';

client.once('ready', () => {
    console.log('Bot is now ONLINE!')
});

const valMapsList = ['Ascent', 'Bind', 'Split', 'Haven'];


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

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

    if(command === 'map'){
        let map = valMapsList[Math.floor(Math.random() * valMapsList.length)];
        message.channel.send("Selected Map: " + map);
    } else if (command == 'ping') {
        message.channel.send('Pong!');
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.