Provides some common dice rolling functions.
About Abbot
Abbot is a programmable bot that turns your team chat into a shared command center. We handle all the boilerplate of building and running these conmmands so that you can focus on making tools that help you ship faster.
We built Abbot because we saw the power of this style of work (called ChatOps), when we worked at GitHub. ChatOps made it possible for GitHub to work productively without meetings, while globally distributed. We think it’s a pretty great way to work, so we made it easy to use in Slack, Discord, and Microsoft Teams.
You can read more about Abbot here, check out our blog, or take a look at some of the other cool packages available as a one-click install from Abbot’s Package Directory.
README
This package provides some helpful dice-rolling features for Abbot. Try inputting your favorite dice expression (I’m partial to 3d4+1
) and see what results!
Code
function roll(die, qty = 1, mod = 0) {
let rolls = []
let total = Number(mod)
for (let i = 0; i < qty; i++) {
let result = Math.floor(Math.random() * die) + 1
total += result
rolls.push(result)
}
return { total, rolls }
}
function buildMessage(total, rolls) {
return `rolled a ${total} (${rolls.join(‘, ‘)})`
}
let matcher = /(\d+)?d(\d+)([\+\-]\d+)?/i
let matches = bot.arguments.match(matcher)
if (matches == null) {
bot.reply(‘Hmm…I\’m not sure that\’s how you roll. Try something like `d8` or `3d6-1`. 🎲’)
return
}
try {
let [ , qty, die, mod ] = matches
if (qty > 50) {
bot.reply(‘Sorry: I can\’t roll that many dice at once. Here\’s the first 50, though: 😅’)
qty = 50
}
let result = roll(die, qty, mod)
bot.reply(buildMessage(result.total, result.rolls))
} catch(ex) {
bot.reply(‘Egads! Something unexpected happened. 😖’)
}
})();