Files
velox-bot/internal/commands/fun/public/dice.go
T

63 lines
1.4 KiB
Go

package public
import (
"strings"
"velox-bot/internal/commands/fun/shared"
"velox-bot/internal/diceexpr"
"github.com/bwmarrin/discordgo"
)
var Dice = &discordgo.ApplicationCommand{
Name: "dice",
Description: "Roll dice expressions (e.g. d20, 2d20+1, 2#d20+1)",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "expr",
Description: "Dice expression to roll",
Required: true,
},
},
Contexts: &[]discordgo.InteractionContextType{
discordgo.InteractionContextGuild,
discordgo.InteractionContextBotDM,
discordgo.InteractionContextPrivateChannel,
},
}
func DiceHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
expr := ""
if opts := i.ApplicationCommandData().Options; len(opts) > 0 {
expr = opts[0].StringValue()
}
expr = strings.TrimSpace(expr)
if expr == "" {
respondDiceFailure(s, i)
return
}
res, err := diceexpr.EvalMany(expr, diceexpr.DefaultLimits())
if err != nil {
respondDiceFailure(s, i)
return
}
out := diceexpr.FormatResult(res, diceexpr.DefaultFormatOptions())
if len(out) > 1900 {
// Keep within Discord message limits; trim safely.
out = out[:1900] + "…"
}
shared.Respond(s, i, out)
}
func respondDiceFailure(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.GuildID != "" {
shared.RespondEphemeral(s, i, "Failure!")
return
}
shared.Respond(s, i, "Failure!")
}