feat(lib,commands,options): choice checking for validation

This commit is contained in:
Guz
2024-11-22 18:35:17 -03:00
parent cad54f3fe7
commit f1dc8e7421

View File

@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"slices"
"unicode/utf8"
"github.com/bwmarrin/discordgo"
)
@@ -219,6 +220,24 @@ func (o *ChatCommandIntegerOption) ApplicationCommandOption() *discordgo.Applica
}
func (o *ChatCommandIntegerOption) Validate() (bool, error) {
for _, c := range o.Choices {
if c.Value < o.MinValue {
return false, fmt.Errorf(
"Choice %q has value (%v) smaller than allowed by property \"MinValue\" (%v)",
c.Name,
c.Value,
o.MinValue,
)
} else if c.Value > o.MaxValue {
return false, fmt.Errorf(
"Choice %q has value (%v) bigger than allowed by property \"MaxValue\" (%v)",
c.Name,
c.Value,
o.MaxValue,
)
}
}
return validateOption(o)
}
@@ -303,6 +322,24 @@ func (o *ChatCommandNumberOption) ApplicationCommandOption() *discordgo.Applicat
}
func (o *ChatCommandNumberOption) Validate() (bool, error) {
for _, c := range o.Choices {
if c.Value < o.MinValue {
return false, fmt.Errorf(
"Choice %q has value (%v) smaller than allowed by property \"MinValue\" (%v)",
c.Name,
c.Value,
o.MinValue,
)
} else if c.Value > o.MaxValue {
return false, fmt.Errorf(
"Choice %q has value (%v) bigger than allowed by property \"MaxValue\" (%v)",
c.Name,
c.Value,
o.MaxValue,
)
}
}
return validateOption(o)
}
@@ -397,6 +434,27 @@ func (o *ChatCommandStringOption) Validate() (bool, error) {
)
}
for _, c := range o.Choices {
l := utf8.RuneCountInString(c.Value)
if l < o.MinLength {
return false, fmt.Errorf(
"Choice %q has value (%q) with length (%v) smaller than allowed by \"MinLength\" property (%v)",
c.Name,
l,
c.Value,
o.MinLength,
)
} else if l > o.MaxLength {
return false, fmt.Errorf(
"Choice %q has value (%q) with length (%v) bigger than allowed by \"MaxLength\" property (%v)",
c.Name,
l,
c.Value,
o.MaxLength,
)
}
}
return validateOption(o)
}