feat: Add roll command
parent
e4f8f523ae
commit
74b92e4fd8
@ -0,0 +1,60 @@
|
||||
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,
|
||||
})
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
crand "crypto/rand"
|
||||
mrand "math/rand"
|
||||
)
|
||||
|
||||
type src struct{}
|
||||
|
||||
func (s src) Int63() int64 {
|
||||
result := make([]byte, 8)
|
||||
_, err := crand.Read(result)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return int64(result[0]&0x7F)<<56 |
|
||||
int64(result[1])<<48 |
|
||||
int64(result[2])<<40 |
|
||||
int64(result[3])<<32 |
|
||||
int64(result[4])<<24 |
|
||||
int64(result[5])<<16 |
|
||||
int64(result[6])<<8 |
|
||||
int64(result[7])<<0
|
||||
}
|
||||
|
||||
func (s src) Seed(seed int64) {
|
||||
// We don't do seeds here.
|
||||
}
|
||||
|
||||
var Rand = mrand.New(src{})
|
Loading…
Reference in New Issue