Skip to content

Commit

Permalink
add interaction dispatch handler
Browse files Browse the repository at this point in the history
  • Loading branch information
maribowman committed Dec 17, 2023
1 parent 5f65c3e commit 0aa8d38
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion app/model/wiring.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package model
import "github.com/bwmarrin/discordgo"

type DiscordService interface {
DispatchHandler(*discordgo.Session, *discordgo.MessageCreate)
MessageDispatchHandler(*discordgo.Session, *discordgo.MessageCreate)
InteractionDispatchHandler(*discordgo.Session, *discordgo.InteractionCreate)
CloseSession()
}

Expand Down
23 changes: 21 additions & 2 deletions app/service/discord_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func NewDiscordService() model.DiscordService {
log.Fatal().Err(err).Msg("error creating discord session")
}

session.AddHandler(service.DispatchHandler)
session.AddHandler(service.MessageDispatchHandler)
session.AddHandler(service.InteractionDispatchHandler)
session.Identify.Intents = discordgo.IntentsGuildMessages

if err = session.Open(); err != nil {
Expand All @@ -37,7 +38,7 @@ func NewDiscordService() model.DiscordService {
return &service
}

func (service *DiscordService) DispatchHandler(session *discordgo.Session, message *discordgo.MessageCreate) {
func (service *DiscordService) MessageDispatchHandler(session *discordgo.Session, message *discordgo.MessageCreate) {
switch message.ChannelID {
case config.Config.Discord.Channels[GroceriesChannelName]:
service.groceryBot.MessageEvent(session, message)
Expand All @@ -46,6 +47,24 @@ func (service *DiscordService) DispatchHandler(session *discordgo.Session, messa
}
}

func (service *DiscordService) InteractionDispatchHandler(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
switch interaction.Type {
//case config.Config.Discord.Channels[GroceriesChannelName]:
// service.groceryBot.MessageEvent(session, message)
// TODO
case discordgo.InteractionApplicationCommand:
//if h, ok := commandsHandlers[i.ApplicationCommandData().Name]; ok {
// h(s, i)
//}
case discordgo.InteractionMessageComponent:
//if h, ok := componentsHandlers[i.MessageComponentData().CustomID]; ok {
// h(s, i)
//}
default:
log.Debug().Msg("could not dispatch interaction event to handler")
}
}

func (service *DiscordService) CloseSession() {
if err := service.session.Close(); err != nil {
log.Error().Err(err).Msg("could not close discord session")
Expand Down

0 comments on commit 0aa8d38

Please sign in to comment.