Added dice rolling command!

pull/20/head
pocketjawa 2020-05-01 01:46:47 -04:00
parent 1f9dc16e3e
commit e8aa115dfa
2 changed files with 44 additions and 0 deletions

View File

@ -163,3 +163,46 @@ func setNowPlaying(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context)
}
s.ChannelMessageSend(m.ChannelID, resp)
}
//Roll any number of dice of the same kind
func diceRoller(dienum, diesides int) (total int, diceroles []string) {
for 0 < dienum {
dienum--
rollresult := rand.Intn(diesides) + 1
total += rollresult
diceroles = append(diceroles, "D"+strconv.Itoa(diesides)+": **"+strconv.Itoa(rollresult)+"**")
}
return
}
//Dice rolling command
func rollDice(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) {
var resp string
var diceroles []string
var dicetotal int
var matchDicePattern = regexp.MustCompile(`^[0-9]*[Dd][1-9][0-9]*$`)
var args = ctx.Fields[1:]
for i := range args {
if matchDicePattern.MatchString(args[i]) {
dLocation := strings.Index(strings.ToLower(args[i]), "d")
dienum, _ := strconv.Atoi(args[i][:dLocation])
diesides, _ := strconv.Atoi(args[i][dLocation+1:])
if dienum == 0 {
dienum = 1
}
resultsTotal, resultsRolls := diceRoller(dienum, diesides)
if dienum > 25 {
diceroles = append(diceroles, strconv.Itoa(dienum)+"*D"+strconv.Itoa(diesides)+": **"+strconv.Itoa(resultsTotal)+"**")
} else {
diceroles = append(diceroles, resultsRolls...)
}
dicetotal += resultsTotal
}
}
results := strings.Join(diceroles, ", ")
resp = m.Author.Username + " rolled " + results + ", for a total of **" + strconv.Itoa(dicetotal) + "**!"
if dicetotal == 0 {
resp = "Oops, looks like no dice were rolled! Try something like `!roll d20`, `!roll 2d6`, or `!roll 3d6 d20` instead."
}
s.ChannelMessageSend(m.ChannelID, resp)
}

View File

@ -69,6 +69,7 @@ func main() {
Router.Route("kill", "Restart the bot.", restartBot)
Router.Route("sleep", "Something about sleeping...", goToSleep)
Router.Route("setplaying", "Set the nowplaying message for the bot.", setNowPlaying)
Router.Route("roll", "Roll some dice!", rollDice)
//Open a connection to Discord
err = Session.Open()