Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discord: Added SelectedValue fields for Channel, Mentionable, User and Role SelectComponents #455

Open
wants to merge 4 commits into
base: v3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 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"`
// DefaultUsers is the slice of UserIDs that are marked as selected by default
DefaultUsers []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.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} {
msg.MinValues = new(int)
msg.MaxValues = new(int)
Expand All @@ -771,6 +789,8 @@ type RoleSelectComponent struct {
ValueLimits [2]int `json:"-"`
// Disabled disables the select if true.
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.
Expand All @@ -788,18 +808,34 @@ func (s *RoleSelectComponent) _icp() {}
func (s *RoleSelectComponent) MarshalJSON() ([]byte, error) {
type sel RoleSelectComponent

type DefaultValue struct {
Id RoleID `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: RoleSelectComponentType,
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} {
msg.MinValues = new(int)
msg.MaxValues = new(int)
Expand All @@ -822,6 +858,11 @@ type MentionableSelectComponent struct {
ValueLimits [2]int `json:"-"`
// Disabled disables the select if true.
Disabled bool `json:"disabled,omitempty"`
// DefaultMentions is the slice of discord.UserID's and discord.RoleID's
// that are marked as selected by default
// Example:
// DefaultMentions: []interface{}{ discord.RoleID(78402180381208302), discord.UserID(87028080234556) }
DefaultMentions []interface{} `json:"-"`
}

// ID implements the Component interface.
Expand All @@ -839,18 +880,41 @@ func (s *MentionableSelectComponent) _icp() {}
func (s *MentionableSelectComponent) MarshalJSON() ([]byte, error) {
type sel MentionableSelectComponent

type DefaultValue struct {
Id Snowflake `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: MentionableSelectComponentType,
sel: (*sel)(s),
}

var defaultValues []DefaultValue

if len(s.DefaultMentions) > 0 {
for _, mentionId := range s.DefaultMentions {
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"})
}
}
}

msg.DefaultValues = defaultValues

if s.ValueLimits != [2]int{0, 0} {
msg.MinValues = new(int)
msg.MaxValues = new(int)
Expand All @@ -875,6 +939,8 @@ type ChannelSelectComponent struct {
Disabled bool `json:"disabled,omitempty"`
// ChannelTypes is the types of channels that can be chosen from.
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.
Expand All @@ -892,18 +958,34 @@ func (s *ChannelSelectComponent) _icp() {}
func (s *ChannelSelectComponent) MarshalJSON() ([]byte, error) {
type sel ChannelSelectComponent

type DefaultValue struct {
Id ChannelID `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: ChannelSelectComponentType,
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} {
msg.MinValues = new(int)
msg.MaxValues = new(int)
Expand Down