From 744c10f330a5858f2f6d0b313bc9db861b76500a Mon Sep 17 00:00:00 2001 From: Barani Kumar S Date: Thu, 24 Oct 2024 22:41:30 +0530 Subject: [PATCH] discord: Modified MentionableSelectComponent to accept both RoleID and UserID as SelectedMentions --- discord/component.go | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/discord/component.go b/discord/component.go index ea0de3e1..90777e62 100644 --- a/discord/component.go +++ b/discord/component.go @@ -720,6 +720,8 @@ type UserSelectComponent struct { ValueLimits [2]int `json:"-"` // Disabled disables the select if true. Disabled bool `json:"disabled,omitempty"` + // SelectedUsers is the slice of UserIDs that are marked as selected by default + SelectedUsers []UserID `json:"-"` } // ID implements the Component interface. @@ -737,11 +739,17 @@ func (s *UserSelectComponent) _icp() {} func (s *UserSelectComponent) MarshalJSON() ([]byte, error) { type sel UserSelectComponent + type DefaultValue struct { + Id UserID `json:"id"` + Type string `json:"type"` + } + type Msg struct { Type ComponentType `json:"type"` *sel MinValues *int `json:"min_values,omitempty"` MaxValues *int `json:"max_values,omitempty"` + DefaultValues []DefaultValue `json:"default_values,omitempty"` } msg := Msg{ @@ -749,6 +757,16 @@ func (s *UserSelectComponent) MarshalJSON() ([]byte, error) { sel: (*sel)(s), } + var defaultValues []DefaultValue + + if len(s.SelectedUsers) > 0 { + for _, userId := range s.SelectedUsers { + defaultValues = append(defaultValues, DefaultValue{Id: userId, Type: "user"}) + } + } + + msg.DefaultValues = defaultValues + if s.ValueLimits != [2]int{0, 0} { msg.MinValues = new(int) msg.MaxValues = new(int) @@ -840,8 +858,9 @@ type MentionableSelectComponent struct { ValueLimits [2]int `json:"-"` // Disabled disables the select if true. Disabled bool `json:"disabled,omitempty"` - // SelectedUsers is the slice of UserIDs that are marked as selected by default - SelectedUsers []UserID `json:"-"` + // DefaultMentions is the slice of discord.UserID's and discord.RoleID's + // that are marked as selected by default + SelectedMentions []interface{} `json:"-"` } // ID implements the Component interface. @@ -860,7 +879,7 @@ func (s *MentionableSelectComponent) MarshalJSON() ([]byte, error) { type sel MentionableSelectComponent type DefaultValue struct { - Id UserID `json:"id"` + Id Snowflake `json:"id"` Type string `json:"type"` } @@ -879,9 +898,16 @@ func (s *MentionableSelectComponent) MarshalJSON() ([]byte, error) { var defaultValues []DefaultValue - if len(s.SelectedUsers) > 0 { - for _, userId := range s.SelectedUsers { - defaultValues = append(defaultValues, DefaultValue{Id: userId, Type: "user"}) + if len(s.SelectedMentions) > 0 { + for _, mentionId := range s.SelectedMentions { + switch id := mentionId.(type) { + case UserID: + defaultValues = + append(defaultValues, DefaultValue{Id: Snowflake(id), Type: "user"}) + case RoleID: + defaultValues = + append(defaultValues, DefaultValue{Id: Snowflake(id), Type: "role"}) + } } }