Added dice rolling command!
parent
1f9dc16e3e
commit
e8aa115dfa
43
commands.go
43
commands.go
|
@ -163,3 +163,46 @@ func setNowPlaying(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context)
|
||||||
}
|
}
|
||||||
s.ChannelMessageSend(m.ChannelID, resp)
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -69,6 +69,7 @@ func main() {
|
||||||
Router.Route("kill", "Restart the bot.", restartBot)
|
Router.Route("kill", "Restart the bot.", restartBot)
|
||||||
Router.Route("sleep", "Something about sleeping...", goToSleep)
|
Router.Route("sleep", "Something about sleeping...", goToSleep)
|
||||||
Router.Route("setplaying", "Set the nowplaying message for the bot.", setNowPlaying)
|
Router.Route("setplaying", "Set the nowplaying message for the bot.", setNowPlaying)
|
||||||
|
Router.Route("roll", "Roll some dice!", rollDice)
|
||||||
|
|
||||||
//Open a connection to Discord
|
//Open a connection to Discord
|
||||||
err = Session.Open()
|
err = Session.Open()
|
||||||
|
|
Loading…
Reference in New Issue