I've made a "working" dice roll function on my bot. Works fine for what I need, but was wondering how I could make it so depending on what is said in discord it rolls.
So saying !rolld6 would use 6 in var response instead of 20.
if (message.content.toLowerCase().includes("!rolld20")) {
const ayy = client.emojis.cache.find(emoji => emoji.name === "diceroll");
var response = [Math.floor(Math.random() * ((20 - 1) + 1) + 1)];
message.channel.send(`${ayy}` + `Rolling.`)
.then(msg => {
setTimeout(function() {
msg.edit(`${ayy}` + `Rolling..`)
}, 1000);
setTimeout(function() {
msg.edit(`${ayy}` + `Rolling...`)
}, 2000)
setTimeout(function() {
msg.edit(`${ayy}` + `Rolling....`)
}, 3000)
setTimeout(function() {
msg.edit(`${ayy}` + " Rolled... " + response + " " + `${ayy}`).then().catch(console.error)
}, 4000)
})
return;
}
Not even sure what I would search to figure this out so any help is greatly appreciated!
Use a regular expression instead of .includes, and then extract the digits after the d to randomize.
You don't need to subtract 1, then add 1 right after that - those cancel out, nor do you need to put the result into an array - interpolating just the random number generated into the message will work fine.
const match = message.content.match(/!rolld(\d+)/i);
if (match) {
const die = match[1];
const response = 1 + Math.floor(Math.random() * die);
const ayy = client.emojis.cache.find(emoji => emoji.name === "diceroll");