Skip to content

Commit

Permalink
Merge branch 'development' into feature/audit-log
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jul 11, 2021
2 parents 3b6333a + e10263b commit cf2fa3f
Show file tree
Hide file tree
Showing 95 changed files with 2,825 additions and 1,721 deletions.
43 changes: 43 additions & 0 deletions api/action_row.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package api

var _ Component = (*ActionRow)(nil)

// NewActionRow creates a new ActionRow holding th provided Component(s)
func NewActionRow(components ...Component) ActionRow {
return ActionRow{
Expand All @@ -13,3 +15,44 @@ type ActionRow struct {
ComponentImpl
Components []Component `json:"components"`
}

// SetComponents returns a new ActionRow with the provided Component(s)
func (r ActionRow) SetComponents(components ...Component) ActionRow {
r.Components = components
return r
}

// SetComponent returns a new ActionRow with the Component which has the customID replaced
func (r ActionRow) SetComponent(customID string, component Component) ActionRow {
for i, c := range r.Components {
switch com := c.(type) {
case Button:
if com.CustomID == customID {
r.Components[i] = component
break
}
case SelectMenu:
if com.CustomID == customID {
r.Components[i] = component
break
}
default:
continue
}
}
return r
}

// AddComponents returns a new ActionRow with the provided Component(s) added
func (r ActionRow) AddComponents(components ...Component) ActionRow {
r.Components = append(r.Components, components...)
return r
}

// RemoveComponent returns a new ActionRow with the provided Component at the index removed
func (r ActionRow) RemoveComponent(index int) ActionRow {
if len(r.Components) > index {
r.Components = append(r.Components[:index], r.Components[index+1:]...)
}
return r
}
70 changes: 59 additions & 11 deletions api/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
)

// NewButton creates a new Button with the provided parameters. Link Button(s) need a url and other Button(s) need a customID
func NewButton(style ButtonStyle, label *string, customID string, url string, emoji *Emoji, disabled bool) Button {
func NewButton(style ButtonStyle, label string, customID string, url string, emoji *Emoji, disabled bool) Button {
return Button{
ComponentImpl: newComponentImpl(ComponentTypeButton),
Style: style,
Expand All @@ -26,37 +26,85 @@ func NewButton(style ButtonStyle, label *string, customID string, url string, em
}

// NewPrimaryButton creates a new Button with ButtonStylePrimary & the provided parameters
func NewPrimaryButton(label string, customID string, emoji *Emoji, disabled bool) Button {
return NewButton(ButtonStylePrimary, &label, customID, "", emoji, disabled)
func NewPrimaryButton(label string, customID string, emoji *Emoji) Button {
return NewButton(ButtonStylePrimary, label, customID, "", emoji, false)
}

// NewSecondaryButton creates a new Button with ButtonStyleSecondary & the provided parameters
func NewSecondaryButton(label string, customID string, emoji *Emoji, disabled bool) Button {
return NewButton(ButtonStyleSecondary, &label, customID, "", emoji, disabled)
func NewSecondaryButton(label string, customID string, emoji *Emoji) Button {
return NewButton(ButtonStyleSecondary, label, customID, "", emoji, false)
}

// NewSuccessButton creates a new Button with ButtonStyleSuccess & the provided parameters
func NewSuccessButton(label string, customID string, emoji *Emoji, disabled bool) Button {
return NewButton(ButtonStyleSuccess, &label, customID, "", emoji, disabled)
func NewSuccessButton(label string, customID string, emoji *Emoji) Button {
return NewButton(ButtonStyleSuccess, label, customID, "", emoji, false)
}

// NewDangerButton creates a new Button with ButtonStyleDanger & the provided parameters
func NewDangerButton(label string, customID string, emoji *Emoji, disabled bool) Button {
return NewButton(ButtonStyleDanger, &label, customID, "", emoji, disabled)
func NewDangerButton(label string, customID string, emoji *Emoji) Button {
return NewButton(ButtonStyleDanger, label, customID, "", emoji, false)
}

// NewLinkButton creates a new link Button with ButtonStyleLink & the provided parameters
func NewLinkButton(label string, url string, emoji *Emoji, disabled bool) Button {
return NewButton(ButtonStyleLink, &label, "", url, emoji, disabled)
return NewButton(ButtonStyleLink, label, "", url, emoji, disabled)
}

// Button can be attacked to all messages & be clicked by a User. If clicked it fires a events.ButtonClickEvent with the declared customID
type Button struct {
ComponentImpl
Style ButtonStyle `json:"style,omitempty"`
Label *string `json:"label,omitempty"`
Label string `json:"label,omitempty"`
Emoji *Emoji `json:"emoji,omitempty"`
CustomID string `json:"custom_id,omitempty"`
URL string `json:"url,omitempty"`
Disabled bool `json:"disabled,omitempty"`
}

// AsDisabled returns a new Button but disabled
func (b Button) AsDisabled() Button {
b.Disabled = true
return b
}

// AsEnabled returns a new Button but enabled
func (b Button) AsEnabled() Button {
b.Disabled = true
return b
}

// WithDisabled returns a new Button but disabled/enabled
func (b Button) WithDisabled(disabled bool) Button {
b.Disabled = disabled
return b
}

// WithEmoji returns a new Button with the provided Emoji
func (b Button) WithEmoji(emoji *Emoji) Button {
b.Emoji = emoji
return b
}

// WithCustomID returns a new Button with the provided custom id
func (b Button) WithCustomID(customID string) Button {
b.CustomID = customID
return b
}

// WithStyle returns a new Button with the provided style
func (b Button) WithStyle(style ButtonStyle) Button {
b.Style = style
return b
}

// WithLabel returns a new Button with the provided label
func (b Button) WithLabel(label string) Button {
b.Label = label
return b
}

// WithURL returns a new Button with the provided url
func (b Button) WithURL(url string) Button {
b.URL = url
return b
}
21 changes: 7 additions & 14 deletions api/button_interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@ package api

// ButtonInteraction is a specific Interaction when CLicked on Button(s)
type ButtonInteraction struct {
*Interaction
Message *Message `json:"message,omitempty"`
Data *ButtonInteractionData `json:"data,omitempty"`
*ComponentInteraction
Data *ButtonInteractionData `json:"data,omitempty"`
}

// DeferEdit replies to the api.ButtonInteraction with api.InteractionResponseTypeDeferredUpdateMessage and cancels the loading state
func (i *ButtonInteraction) DeferEdit() error {
return i.Respond(InteractionResponseTypeDeferredUpdateMessage, nil)
// Button returns the Button which issued this ButtonInteraction. nil for ephemeral Message(s)
func (i *ButtonInteraction) Button() *Button {
return i.Message.ButtonByID(i.CustomID())
}

// Edit replies to the api.ButtonInteraction with api.InteractionResponseTypeUpdateMessage & api.MessageCreate which edits the original api.Message
func (i *ButtonInteraction) Edit(messageCreate MessageCreate) error {
return i.Respond(InteractionResponseTypeUpdateMessage, messageCreate)
}

// ButtonInteractionData is the command data payload
// ButtonInteractionData is the Button data payload
type ButtonInteractionData struct {
CustomID string `json:"custom_id"`
ComponentType ComponentType `json:"component_type"`
*ComponentInteractionData
}
28 changes: 18 additions & 10 deletions api/channels.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package api

import "errors"
import (
"errors"

"github.com/DisgoOrg/restclient"
)

// ChannelType for interacting with discord's channels
type ChannelType int
Expand Down Expand Up @@ -45,30 +49,29 @@ type MessageChannel struct {
}

// SendMessage sends a Message to a TextChannel
func (c MessageChannel) SendMessage(message MessageCreate) (*Message, error) {
// Todo: attachments
return c.Disgo.RestClient().SendMessage(c.ID, message)
func (c MessageChannel) SendMessage(message MessageCreate) (*Message, restclient.RestError) {
return c.Disgo.RestClient().CreateMessage(c.ID, message)
}

// EditMessage edits a Message in this TextChannel
func (c MessageChannel) EditMessage(messageID Snowflake, message MessageUpdate) (*Message, error) {
return c.Disgo.RestClient().EditMessage(c.ID, messageID, message)
func (c MessageChannel) EditMessage(messageID Snowflake, message MessageUpdate) (*Message, restclient.RestError) {
return c.Disgo.RestClient().UpdateMessage(c.ID, messageID, message)
}

// DeleteMessage allows you to edit an existing Message sent by you
func (c MessageChannel) DeleteMessage(messageID Snowflake) error {
func (c MessageChannel) DeleteMessage(messageID Snowflake) restclient.RestError {
return c.Disgo.RestClient().DeleteMessage(c.ID, messageID)
}

// BulkDeleteMessages allows you bulk delete Message(s)
func (c MessageChannel) BulkDeleteMessages(messageIDs ...Snowflake) error {
func (c MessageChannel) BulkDeleteMessages(messageIDs ...Snowflake) restclient.RestError {
return c.Disgo.RestClient().BulkDeleteMessages(c.ID, messageIDs...)
}

// CrosspostMessage crossposts an existing Message
func (c MessageChannel) CrosspostMessage(messageID Snowflake) (*Message, error) {
func (c MessageChannel) CrosspostMessage(messageID Snowflake) (*Message, restclient.RestError) {
if c.Type != ChannelTypeNews {
return nil, errors.New("channel type is not NEWS")
return nil, restclient.NewError(nil, errors.New("channel type is not NEWS"))
}
return c.Disgo.RestClient().CrosspostMessage(c.ID, messageID)
}
Expand All @@ -78,6 +81,11 @@ type DMChannel struct {
MessageChannel
}

// CreateDMChannel is the payload used to create a DMChannel
type CreateDMChannel struct {
RecipientID Snowflake `json:"recipient_id"`
}

// GuildChannel is a generic type for all server channels
type GuildChannel struct {
Channel
Expand Down
4 changes: 2 additions & 2 deletions api/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ func (c *Command) Update(command CommandUpdate) error {
var rC *Command
var err error
if c.GuildID == nil {
rC, err = c.Disgo.RestClient().EditGlobalCommand(c.Disgo.ApplicationID(), c.ID, command)
rC, err = c.Disgo.RestClient().UpdateGlobalCommand(c.Disgo.ApplicationID(), c.ID, command)

} else {
rC, err = c.Disgo.RestClient().EditGuildCommand(c.Disgo.ApplicationID(), *c.GuildID, c.ID, command)
rC, err = c.Disgo.RestClient().UpdateGuildCommand(c.Disgo.ApplicationID(), *c.GuildID, c.ID, command)
}
if err != nil {
return err
Expand Down
50 changes: 45 additions & 5 deletions api/command_interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,52 @@ type CommandInteraction struct {
Data *CommandInteractionData `json:"data,omitempty"`
}

// CommandID returns the ID of the api.Command which got used
func (i *CommandInteraction) CommandID() Snowflake {
return i.Data.ID
}

// CommandName the name of the api.Command which got used
func (i *CommandInteraction) CommandName() string {
return i.Data.CommandName
}

// SubCommandName the subcommand name of the api.Command which got used. May be nil
func (i *CommandInteraction) SubCommandName() *string {
return i.Data.SubCommandName
}

// SubCommandGroupName the subcommand group name of the api.Command which got used. May be nil
func (i *CommandInteraction) SubCommandGroupName() *string {
return i.Data.SubCommandGroupName
}

// CommandPath returns the api.Command path
func (i *CommandInteraction) CommandPath() string {
path := i.CommandName()
if name := i.SubCommandName(); name != nil {
path += "/" + *name
}
if name := i.SubCommandGroupName(); name != nil {
path += "/" + *name
}
return path
}

// Options returns the parsed Option which the Command got used with
func (i *CommandInteraction) Options() []*Option {
return i.Data.Options
}

// CommandInteractionData is the command data payload
type CommandInteractionData struct {
ID Snowflake `json:"id"`
Name string `json:"name"`
Resolved *Resolved `json:"resolved,omitempty"`
Options []*OptionData `json:"options,omitempty"`
ID Snowflake `json:"id"`
CommandName string `json:"name"`
SubCommandName *string `json:"-"`
SubCommandGroupName *string `json:"-"`
Resolved *Resolved `json:"resolved,omitempty"`
RawOptions []OptionData `json:"options,omitempty"`
Options []*Option `json:"-"`
}

// Resolved contains resolved mention data
Expand All @@ -27,7 +67,7 @@ type OptionData struct {
Name string `json:"name"`
Type CommandOptionType `json:"type"`
Value interface{} `json:"value,omitempty"`
Options []*OptionData `json:"options,omitempty"`
Options []OptionData `json:"options,omitempty"`
}

// Option holds info about an Option.Value
Expand Down
29 changes: 21 additions & 8 deletions api/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type ComponentType int
const (
ComponentTypeActionRow = iota + 1
ComponentTypeButton
ComponentTypeSelectMenu
)

// Component is a general interface each Component needs to implement
Expand All @@ -30,12 +31,24 @@ func (t ComponentImpl) Type() ComponentType {

// UnmarshalComponent is used for easier unmarshalling of different Component(s)
type UnmarshalComponent struct {
ComponentType ComponentType `json:"type"`
Style ButtonStyle `json:"style"`
Label *string `json:"label"`
Emoji *Emoji `json:"emoji"`
CustomID string `json:"custom_id"`
URL string `json:"url"`
Disabled bool `json:"disabled"`
Components []UnmarshalComponent `json:"components"`
ComponentType ComponentType `json:"type"`

// Button && SelectMenu
CustomID string `json:"custom_id"`

// Button
Style ButtonStyle `json:"style"`
Label string `json:"label"`
Emoji *Emoji `json:"emoji"`
URL string `json:"url"`
Disabled bool `json:"disabled"`

// ActionRow
Components []UnmarshalComponent `json:"components"`

// SelectMenu
Placeholder string `json:"placeholder"`
MinValues int `json:"min_values,omitempty"`
MaxValues int `json:"max_values,omitempty"`
Options []SelectOption `json:"options"`
}
Loading

0 comments on commit cf2fa3f

Please sign in to comment.