Skip to content

Commit

Permalink
moved some metods to interaction & added form-data support to rest-cl…
Browse files Browse the repository at this point in the history
…ient
  • Loading branch information
topi314 committed May 24, 2021
1 parent e6e8eee commit 1f6bb92
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 78 deletions.
69 changes: 2 additions & 67 deletions api/events/interaction_events.go
Expand Up @@ -9,47 +9,7 @@ import (
// GenericInteractionEvent generic api.Interaction event
type GenericInteractionEvent struct {
GenericEvent
*api.Interaction
}

// Guild returns the api.Guild from the api.Cache
func (e GenericInteractionEvent) Guild() *api.Guild {
if e.Interaction.GuildID == nil {
return nil
}
return e.Disgo().Cache().Guild(*e.Interaction.GuildID)
}

// DMChannel returns the api.DMChannel from the api.Cache
func (e GenericInteractionEvent) DMChannel() *api.DMChannel {
if e.Interaction.ChannelID == nil {
return nil
}
return e.Disgo().Cache().DMChannel(*e.Interaction.ChannelID)
}

// MessageChannel returns the api.MessageChannel from the api.Cache
func (e GenericInteractionEvent) MessageChannel() *api.MessageChannel {
if e.Interaction.ChannelID == nil {
return nil
}
return e.Disgo().Cache().MessageChannel(*e.Interaction.ChannelID)
}

// TextChannel returns the api.TextChannel from the api.Cache
func (e GenericInteractionEvent) TextChannel() *api.TextChannel {
if e.Interaction.ChannelID == nil {
return nil
}
return e.Disgo().Cache().TextChannel(*e.Interaction.ChannelID)
}

// GuildChannel returns the api.GuildChannel from the api.Cache
func (e GenericInteractionEvent) GuildChannel() *api.GuildChannel {
if e.Interaction.ChannelID == nil {
return nil
}
return e.Disgo().Cache().GuildChannel(*e.Interaction.ChannelID)
Interaction *api.Interaction
}

// SlashCommandEvent indicates that a slash api.Command was ran in a api.Guild
Expand Down Expand Up @@ -134,30 +94,5 @@ func (e *SlashCommandEvent) Reply(response *api.InteractionResponse) error {
return nil
}

return e.Disgo().RestClient().SendInteractionResponse(e.Interaction.ID, e.Interaction.Token, response)
}

// EditOriginal edits the original api.InteractionResponse
func (e *SlashCommandEvent) EditOriginal(followupMessage *api.FollowupMessage) (*api.Message, error) {
return e.Disgo().RestClient().EditInteractionResponse(e.Disgo().ApplicationID(), e.Interaction.Token, followupMessage)
}

// DeleteOriginal deletes the original api.InteractionResponse
func (e *SlashCommandEvent) DeleteOriginal() error {
return e.Disgo().RestClient().DeleteInteractionResponse(e.Disgo().ApplicationID(), e.Interaction.Token)
}

// SendFollowup used to send a api.FollowupMessage to an api.Interaction
func (e *SlashCommandEvent) SendFollowup(followupMessage *api.FollowupMessage) (*api.Message, error) {
return e.Disgo().RestClient().SendFollowupMessage(e.Disgo().ApplicationID(), e.Interaction.Token, followupMessage)
}

// EditFollowup used to edit a api.FollowupMessage from an api.Interaction
func (e *SlashCommandEvent) EditFollowup(messageID api.Snowflake, followupMessage *api.FollowupMessage) (*api.Message, error) {
return e.Disgo().RestClient().EditFollowupMessage(e.Disgo().ApplicationID(), e.Interaction.Token, messageID, followupMessage)
}

// DeleteFollowup used to delete a api.FollowupMessage from an api.Interaction
func (e *SlashCommandEvent) DeleteFollowup(messageID api.Snowflake) error {
return e.Disgo().RestClient().DeleteFollowupMessage(e.Disgo().ApplicationID(), e.Interaction.Token, messageID)
return e.Interaction.Disgo.RestClient().SendInteractionResponse(e.Interaction.ID, e.Interaction.Token, response)
}
66 changes: 66 additions & 0 deletions api/interaction.go
Expand Up @@ -11,6 +11,7 @@ const (

// An Interaction is the slash command object you receive when a user uses one of your commands
type Interaction struct {
Disgo Disgo
ID Snowflake `json:"id"`
Type InteractionType `json:"type"`
Data *InteractionData `json:"data,omitempty"`
Expand All @@ -22,6 +23,71 @@ type Interaction struct {
Version int `json:"version"`
}

// Guild returns the api.Guild from the api.Cache
func (i *Interaction) Guild() *Guild {
if i.GuildID == nil {
return nil
}
return i.Disgo.Cache().Guild(*i.GuildID)
}

// DMChannel returns the api.DMChannel from the api.Cache
func (i *Interaction) DMChannel() *DMChannel {
if i.ChannelID == nil {
return nil
}
return i.Disgo.Cache().DMChannel(*i.ChannelID)
}

// MessageChannel returns the api.MessageChannel from the api.Cache
func (i *Interaction) MessageChannel() *MessageChannel {
if i.ChannelID == nil {
return nil
}
return i.Disgo.Cache().MessageChannel(*i.ChannelID)
}

// TextChannel returns the api.TextChannel from the api.Cache
func (i *Interaction) TextChannel() *TextChannel {
if i.ChannelID == nil {
return nil
}
return i.Disgo.Cache().TextChannel(*i.ChannelID)
}

// GuildChannel returns the api.GuildChannel from the api.Cache
func (i *Interaction) GuildChannel() *GuildChannel {
if i.ChannelID == nil {
return nil
}
return i.Disgo.Cache().GuildChannel(*i.ChannelID)
}

// EditOriginal edits the original api.InteractionResponse
func (i *Interaction) EditOriginal(followupMessage *FollowupMessage) (*Message, error) {
return i.Disgo.RestClient().EditInteractionResponse(i.Disgo.ApplicationID(), i.Token, followupMessage)
}

// DeleteOriginal deletes the original api.InteractionResponse
func (i *Interaction) DeleteOriginal() error {
return i.Disgo.RestClient().DeleteInteractionResponse(i.Disgo.ApplicationID(), i.Token)
}

// SendFollowup used to send a api.FollowupMessage to an api.Interaction
func (i *Interaction) SendFollowup(followupMessage *FollowupMessage) (*Message, error) {
return i.Disgo.RestClient().SendFollowupMessage(i.Disgo.ApplicationID(), i.Token, followupMessage)
}

// EditFollowup used to edit a api.FollowupMessage from an api.Interaction
func (i *Interaction) EditFollowup(messageID Snowflake, followupMessage *FollowupMessage) (*Message, error) {
return i.Disgo.RestClient().EditFollowupMessage(i.Disgo.ApplicationID(), i.Token, messageID, followupMessage)
}

// DeleteFollowup used to delete a api.FollowupMessage from an api.Interaction
func (i *Interaction) DeleteFollowup(messageID Snowflake) error {
return i.Disgo.RestClient().DeleteFollowupMessage(i.Disgo.ApplicationID(), i.Token, messageID)
}

// InteractionData is the command data payload
type InteractionData struct {
ID Snowflake `json:"id"`
Expand Down
10 changes: 5 additions & 5 deletions example/examplebot.go
Expand Up @@ -194,7 +194,7 @@ func slashCommandListener(event *events.SlashCommandEvent) {
embed.SetField(1, "Time", strconv.Itoa(int(elapsed.Milliseconds()))+"ms", true)

if err != nil {
_, _ = event.EditOriginal(api.NewFollowupMessageBuilder().
_, _ = event.Interaction.EditOriginal(api.NewFollowupMessageBuilder().
SetEmbeds(embed.
SetColor(red).
SetField(0, "Status", "Failed", true).
Expand All @@ -205,7 +205,7 @@ func slashCommandListener(event *events.SlashCommandEvent) {
)
return
}
_, _ = event.EditOriginal(api.NewFollowupMessageBuilder().
_, _ = event.Interaction.EditOriginal(api.NewFollowupMessageBuilder().
SetEmbeds(embed.
SetColor(green).
SetField(0, "Status", "Success", true).
Expand All @@ -228,23 +228,23 @@ func slashCommandListener(event *events.SlashCommandEvent) {
_ = event.Acknowledge(true)

time.Sleep(2 * time.Second)
_, _ = event.EditOriginal(api.NewFollowupMessageBuilder().
_, _ = event.Interaction.EditOriginal(api.NewFollowupMessageBuilder().
SetEmbeds(api.NewEmbedBuilder().
SetDescription("finished with thinking").
Build(),
).Build(),
)

time.Sleep(1 * time.Second)
_, _ = event.SendFollowup(api.NewFollowupMessageBuilder().
_, _ = event.Interaction.SendFollowup(api.NewFollowupMessageBuilder().
SetEmbeds(api.NewEmbedBuilder().
SetDescription("followup 1").
Build(),
).Build(),
)

time.Sleep(1 * time.Second)
_, _ = event.SendFollowup(api.NewFollowupMessageBuilder().
_, _ = event.Interaction.SendFollowup(api.NewFollowupMessageBuilder().
SetEphemeral(true).
SetContent("followup 2 only you can see").
Build(),
Expand Down
1 change: 1 addition & 0 deletions internal/entity_builder_impl.go
Expand Up @@ -20,6 +20,7 @@ func (b EntityBuilderImpl) Disgo() api.Disgo {

// CreateInteraction returns a new api.Interaction entity
func (b EntityBuilderImpl) CreateInteraction(interaction *api.Interaction, updateCache api.CacheStrategy) *api.Interaction {
interaction.Disgo = b.disgo
if interaction.Member != nil {
interaction.Member = b.CreateMember(*interaction.GuildID, interaction.Member, api.CacheStrategyYes)
}
Expand Down
21 changes: 15 additions & 6 deletions internal/restclient_impl.go
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"

"github.com/DisgoOrg/disgo/api/events"
Expand Down Expand Up @@ -56,12 +57,21 @@ func (r RestClientImpl) UserAgent() string {
func (r RestClientImpl) Request(route *endpoints.CompiledAPIRoute, rqBody interface{}, rsBody interface{}) error {
var reader io.Reader
var rqJSON []byte
var contentType string
if rqBody != nil {
rqJSON, err := json.Marshal(rqBody)
if err != nil {
return err
switch v := rqBody.(type) {
case url.Values:
contentType = "application/x-www-form-urlencoded"
rqJSON = []byte(v.Encode())
default:
contentType = "application/json"
var err error
rqJSON, err = json.Marshal(rqBody)
if err != nil {
return err
}
}
r.Disgo().Logger().Debugf("request json: \"%s\"", string(rqJSON))
r.Disgo().Logger().Debugf("request body: \"%s\"", string(rqJSON))
reader = bytes.NewBuffer(rqJSON)
} else {
reader = nil
Expand All @@ -71,10 +81,9 @@ func (r RestClientImpl) Request(route *endpoints.CompiledAPIRoute, rqBody interf
if err != nil {
return err
}

rq.Header.Set("User-Agent", r.UserAgent())
rq.Header.Set("Authorization", "Bot "+r.disgo.Token())
rq.Header.Set("Content-Type", "application/json")
rq.Header.Set("Content-Type", contentType)

rs, err := r.client.Do(rq)
if err != nil {
Expand Down

0 comments on commit 1f6bb92

Please sign in to comment.