MCPcopy
hub / github.com/codebymitch/TitanBot / execute

Function execute

src/commands/Fun/roll.js:20–87  ·  view source on GitHub ↗
(interaction, config, client)

Source from the content-addressed store, hash-verified

18 category: 'Fun',
19
20 async execute(interaction, config, client) {
21 try {
22 await InteractionHelper.safeDefer(interaction);
23
24 const notation = interaction.options
25 .getString("notation")
26 .toLowerCase()
27 .replace(/\s/g, "");
28
29 const match = notation.match(/^(\d*)d(\d+)([\+\-]\d+)?$/);
30
31 if (!match) {
32 throw new TitanBotError(
33 `Invalid dice notation: ${notation}`,
34 ErrorTypes.USER_INPUT,
35 'Invalid notation. Use format like `1d20` or `3d6+5`.'
36 );
37 }
38
39 const numDice = parseInt(match[1] || "1", 10);
40 const numSides = parseInt(match[2], 10);
41 const modifier = parseInt(match[3] || "0", 10);
42
43 if (numDice < 1 || numDice > 20) {
44 throw new TitanBotError(
45 `Too many dice requested: ${numDice}`,
46 ErrorTypes.VALIDATION,
47 'Please keep the number of dice between 1 and 20.'
48 );
49 }
50
51 if (numSides < 1 || numSides > 1000) {
52 throw new TitanBotError(
53 `Invalid number of sides: ${numSides}`,
54 ErrorTypes.VALIDATION,
55 'Please keep the number of sides between 1 and 1000.'
56 );
57 }
58
59 let rolls = [];
60 let totalRoll = 0;
61
62 for (let i = 0; i < numDice; i++) {
63 const roll = Math.floor(Math.random() * numSides) + 1;
64 rolls.push(roll);
65 totalRoll += roll;
66 }
67
68 const finalTotal = totalRoll + modifier;
69
70 const resultsDetail =
71 numDice > 1 ? `**Rolls:** ${rolls.join(" + ")}\n` : "";
72 const modifierText = modifier !== 0 ? `+ (${modifier})` : "";
73
74 const embed = successEmbed(
75 `🎲 Rolling ${numDice}d${numSides}${modifier !== 0 ? match[3] : ""}`,
76 `${resultsDetail}**Total Roll:** ${totalRoll}${modifierText} = **${finalTotal}**`,
77 );

Callers

nothing calls this directly

Calls 4

successEmbedFunction · 0.90
handleInteractionErrorFunction · 0.90
safeDeferMethod · 0.80
safeEditReplyMethod · 0.80

Tested by

no test coverage detected