61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package commands
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"gitea.teamortix.com/chanbakjsd/Milen/util"
|
|
)
|
|
|
|
func handleRoll(dg *discordgo.Session, m *discordgo.MessageCreate, arguments []string) {
|
|
if len(arguments) > 1 {
|
|
util.SendFailEmbed(dg, m.ChannelID, "Error", "Expected zero or one argument.")
|
|
return
|
|
}
|
|
var argument string
|
|
if len(arguments) == 0 {
|
|
argument = "6"
|
|
} else {
|
|
argument = arguments[0]
|
|
}
|
|
|
|
split := strings.SplitN(argument, "d", 2)
|
|
if len(split) == 1 {
|
|
split = []string{"1", split[0]}
|
|
}
|
|
if split[0] == "" {
|
|
split[0] = "1"
|
|
}
|
|
|
|
numberOfDice, err := strconv.Atoi(split[0])
|
|
if err != nil || numberOfDice < 1 {
|
|
util.SendFailEmbed(dg, m.ChannelID, "Error", "Unknown dice format. Expected `[num]d[amount of sides]`.")
|
|
return
|
|
}
|
|
if numberOfDice > 5 {
|
|
util.SendFailEmbed(dg, m.ChannelID, "Error", "I only have five dice.")
|
|
return
|
|
}
|
|
numberOfSides, err := strconv.Atoi(split[1])
|
|
if err != nil || numberOfSides < 1 {
|
|
util.SendFailEmbed(dg, m.ChannelID, "Error", "Unknown dice format. Expected `[num]d[amount of sides]`.")
|
|
return
|
|
}
|
|
if numberOfSides > 10000 {
|
|
util.SendFailEmbed(dg, m.ChannelID, "Error", "The largest dice I have only has 10000 sides.")
|
|
return
|
|
}
|
|
|
|
result := make([]string, 0, numberOfDice)
|
|
for i := 0; i < numberOfDice; i++ {
|
|
result = append(result, strconv.Itoa(util.Rand.Intn(numberOfSides)+1))
|
|
}
|
|
util.SendEmbed(dg, m.ChannelID, &discordgo.MessageEmbed{
|
|
Title: "Roll Results",
|
|
Description: "Your roll results are: " + util.FormatCommaSeparatedList(result),
|
|
Color: 0x00D000,
|
|
})
|
|
}
|