From 69448a105242d938d4903b97e03bbebe390ccbd7 Mon Sep 17 00:00:00 2001 From: Ceda EI Date: Sat, 14 Oct 2023 21:17:06 +0530 Subject: [PATCH] Add dice --- commands/dice.js | 31 +++++++++++++++++++++++++++++++ commands/index.js | 2 ++ package-lock.json | 4 ++-- static/commands_list.txt | 12 ++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 commands/dice.js diff --git a/commands/dice.js b/commands/dice.js new file mode 100644 index 0000000..5977702 --- /dev/null +++ b/commands/dice.js @@ -0,0 +1,31 @@ +function diceRoll(side) { + + return Math.ceil(Math.random() * side); + +} +module.exports = () => async (ctx) => { + + const numDice = parseInt(ctx.match[1] || "1"); + const diceSides = parseInt(ctx.match[2]); + const rolls = Array(numDice) + .fill(0) + .map(() => diceRoll(diceSides)); + const total = rolls.reduce((acc, curr) => acc + curr, 0); + const message = rolls + .map((i) => `_You roll a_ *D${diceSides}* _and get a_ *${i}*`) + .join("\n"); + let totalMessage = ""; + if (numDice > 1 || ctx.match[3]) + totalMessage = `*Total:* ${total}`; + + if (ctx.match[3]) { + + const modifier = parseInt(ctx.match[5]); + const sign = ctx.match[4]; + if (sign === "+") totalMessage += ` + ${modifier} = ${total + modifier}`; + else totalMessage += ` - ${modifier} = ${total - modifier}`; + + } + ctx.reply(message + "\n\n" + totalMessage, { parse_mode: "Markdown" }); + +}; diff --git a/commands/index.js b/commands/index.js index 2292574..4c5ba40 100644 --- a/commands/index.js +++ b/commands/index.js @@ -10,6 +10,7 @@ const info = require("./info"); const expand = require("./expand"); const roleplay = require("./roleplay"); const suggest = require("./suggest"); +const dice = require("./dice"); module.exports = (bot, [ questions, kys, insults, commands_list, words, roleplay_data ], feedback_id, apiToken, ugokiRoot, axios) => { @@ -69,6 +70,7 @@ module.exports = (bot, [ questions, kys, insults, commands_list, words, roleplay bot.command("donate", (ctx) => ctx.reply("Thanks for considering to donate." + " To support the development and hosting of Quadnite Bot, you can " + "donate here: https://liberapay.com/ceda_ei/")); + bot.hears(/(\d*)d(\d+)(\s*([-+])\s*(\d+))?/, dice()); function getGetGif(command) { diff --git a/package-lock.json b/package-lock.json index 36c4b52..e2efa57 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "quadnite-bot", - "version": "1.0.0", + "version": "2.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "quadnite-bot", - "version": "1.0.0", + "version": "2.0.0", "license": "GPL-3.0", "dependencies": { "axios": "^0.21.0", diff --git a/static/commands_list.txt b/static/commands_list.txt index 5a7a726..55cf212 100644 --- a/static/commands_list.txt +++ b/static/commands_list.txt @@ -59,3 +59,15 @@ yes help - Need help? Go here feedback - Send feedback, suggestion for kys, insult text rate - Rate me on TGDR + +Dice + +Simply mentioning a dice in a message will send the results + +[num]d[sides] [modifier] + +num is the number of the dices to roll. (Optional) +sides is the number of sides of the dice. (Required) +modifier is in the form of + or - followed by a number. (Optional) + +Examples: 1d6, 2d10 + 5, 3d20 - 2, 2d20