'How can I add bonus effects to a dice game in discord js?
I have a question, which i wonder if it is even possible. All the code works exactly how i want it to. First, let me explain what my purpose is!
I have a fully functionnal script for my discord bot. Basically, it is a dice game between 2 players with in stake a role chosen randomly between all the roles the player has. Each role will have a BONUS EFFECT and what i would like to do is to add this bonus effect to the game. In a manner, for example just before the player 1 pulls the first dice, ask him if he wants to activate his bonus or something like that.
The bot works this way : !fight usertag to start the game, click on a dice emoji to react and it rolls a 20 dice to determine the beginner,it sends a PM with the choosen role and his effect, and then each user has to react with a dice emoji to "inflict damages". I'll add an image for clarity:
I will post here my code so you can see.
const { ReactionCollector } = require('discord.js');
const request = require("./../config");
const { MessageEmbed } = require('discord.js');
const embed = new MessageEmbed();
function rolldice(numero){
return Math.floor(Math.random() * numero + 1);
}
function premier(msg,user){
diceroll1 = rolldice(20)
diceroll2 = rolldice(20)
msg.channel.send({ embed: { color: 0x407294,title:`Déterminons qui va commencer` ,description:`Un d20 a été lancer pour déterminer le premier à jouer.\n\n${msg.author.tag} a eu : ${diceroll1} \n\n ${user.tag} a eu : ${diceroll2}`}})
if(diceroll1 > diceroll2){
msg.channel.send({ embed: { color: 0x407294, description:`${msg.author.tag} gagne et va donc commencer.`}})
return msg.author.id
}
else if(diceroll1 < diceroll2){
msg.channel.send({ embed: { color: 0x407294, description:`${user.tag} gagne et va donc commencer.`}})
return user.id
}
else{
msg.channel.send({ embed: { color: 0x407294, description:`Vous avez fait égalité. On recommence le tirage.`}})
return premier(msg,user)
}
}
function attaque1(msg, user){
damagedice1 = rolldice(6)
damagedice2 = rolldice(6)
somme = damagedice1 + damagedice2
return {somme,damagedice1,damagedice2}
}
async function bagarre(msg,user,w,v,x,y){
const filter = (reaction,user) => ['🎲'].includes(reaction.emoji.name) && user.id ===v;
const reactOptions = {max : 1};
const duel = await msg.channel.send({ embed: { color: 0x407294, description:`${x} clique sur 🎲 pour lancer 2d6 pour determiner les dégats que tu vas infliger.`}})
await duel.react('🎲');
const reactions = await duel.awaitReactions(filter, reactOptions);
if(reactions.first().emoji.name === '🎲'){
var atksmme = attaque1(msg,user)
w = w - atksmme.somme
msg.channel.send({ embed: { color: 0x407294, description:`${x} inflige ${atksmme.somme} de dégâts à ${y}! (${atksmme.damagedice1} + ${atksmme.damagedice2})`}})
if(w > 0){
msg.channel.send({ embed: { color: 0x407294, description:`Il reste ${w} points de vie à ${y}`}})
}
else if(w<=0){
msg.channel.send({ embed: { color: 0x407294, description:`Il ne reste plus de points de vie à ${y}, il est finito.`}})
}
}
return w
}
module.exports = {
name: 'fight',
args : true,
usage : '@<user>',
async execute(msg,args) {
//VARIABLES
const { client } = msg;
var winner;
var user1health = 12;
var user2health = 12;
//checks if the username to fight is in the msg
var author1 = msg.author.username;
var user = msg.mentions.users.first();
if(!user) return msg.reply({ embed: { color: 0x407294, description:"Tu n'as pas préciser contre qui tu veux te battre!"}});
//checks if the users is trying to fight themselves
if(user.id == msg.author.id) return msg.reply({ embed: { color: 0x407294, description:'Tu ne peux pas te battre contre toi même!'}});
//checks if the user is trying to fight the bot
if(user.bot == true)
return msg.reply({ embed: { color: 0x407294, description:'Tu ne peux pas te battre contre un bot!'}});
//saves the two user ids to variables
var fighter1 = msg.author.id;
var fighter2 = user.id;
var challenged = user.toString();
var fighter1tag = msg.author.tag;
var fighter2tag = user.tag;
const bataille = await msg.channel.send({ embed: {
color: 0x407294,
author: {
name: fighter1tag,
icon_url: msg.author.avatarURL(),
},
description:`${challenged}, tu veux te battre?`}});
await bataille.react('🎲');
const filter = (reaction, user) => ['🎲'].includes(reaction.emoji.name) && user.id === fighter2;
const reactOptions = {max : 1};
const reponseAdversaire = await bataille.awaitReactions(filter,reactOptions);
if(reponseAdversaire.first().emoji.name === '🎲'){
msg.channel.send({ embed: {
color: 0x407294,
title:`Le combat peut commencer.`,
fields : [
{
name: `Pdv de ${fighter1tag}`,
value: user1health,
},
{
name: `Pdv de ${fighter2tag}`,
value: user2health,
},
]
}});
await new Promise(resolve => setTimeout(resolve, 3000));
//MP THE PLAYER WITH THE ROLE AND ITS BONUS
roles1 = msg.member.roles.cache.map(r => r.name).slice(0,-1)
roles2 = msg.mentions.members.first().roles.cache.map(r => r.name).slice(0,-1)
var roleFighter1 = roles1[Math.floor(Math.random()*roles1.length)];
var roleFighter2 = roles2[Math.floor(Math.random()*roles2.length)];
client.users.cache.get(fighter1).send({ embed: { color: 0x407294, description:`Ton rôle en jeu est ${roleFighter1} et voici son effet: ${request.bonus[roleFighter1]}.\nTon adversaire ne le sait pas. Utilise cela à bon escient.`}})
client.users.cache.get(fighter2).send({ embed: { color: 0x407294, description:`Ton rôle en jeu est ${roleFighter2} et voici son effet: ${request.bonus[roleFighter2]}.\nTon adversaire ne le sait pas. Utilise cela à bon escient.`}})
//DETERMINE THE FIRST TO PLAY
winner = premier(msg,user)
await new Promise(resolve => setTimeout(resolve, 3000));
// START OF THE 1V1 LOOP
if(winner === fighter1){
while(user2health > 0){
user2health = await bagarre(msg,user,user2health,fighter1,fighter1tag,fighter2tag);
if(user2health <= 0){break};
while(user1health > 0) {
user1health = await bagarre(msg,user,user1health,fighter2,fighter2tag,fighter1tag);
if(user1health <= 0){break};
break;
}
if(user1health <= 0 || user2health <= 0) {break};
}
}
else if(winner === fighter2){
while(user1health > 0){
user1health = await bagarre(msg,user,user1health,fighter2,fighter2tag,fighter1tag);
if(user1health <= 0){break};
while(user2health > 0) {
user2health = await bagarre(msg,user,user2health,fighter1,fighter1tag,fighter2tag);
if(user2health <= 0){break};
break;
}
if(user1health <= 0 || user2health <= 0) {break};
}
}
}
}}
And another file to have the bonuses
var bonus = {
'@Uchiha 👁️':"Changing the die value ",
'@Akatsuki ☁️':"Pass the opponent's turn",
'@Boss Passione 🐞':"On the next turn, double the value of the dice",
'@Révolutionnaire 🐉':"Reduces the opponent's die value",
'@SSJ3':"Swaps the value of the opponent's dice with that of the thrower",
'@Spin 🌀':"If the opponent has a rank in the Joestar category, heals the caster's HP by 4 and reduces the value of the opponent's next throw by 2",
'@Empereur':"Allows to reuse the value of the previous dice roll",
'@Kage':"Allows the value of the opponent's die to be canceled if it exceeds 4",
'@Ruban Rouge':"Grants a second roll of the dice, the thrower can choose to keep the previous or the new one",
'@Zeppeli':"If the opponent has a rank in the Joestar category, heals the caster's HP by 4 and reduces the value of the opponent's next throw by 2",
'@Jinchuriki 🍥':"Reflects damage caused by the opponent for 1 turn",
'@Stand':"If the opponent has a rank in the Joestar category, heals the caster's HP by 4 and reduces the value of the opponent's next throw by 2",
'@SSJ2':"If the opponent has a rank of the Saiyan category, increase the value of the die by 3",
'@Jonin':"If the opponent has a rank in the Genin category, copy the maximum value of a dice roll made by the opponent and apply it to the thrower",
'@Grand Corsaire':"Si l'adversaire possède un grade de la catégorie Joestar, soigne les PDV du lanceur de 4 et réduit la valeur du prochain lancer adverse de 2",
'@SSJ':"If the opponent has a rank of the Saiyan category, increase the value of the die by 3",
'@Chunin':"Grants a second roll of the dice, the thrower can choose to keep the previous or the new one ",
'@Onde':"If the opponent has a rank in the Joestar category, heals the caster's HP by 4 and reduces the value of the opponent's next throw by 2 ",
'@Supernova':"Changement de la valeur du dé ",
'@Marine ⚓':"Changement de la valeur du dé ",
'@Speedwagon':"If the opponent has a rank in the Joestar category, heals the caster's HP by 4 and reduces the value of the opponent's next throw by 2",
'@Namek':"Changement de la valeur du dé ",
}
exports.bonus=bonus;
Big thanks to the one who's gonna try to help me!
Solution 1:[1]
Can you reach your friend's tile number in the next roll? Create a function that takes your position a and your friend's position b and returns a boolean representation of whether it's possible to earn a bonus on any dice roll.
function possBonus(a, b) {
if(a === b || a+6 < b || a > b+6 || a>b) {
return false;
}else if(a-b <=6 || b-a <=6 ) {
return true;
}
}
console.log(possBonus(5, 3))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | S Gabale |

