Selaa lähdekoodia

feat: min and max constraints for slash command options (#1026)

* feat: min and max constraints for slash command options

* fix: typo

* feat: changed slash command options min-max values to float

* feat: improved min max constraints

* feat: removed helper functions

* fix(examples): helper functions

* feat(components): made MaxValues int in SelectMenu

* fix(examples/components): MaxValues

* feat(interactions#ApplicationCommandOption): removed pointer from MaxValue

* feat(examples): removed helper functions for MinValue and MinValues

* fix(examples): removed newOptionalInt
Fedor Lapshin 2 vuotta sitten
vanhempi
commit
a4d694738f
4 muutettua tiedostoa jossa 12 lisäystä ja 2 poistoa
  1. 1 1
      components.go
  2. 2 1
      examples/components/main.go
  3. 4 0
      examples/slash_commands/main.go
  4. 5 0
      interactions.go

+ 1 - 1
components.go

@@ -175,7 +175,7 @@ type SelectMenu struct {
 	// The text which will be shown in the menu if there's no default options or all options was deselected and component was closed.
 	Placeholder string `json:"placeholder"`
 	// This value determines the minimal amount of selected items in the menu.
-	MinValues int `json:"min_values,omitempty"`
+	MinValues *int `json:"min_values,omitempty"`
 	// This value determines the maximal amount of selected items in the menu.
 	// If MaxValues or MinValues are greater than one then the user can select multiple items in the component.
 	MaxValues int                `json:"max_values,omitempty"`

+ 2 - 1
examples/components/main.go

@@ -314,6 +314,7 @@ var (
 					},
 				}
 			case "multi":
+				minValues := 1
 				response = &discordgo.InteractionResponse{
 					Type: discordgo.InteractionResponseChannelMessageWithSource,
 					Data: &discordgo.InteractionResponseData{
@@ -328,7 +329,7 @@ var (
 										Placeholder: "Select tags to search on StackOverflow",
 										// This is where confusion comes from. If you don't specify these things you will get single item select.
 										// These fields control the minimum and maximum amount of selected items.
-										MinValues: 1,
+										MinValues: &minValues,
 										MaxValues: 3,
 										Options: []discordgo.SelectMenuOption{
 											{

+ 4 - 0
examples/slash_commands/main.go

@@ -32,6 +32,8 @@ func init() {
 }
 
 var (
+	integerOptionMinValue = 1.0
+
 	commands = []*discordgo.ApplicationCommand{
 		{
 			Name: "basic-command",
@@ -59,6 +61,8 @@ var (
 					Type:        discordgo.ApplicationCommandOptionInteger,
 					Name:        "integer-option",
 					Description: "Integer option",
+					MinValue:    &integerOptionMinValue,
+					MaxValue:    10,
 					Required:    true,
 				},
 				{

+ 5 - 0
interactions.go

@@ -94,6 +94,7 @@ type ApplicationCommandOption struct {
 	// NOTE: This feature was on the API, but at some point developers decided to remove it.
 	// So I commented it, until it will be officially on the docs.
 	// Default     bool                              `json:"default"`
+
 	ChannelTypes []ChannelType               `json:"channel_types"`
 	Required     bool                        `json:"required"`
 	Options      []*ApplicationCommandOption `json:"options"`
@@ -101,6 +102,10 @@ type ApplicationCommandOption struct {
 	// NOTE: mutually exclusive with Choices.
 	Autocomplete bool                              `json:"autocomplete"`
 	Choices      []*ApplicationCommandOptionChoice `json:"choices"`
+	// Minimal value of number/integer option.
+	MinValue *float64 `json:"min_value,omitempty"`
+	// Maximum value of number/integer option.
+	MaxValue float64 `json:"max_value,omitempty"`
 }
 
 // ApplicationCommandOptionChoice represents a slash command option choice.