1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-02 11:52:56 +00:00

discord: Add ChannelType field to CommandOption (#279)

* discord: add ChannelType field to CommandOption

* preallocate option.ChannelTypes

Co-authored-by: diamondburned <diamond@arikawa-hi.me>

Co-authored-by: diamondburned <diamond@arikawa-hi.me>
This commit is contained in:
starshines 2021-09-28 23:12:04 +02:00 committed by GitHub
parent ca70e0cca9
commit 8ea320ccb9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -97,6 +97,45 @@ type CommandOption struct {
Required bool `json:"required"`
Choices []CommandOptionChoice `json:"choices,omitempty"`
Options []CommandOption `json:"options,omitempty"`
// If this option is a channel type, the channels shown will be restricted to these types
ChannelTypes []ChannelType `json:"-"`
}
func (c CommandOption) MarshalJSON() ([]byte, error) {
type RawOption CommandOption
option := struct {
RawOption
ChannelTypes []uint16 `json:"channel_types,omitempty"`
}{RawOption: RawOption(c)}
// []uint8 is marshalled as a base64 string, so we marshal a []uint64 instead.
if len(c.ChannelTypes) > 0 {
option.ChannelTypes = make([]uint16, 0, len(c.ChannelTypes))
for _, t := range c.ChannelTypes {
option.ChannelTypes = append(option.ChannelTypes, uint16(t))
}
}
return json.Marshal(option)
}
func (c *CommandOption) UnmarshalJSON(data []byte) error {
type RawOption CommandOption
cmd := struct {
*RawOption
ChannelTypes []uint16 `json:"channel_types,omitempty"`
}{RawOption: (*RawOption)(c)}
if err := json.Unmarshal(data, &cmd); err != nil {
return err
}
c.ChannelTypes = make([]ChannelType, 0, len(cmd.ChannelTypes))
for _, t := range cmd.ChannelTypes {
c.ChannelTypes = append(c.ChannelTypes, ChannelType(t))
}
return nil
}
type CommandOptionType uint