So i created a basic dice rolling dicord bot for Dungeons & Dragons.
the code i have so far works to roll any type of dice, (ex. "roll xdy" "roll 1d20", "roll 100d100")
when someone sends a message that matches, it will output the resulting dice rolls.
my issue is that i would like to add these numbers together and show the resulting total aswell but im unsure how to get there.
// Run dotenv
require('dotenv').config();
const { any } = require('async');
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
client.on('messageCreate', msg => {
z = msg.content;
matches = z.match(/\d+/g);
x = matches[0];
y = matches[1];
if (msg.content === 'ping') {
msg.reply('pong');
}
if (msg.content == 'roll ' + x + 'd' + y) {
function rollDie(sides) {
if (!sides) sides = 6;
return 1 + Math.floor(Math.random() * sides);
}
function rollDice(number, sides) {
var total = [];
var number = x;
var sides = y;
while (number-- > 0) total.push(rollDie(sides));
return total;
}
msg.reply("result: " + rollDice());
console.log(rollDice())
}
});
client.login(process.env.DISCORD_TOKEN);