From 0ab0bcac22ba5576c123dad45382a0b1f68eb281 Mon Sep 17 00:00:00 2001 From: Nemo Date: Sat, 13 Nov 2021 22:58:00 +0100 Subject: [PATCH] =?UTF-8?q?ajout=20de=20la=20fonctionnalit=C3=A9=20roll?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/roll.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 commands/roll.js diff --git a/commands/roll.js b/commands/roll.js new file mode 100644 index 0000000..d77081d --- /dev/null +++ b/commands/roll.js @@ -0,0 +1,49 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('roll') + .setDescription('Roll the dice, seal your fate.') + .addStringOption(option => + option.setName('dice') + .setDescription('*ex: /roll 1d6 - max 100 faces - max 15 dés') + .setRequired(true) + ), + async execute(interaction) { + const option = interaction.options.get('dice'); + const regex = /^([0-9]){1,}([dD])([0-9]{1,})$/; + if(!option.value.match(regex)){ + await interaction.reply({content: 'Veuillez entrer le bon format - *ex: 1d6*', ephemeral: true}); + return; + } + + const args = option.value.split(/[dD]/); + const num = parseInt(args[0]); + const dice = parseInt(args[1]); + + if(num < 1 || num > 15){ + await interaction.reply({content: 'Veuillez entrer un nombre de dé compris entre 1 et 15 - *ex: 1d6*', ephemeral: true}); + return; + } + + if(dice < 1 || dice > 100){ + await interaction.reply({content: 'Veuillez entrer un nombre de dé compris entre 1 et 15 - *ex: 1d6*', ephemeral: true}); + return; + } + + var reply = num + " dés de " + dice + " faces lancés :\t"; + var total = 0; + var dices = "" + + for(var i = 0; i < num; i++){ + var roll = 1 + Math.floor(Math.random() * (dice)); + total += roll; + dices += " 🎲 **" + roll + "** "; + } + + reply += "*total de* **" + total + "**\n" + dices; + + await interaction.reply(reply); + }, +}; +