82 lines
2.8 KiB
JavaScript
82 lines
2.8 KiB
JavaScript
const { SlashCommandBuilder } = require('discord.js');
|
|
const { AttachmentBuilder, EmbedBuilder } = require('discord.js');
|
|
|
|
module.exports = {
|
|
data: new SlashCommandBuilder()
|
|
.setName('roll')
|
|
.setDescription('Lance un dé!')
|
|
.addStringOption(option =>
|
|
option
|
|
.setName('jet')
|
|
.setDescription('Jet du dé (par ex: 2d6)')
|
|
.setRequired(true)),
|
|
async execute(interaction) {
|
|
await interaction.deferReply();
|
|
|
|
// dice argument
|
|
let jet = interaction.options.getString('jet');
|
|
|
|
// REGEX FORMAT DICE RULE (i.e. 4d6, 10d12, 9d10, 10d5...)
|
|
const diceRule = /^\d+d\d+$/;
|
|
|
|
// dice format check
|
|
if (!diceRule.test(jet)){
|
|
interaction.editReply("Désolé, mais le format du jet est dégueulasse...");
|
|
return;
|
|
}
|
|
|
|
// getting dice values
|
|
let dice = jet.split('d');
|
|
let nbDice = dice[0];
|
|
let typeDice = dice[1];
|
|
|
|
// negative or null dice values check
|
|
if (nbDice <= 0 || typeDice <= 0) {
|
|
interaction.editReply("Désolé, mais le format du jet est dégueulasse... (comment ça zéro ?)");
|
|
return;
|
|
}
|
|
|
|
// constructing path to the image
|
|
let pathThumbnail = "";
|
|
if (typeDice == 4 || typeDice == 6 || typeDice == 8 || typeDice == 10 || typeDice == 12) {
|
|
pathThumbnail = 'dice_icons/d' + typeDice.toString() + '.png'
|
|
} else {
|
|
pathThumbnail = 'dice_icons/default.png'
|
|
}
|
|
// attachment of the image
|
|
const file = new AttachmentBuilder(pathThumbnail, { name : 'dice.png' });
|
|
|
|
// initializing result vars
|
|
let result = 0;
|
|
let resultText = "";
|
|
for (let i = 0 ; i < nbDice ; i++) {
|
|
let tmp = getRandomInt(typeDice)
|
|
result += tmp;
|
|
resultText += tmp.toString()
|
|
if (i != nbDice-1) {
|
|
resultText += ' + '
|
|
}
|
|
}
|
|
|
|
// replying embeded message
|
|
interaction.editReply({ embeds:
|
|
[
|
|
new EmbedBuilder()
|
|
.setTitle("Lancer de dé")
|
|
.setURL("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
|
|
.setThumbnail('attachment://dice.png') // trouver la bonne image
|
|
.addFields(
|
|
{ name: "Vous avez lancé...", value: jet.toString() },
|
|
{ name: "Vos jets :", value: resultText },
|
|
{ name: "Votre résultat est...", value: result.toString() }
|
|
)
|
|
// .setImage('attachment://'+file)
|
|
], files: [file]
|
|
});
|
|
},
|
|
};
|
|
|
|
// get random integer from 1 to max included
|
|
function getRandomInt(max) {
|
|
return Math.floor(Math.random() * (max-1))+1;
|
|
} |