Reputation: 83
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.
Upvotes: 0
Views: 40
Reputation: 2367
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!');
}
});
Upvotes: 1