Skip to content

Commit

Permalink
refactor handler & event payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jun 29, 2023
1 parent 3efc9f6 commit a2c847b
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 104 deletions.
5 changes: 2 additions & 3 deletions bot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import (
"context"

"github.com/disgoorg/disgo"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"

"github.com/disgoorg/disgo/cache"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/disgo/httpserver"
"github.com/disgoorg/disgo/rest"
"github.com/disgoorg/disgo/sharding"
"github.com/disgoorg/disgo/voice"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"
)

// New creates a new bot.Client with the provided token & bot.ConfigOpt(s)
Expand Down
41 changes: 36 additions & 5 deletions bot/event_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,45 @@ type eventManagerImpl struct {
}

func (m *eventManagerImpl) HandleEvent(event gateway.Event) {
// set respond function if not set to handle http & gateway interactions the same way
if e, ok := event.(gateway.EventInteractionCreate); ok && e.Respond == nil {
e.Respond = func(response discord.InteractionResponse) error {
return m.client.Rest.CreateInteractionResponse(e.Interaction.ID(), e.Interaction.Token(), response)

switch e := event.(type) {
case gateway.EventInteractionCreate:
// set respond function if not set to handle http & gateway interactions the same way
if e.Respond == nil {
e.Respond = func(response discord.InteractionResponse) error {
return m.client.Rest.CreateInteractionResponse(e.Interaction.ID(), e.Interaction.Token(), response)
}
event = e
}

switch i := e.Interaction.(type) {
case discord.ApplicationCommandInteraction:
m.DispatchEvent(EventApplicationCommandInteractionCreate{
ApplicationCommandInteraction: i,
Respond: e.Respond,
})
case discord.AutocompleteInteraction:
m.DispatchEvent(EventAutocompleteInteractionCreate{
AutocompleteInteraction: i,
Respond: e.Respond,
})
case discord.ComponentInteraction:
m.DispatchEvent(EventComponentInteractionCreate{
ComponentInteraction: i,
Respond: e.Respond,
})
case discord.ModalInteraction:
m.DispatchEvent(EventModalInteractionCreate{
ModalInteraction: i,
Respond: e.Respond,
})
}
event = e
}

m.DispatchEvent(event)
}

func (m *eventManagerImpl) DispatchEvent(event gateway.Event) {
defer func() {
if r := recover(); r != nil {
m.config.Logger.Errorf("recovered from panic in event listener: %+v\nstack: %s", r, string(debug.Stack()))
Expand Down
23 changes: 23 additions & 0 deletions bot/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,45 @@ import (
"github.com/disgoorg/disgo/gateway"
)

const (
EventTypeApplicationCommandInteractionCreate gateway.EventType = "APPLICATION_COMMAND_INTERACTION_CREATE"
EventTypeAutocompleteInteractionCreate gateway.EventType = "AUTOCOMPLETE_INTERACTION_CREATE"
EventTypeComponentInteractionCreate gateway.EventType = "COMPONENT_INTERACTION_CREATE"
EventTypeModalInteractionCreate gateway.EventType = "MODAL_INTERACTION_CREATE"
)

type EventApplicationCommandInteractionCreate struct {
discord.ApplicationCommandInteraction
Respond gateway.RespondFunc `json:"-"`
}

func (EventApplicationCommandInteractionCreate) EventType() gateway.EventType {
return EventTypeApplicationCommandInteractionCreate
}

type EventAutocompleteInteractionCreate struct {
discord.AutocompleteInteraction
Respond gateway.RespondFunc `json:"-"`
}

func (EventAutocompleteInteractionCreate) EventType() gateway.EventType {
return EventTypeAutocompleteInteractionCreate
}

type EventComponentInteractionCreate struct {
discord.ComponentInteraction
Respond gateway.RespondFunc `json:"-"`
}

func (EventComponentInteractionCreate) EventType() gateway.EventType {
return EventTypeComponentInteractionCreate
}

type EventModalInteractionCreate struct {
discord.ModalInteraction
Respond gateway.RespondFunc `json:"-"`
}

func (EventModalInteractionCreate) EventType() gateway.EventType {
return EventTypeModalInteractionCreate
}
Loading

0 comments on commit a2c847b

Please sign in to comment.