When I have this code here:
client.on("message", msg => {
const args = msg.content.trim().split(/ /g);
let second = args[1];
let x = Math.floor((Math.random() * second) 1);
if(msg.content === "random") {
msg.channel.send(`${x}`)
console.log(x)
console.log(second)
}
})
I need to send "random 10 (example)" twice and it still doesn't work cause it uses the "random" as the input for "second¨ how do i make this work?
CodePudding user response:
Since "msg" is a string, you need to parse it as an integer before preforming an operation on it. I recommend replacing the "let x" line with the following:
let x = Math.floor((Math.random() * parseInt(second)) 1);
parseInt is an internal function that takes a string with number contents and turns it into an integer.
CodePudding user response:
const triggerWords = ['random', 'Random'];
client.on("message", msg => {
if (msg.author.bot) return false;
const args = msg.content.trim().split(/ /g);
let second = args[1];
let x = Math.floor((Math.random() * parseInt(second)) 1);
triggerWords.forEach((word) => {
if (msg.content.includes(word)) {
msg.reply(`${x}`);
}
});
});
CodePudding user response:
To add on to quandale dingle's answer, what you can do with your bot is to create a command handler, which will make the developing process much easier and will look nice. If you don't really want to create a command handler, you can also use the following code:
if (message.content.startsWith(prefix)){
//set a prefix before, and include this if statement in the on message
const args = message.content.slice(prefix.length).trim().split(/ /g)
const commandName = args.shift().toLowerCase()
//use if statements for each command, make sure they are nested inside the startsWith statement
}
If you want to create a command handler, there's some sample code below. It works on discord.js v13, but I've no idea if it will work in v12. This is a more advanced command handler.
/*looks for commands in messages & according dependencies
or command handler in more simple terms*/
const fs = require("fs");
const prefix = " "; //or anything that's to your liking
client.commands = new Discord.Collection();
fs.readdirSync("./commands/").forEach((dir) => {
const commandFiles = fs.readdirSync(`./commands/${dir}/`).filter((file) =>
file.endsWith(".js")
); //subfolders, like ./commands/helpful/help.js
//alternatively... for a simpler command handler
fs.readdirSync("./commands/").filter((file) =>
file.endsWith(".js")
);
for (const file of commandFiles) {
const commandName = file.split(".")[0]
const command = require(`./commands/${file}`);
client.commands.set(commandName, command);
}
});
//looks for commands in messages
client.on("messageCreate", message => {
if (message.author == client.user) return;
if (message.content.startsWith(prefix)){
const args = message.content.slice(prefix.length).trim().split(/ /g)
const commandName = args.shift().toLowerCase()
const command = client.commands.get(commandName)
if (!command) return;
command.run(client, message, args)
}
});