1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-01 04:24:19 +00:00

discord: Add SelectedValue for SelectComponents (#455)

* discord: Added SelectedValue fields for Channel, User and Role SelectComponents

* discord: Modified MentionableSelectComponent to accept both RoleID and UserID as SelectedMentions

* discord: Minor comment change

* discord: Applied [this](https://github.com/diamondburned/arikawa/pull/455#discussion_r1815822434)

* discord: Modified MentionableSelectComponent to take in DefaultMention union type

* comments: Little typo fix

* discord: Minor wordings and comment fix

* comments: Fix format
This commit is contained in:
Barani Kumar S 2024-11-01 01:42:25 +05:30 committed by GitHub
parent f8537d78bb
commit c2bbe540a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -720,6 +720,8 @@ type UserSelectComponent struct {
ValueLimits [2]int `json:"-"` ValueLimits [2]int `json:"-"`
// Disabled disables the select if true. // Disabled disables the select if true.
Disabled bool `json:"disabled,omitempty"` Disabled bool `json:"disabled,omitempty"`
// DefaultUsers is the slice of UserIDs that are marked as selected by default
DefaultUsers []UserID `json:"-"`
} }
// ID implements the Component interface. // ID implements the Component interface.
@ -737,11 +739,17 @@ func (s *UserSelectComponent) _icp() {}
func (s *UserSelectComponent) MarshalJSON() ([]byte, error) { func (s *UserSelectComponent) MarshalJSON() ([]byte, error) {
type sel UserSelectComponent type sel UserSelectComponent
type DefaultValue struct {
Id UserID `json:"id"`
Type string `json:"type"`
}
type Msg struct { type Msg struct {
Type ComponentType `json:"type"` Type ComponentType `json:"type"`
*sel *sel
MinValues *int `json:"min_values,omitempty"` MinValues *int `json:"min_values,omitempty"`
MaxValues *int `json:"max_values,omitempty"` MaxValues *int `json:"max_values,omitempty"`
DefaultValues []DefaultValue `json:"default_values,omitempty"`
} }
msg := Msg{ msg := Msg{
@ -749,6 +757,16 @@ func (s *UserSelectComponent) MarshalJSON() ([]byte, error) {
sel: (*sel)(s), sel: (*sel)(s),
} }
var defaultValues []DefaultValue
if len(s.DefaultUsers) > 0 {
for _, userId := range s.DefaultUsers {
defaultValues = append(defaultValues, DefaultValue{Id: userId, Type: "user"})
}
}
msg.DefaultValues = defaultValues
if s.ValueLimits != [2]int{0, 0} { if s.ValueLimits != [2]int{0, 0} {
msg.MinValues = new(int) msg.MinValues = new(int)
msg.MaxValues = new(int) msg.MaxValues = new(int)
@ -771,6 +789,8 @@ type RoleSelectComponent struct {
ValueLimits [2]int `json:"-"` ValueLimits [2]int `json:"-"`
// Disabled disables the select if true. // Disabled disables the select if true.
Disabled bool `json:"disabled,omitempty"` Disabled bool `json:"disabled,omitempty"`
// DefaultRoles is the slice of RoleIDs that are marked as selected by default
DefaultRoles []RoleID `json:"-"`
} }
// ID implements the Component interface. // ID implements the Component interface.
@ -788,11 +808,17 @@ func (s *RoleSelectComponent) _icp() {}
func (s *RoleSelectComponent) MarshalJSON() ([]byte, error) { func (s *RoleSelectComponent) MarshalJSON() ([]byte, error) {
type sel RoleSelectComponent type sel RoleSelectComponent
type DefaultValue struct {
Id RoleID `json:"id"`
Type string `json:"type"`
}
type Msg struct { type Msg struct {
Type ComponentType `json:"type"` Type ComponentType `json:"type"`
*sel *sel
MinValues *int `json:"min_values,omitempty"` MinValues *int `json:"min_values,omitempty"`
MaxValues *int `json:"max_values,omitempty"` MaxValues *int `json:"max_values,omitempty"`
DefaultValues []DefaultValue `json:"default_values,omitempty"`
} }
msg := Msg{ msg := Msg{
@ -800,6 +826,16 @@ func (s *RoleSelectComponent) MarshalJSON() ([]byte, error) {
sel: (*sel)(s), sel: (*sel)(s),
} }
var defaultValues []DefaultValue
if len(s.DefaultRoles) > 0 {
for _, roleId := range s.DefaultRoles {
defaultValues = append(defaultValues, DefaultValue{Id: roleId, Type: "role"})
}
}
msg.DefaultValues = defaultValues
if s.ValueLimits != [2]int{0, 0} { if s.ValueLimits != [2]int{0, 0} {
msg.MinValues = new(int) msg.MinValues = new(int)
msg.MaxValues = new(int) msg.MaxValues = new(int)
@ -811,6 +847,22 @@ func (s *RoleSelectComponent) MarshalJSON() ([]byte, error) {
return json.Marshal(msg) return json.Marshal(msg)
} }
// DefaultMention type is a Union type which packs both UserID and RoleID
type DefaultMention struct {
userId UserID `json:"-"`
roleId RoleID `json:"-"`
}
// DefaultUserMention creates a new DefaultMention type with only UserID
func DefaultUserMention (userId UserID) DefaultMention {
return DefaultMention{userId: userId}
}
// DefaultRoleMention creates a new DefaultMention type with only RoleID
func DefaultRoleMention(roleId RoleID) DefaultMention {
return DefaultMention{roleId: roleId}
}
type MentionableSelectComponent struct { type MentionableSelectComponent struct {
// CustomID is the custom unique ID. // CustomID is the custom unique ID.
CustomID ComponentID `json:"custom_id,omitempty"` CustomID ComponentID `json:"custom_id,omitempty"`
@ -822,6 +874,14 @@ type MentionableSelectComponent struct {
ValueLimits [2]int `json:"-"` ValueLimits [2]int `json:"-"`
// Disabled disables the select if true. // Disabled disables the select if true.
Disabled bool `json:"disabled,omitempty"` Disabled bool `json:"disabled,omitempty"`
// DefaultMentions is the slice of User / Role Mentions that are selected by default
// Example:
// DefaultMentions: []DefaultMention{
// discord.DefaultUserMention(0382080830233),
// discord.DefaultRoleMention(4820380382080),
// ...
// }
DefaultMentions []DefaultMention `json:"-"`
} }
// ID implements the Component interface. // ID implements the Component interface.
@ -839,11 +899,17 @@ func (s *MentionableSelectComponent) _icp() {}
func (s *MentionableSelectComponent) MarshalJSON() ([]byte, error) { func (s *MentionableSelectComponent) MarshalJSON() ([]byte, error) {
type sel MentionableSelectComponent type sel MentionableSelectComponent
type DefaultValue struct {
Id Snowflake `json:"id"`
Type string `json:"type"`
}
type Msg struct { type Msg struct {
Type ComponentType `json:"type"` Type ComponentType `json:"type"`
*sel *sel
MinValues *int `json:"min_values,omitempty"` MinValues *int `json:"min_values,omitempty"`
MaxValues *int `json:"max_values,omitempty"` MaxValues *int `json:"max_values,omitempty"`
DefaultValues []DefaultValue `json:"default_values,omitempty"`
} }
msg := Msg{ msg := Msg{
@ -851,6 +917,23 @@ func (s *MentionableSelectComponent) MarshalJSON() ([]byte, error) {
sel: (*sel)(s), sel: (*sel)(s),
} }
var defaultValues []DefaultValue
if len(s.DefaultMentions) > 0 {
for _, mention := range s.DefaultMentions {
if mention.userId.IsValid() {
defaultValues =
append(defaultValues, DefaultValue{Id: Snowflake(mention.userId), Type: "user"})
}
if mention.roleId.IsValid() {
defaultValues =
append(defaultValues, DefaultValue{Id: Snowflake(mention.roleId), Type: "role"})
}
}
}
msg.DefaultValues = defaultValues
if s.ValueLimits != [2]int{0, 0} { if s.ValueLimits != [2]int{0, 0} {
msg.MinValues = new(int) msg.MinValues = new(int)
msg.MaxValues = new(int) msg.MaxValues = new(int)
@ -875,6 +958,8 @@ type ChannelSelectComponent struct {
Disabled bool `json:"disabled,omitempty"` Disabled bool `json:"disabled,omitempty"`
// ChannelTypes is the types of channels that can be chosen from. // ChannelTypes is the types of channels that can be chosen from.
ChannelTypes []ChannelType `json:"channel_types,omitempty"` ChannelTypes []ChannelType `json:"channel_types,omitempty"`
// DefaultChannels is the list of channels that are marked as selected by default.
DefaultChannels []ChannelID `json:"-"`
} }
// ID implements the Component interface. // ID implements the Component interface.
@ -892,11 +977,17 @@ func (s *ChannelSelectComponent) _icp() {}
func (s *ChannelSelectComponent) MarshalJSON() ([]byte, error) { func (s *ChannelSelectComponent) MarshalJSON() ([]byte, error) {
type sel ChannelSelectComponent type sel ChannelSelectComponent
type DefaultValue struct {
Id ChannelID `json:"id"`
Type string `json:"type"`
}
type Msg struct { type Msg struct {
Type ComponentType `json:"type"` Type ComponentType `json:"type"`
*sel *sel
MinValues *int `json:"min_values,omitempty"` MinValues *int `json:"min_values,omitempty"`
MaxValues *int `json:"max_values,omitempty"` MaxValues *int `json:"max_values,omitempty"`
DefaultValues []DefaultValue `json:"default_values,omitempty"`
} }
msg := Msg{ msg := Msg{
@ -904,6 +995,16 @@ func (s *ChannelSelectComponent) MarshalJSON() ([]byte, error) {
sel: (*sel)(s), sel: (*sel)(s),
} }
var defaultValues []DefaultValue
if len(s.DefaultChannels) > 0 {
for _, channelId := range s.DefaultChannels {
defaultValues = append(defaultValues, DefaultValue{Id: channelId, Type: "channel"})
}
}
msg.DefaultValues = defaultValues
if s.ValueLimits != [2]int{0, 0} { if s.ValueLimits != [2]int{0, 0} {
msg.MinValues = new(int) msg.MinValues = new(int)
msg.MaxValues = new(int) msg.MaxValues = new(int)