Skip to content

Commit

Permalink
discord: Modified MentionableSelectComponent to accept both RoleID an…
Browse files Browse the repository at this point in the history
…d UserID as SelectedMentions
  • Loading branch information
CosmicPredator committed Oct 24, 2024
1 parent 431d83d commit 744c10f
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions discord/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -737,18 +739,34 @@ 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{
Type: UserSelectComponentType,
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)
Expand Down Expand Up @@ -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.
Expand All @@ -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"`
}

Expand All @@ -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"})
}
}
}

Expand Down

0 comments on commit 744c10f

Please sign in to comment.