Compare commits

...

2 Commits

Author SHA1 Message Date
Ceda EI d844ac8455 Make dice command case-insensitive 2023-10-14 23:00:15 +05:30
Ceda EI 69448a1052 Add dice 2023-10-14 21:17:06 +05:30
4 changed files with 47 additions and 2 deletions

31
commands/dice.js Normal file
View File

@ -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" });
};

View File

@ -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+))?/i, dice());
function getGetGif(command) {

4
package-lock.json generated
View File

@ -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",

View File

@ -59,3 +59,15 @@ yes
help - Need help? Go here
feedback - Send feedback, suggestion for kys, insult text
rate - Rate me on TGDR
<b>Dice</b>
Simply mentioning a dice in a message will send the results
<code>[num]d[sides] [modifier]</code>
<i>num</i> is the number of the dices to roll. (Optional)
<i>sides</i> is the number of sides of the dice. (Required)
<i>modifier</i> is in the form of + or - followed by a number. (Optional)
<i>Examples</i>: 1d6, 2d10 + 5, 3d20 - 2, 2d20