1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-27 09:12:53 +00:00

discord: Improve union interface documentation; fix *Option JSON

This commit is contained in:
diamondburned 2021-11-25 15:02:24 -08:00
parent 5c88317130
commit 8d78221de0
No known key found for this signature in database
GPG key ID: D78C4471CE776659
4 changed files with 102 additions and 19 deletions

View file

@ -255,6 +255,20 @@ const (
// CommandOption is a union of command option types. The constructors for
// CommandOption will hint the types that can be a CommandOption.
//
// The following types implement this interface:
//
// - *SubcommandGroupOption
// - *SubcommandOption
// - *StringOption
// - *IntegerOption
// - *BooleanOption
// - *UserOption
// - *ChannelOption
// - *RoleOption
// - *MentionableOption
// - *NumberOption
//
type CommandOption interface {
Name() string
Type() CommandOptionType
@ -329,6 +343,18 @@ func (s *SubcommandOption) UnmarshalJSON(b []byte) error {
}
// CommandOptionValue is a subcommand option that fits into a subcommand.
//
// The following types implement this interface:
//
// - *StringOption
// - *IntegerOption
// - *BooleanOption
// - *UserOption
// - *ChannelOption
// - *RoleOption
// - *MentionableOption
// - *NumberOption
//
type CommandOptionValue interface {
CommandOption
_val()
@ -641,3 +667,39 @@ func (u *UserOption) MarshalJSON() ([]byte, error) {
raw: (*raw)(u),
})
}
// MarshalJSON marshals ChannelOption to JSON with the "type" field.
func (c *ChannelOption) MarshalJSON() ([]byte, error) {
type raw ChannelOption
return json.Marshal(struct {
Type CommandOptionType `json:"type"`
*raw
}{
Type: c.Type(),
raw: (*raw)(c),
})
}
// MarshalJSON marshals RoleOption to JSON with the "type" field.
func (r *RoleOption) MarshalJSON() ([]byte, error) {
type raw RoleOption
return json.Marshal(struct {
Type CommandOptionType `json:"type"`
*raw
}{
Type: r.Type(),
raw: (*raw)(r),
})
}
// MarshalJSON marshals MentionableOption to JSON with the "type" field.
func (m *MentionableOption) MarshalJSON() ([]byte, error) {
type raw MentionableOption
return json.Marshal(struct {
Type CommandOptionType `json:"type"`
*raw
}{
Type: m.Type(),
raw: (*raw)(m),
})
}

View file

@ -64,6 +64,13 @@ func (c *ContainerComponents) UnmarshalJSON(b []byte) error {
// Component is a component that can be attached to an interaction response. A
// Component is either an InteractiveComponent or a ContainerComponent. See
// those appropriate types for more information.
//
// The following types satisfy this interface:
//
// - *ActionRowComponent
// - *ButtonComponent
// - *SelectComponent
//
type Component interface {
// Type returns the type of the underlying component.
Type() ComponentType
@ -73,6 +80,12 @@ type Component interface {
// InteractiveComponent extends the Component for components that are
// interactible, or components that aren't containers (like ActionRow). This is
// useful for ActionRow to type-check that no nested ActionRows are allowed.
//
// The following types satisfy this interface:
//
// - *ButtonComponent
// - *SelectComponent
//
type InteractiveComponent interface {
Component
// ID returns the ID of the underlying component.
@ -83,6 +96,11 @@ type InteractiveComponent interface {
// ContainerComponent is the opposite of InteractiveComponent: it describes
// components that only contain other components. The only component that
// satisfies that is ActionRow.
//
// The following types satisfy this interface:
//
// - *ActionRowComponent
//
type ContainerComponent interface {
Component
_ctn()

View file

@ -132,8 +132,16 @@ const (
// InteractionData holds the respose data of an interaction, or more
// specifically, the data that Discord sends to us. Type assertions should be
// made on it to access the underlying data. The underlying types of the
// Responses are value types. See the constructors for the possible types.
// made on it to access the underlying data.
//
// The following types implement this interface:
//
// - *PingInteraction
// - *AutocompleteInteraction
// - *CommandInteraction
// - *SelectInteraction (also ComponentInteraction)
// - *ButtonInteraction (also ComponentInteraction)
//
type InteractionData interface {
InteractionType() InteractionDataType
data()
@ -173,8 +181,13 @@ func (*AutocompleteInteraction) InteractionType() InteractionDataType {
func (*AutocompleteInteraction) data() {}
// ComponentInteraction is a union component interaction response types. The
// types can be whatever the constructors for this type will return. Underlying
// types of Response are all value types.
// types can be whatever the constructors for this type will return.
//
// The following types implement this interface:
//
// - *SelectInteraction
// - *ButtonInteraction
//
type ComponentInteraction interface {
InteractionData
// ID returns the ID of the component in response. Not all component

View file

@ -12,29 +12,19 @@ types=(
MentionableOption
)
recvs=(
s
s
s
i
b
u
c
r
m
)
for ((i = 0; i < ${#types[@]}; i++)); {
recv=$(head -c1 <<< "${types[$i]}" | tr "[:upper:]" "[:lower:]")
for ((i = 0; i < 6; i++)); {
cat<<EOF
// MarshalJSON marshals ${types[$i]} to JSON with the "type" field.
func (${recvs[$i]} *${types[$i]}) MarshalJSON() ([]byte, error) {
func (${recv} *${types[$i]}) MarshalJSON() ([]byte, error) {
type raw ${types[$i]}
return json.Marshal(struct {
Type CommandOptionType \`json:"type"\`
*raw
}{
Type: ${recvs[$i]}.Type(),
raw: (*raw)(${recvs[$i]}),
Type: ${recv}.Type(),
raw: (*raw)(${recv}),
})
}