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);
CodePudding user response:
Something like this should do it.
Reduce applies a callback function to each element of a given array, so you can use it to add up all the values of an array and report it.
passing an array 'totalArr' into this function should give you the sum total of all numbers in the array
Javascript
const rollTotal = function (totalArr) {
const startingRoll = 0;
const subTotal = totalArr.reduce(
(previousValue, currentValue) =>
previousValue currentValue,
startingRoll
);
return subTotal;
};
Test
console.log(rollTotal([1, 2, 3]));
Output = 6
CodePudding user response:
Seemed like you were declaring variables without using let
or var
(z
, matches
, x
, and y
). No reason to use var
anymore. I used the toLowerCase and trim methods, and wrapped all the logic in an if (msg) { .. }
to scrub input a bit. You had parameters for the rollDice
function but then were just pulling from the variables you created and not using the parameters, so I modified that. I used the reduce method to sum the array. I changed some variable names to be more descriptive (such as z
to msgContent
) and used them in more available places.
In your rollDie
function you give a default die side number of 6, but because of the if
statement wrapping it, the only way the function will be called is if a side number is specifically chosen. So I modified this so that someone can type "Roll 3" if they just want to roll 3 6-sided dice. When reporting the dice roll I used the join method so they would display in a comma and space separated list instead of as an array.
const { any } = require("async");
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
client.on("messageCreate", (msg) => {
if (msg) {
let msgContent = msg.content.trim().toLowerCase(),
matches = msgContent.match(/\d /g),
numDice = matches[0],
numSides = matches[1],
diceResults = [];
if (msgContent === "ping") {
msg.reply("pong");
}
if (
msgContent === "roll " numDice ||
msgContent === "roll " numDice "d" numSides
) {
function rollDie(sides) {
if (!sides) sides = 6;
return 1 Math.floor(Math.random() * sides);
}
function rollDice(number, sides) {
let diceArray = [];
while (number-- > 0) diceArray.push(rollDie(sides));
return diceArray;
}
function calcDiceTotal(diceArray) {
return diceArray.reduce(
(previousValue, currentValue) => previousValue currentValue,
0
);
}
diceResults = rollDice(numDice, numSides);
msg.reply(
"ROLLING... " diceResults.join(", ")
" || TOTAL: " calcDiceTotal(diceResults);
);
}
}
});
client.login(process.env.DISCORD_TOKEN);
Output should look like this: "ROLLING... 3, 1, 6, 3, 5 || TOTAL: 18"