Inject config into each command handler

This commit is contained in:
Ryan Cavicchioni 2022-08-03 23:52:08 -05:00
parent 4e69e241dd
commit 2895789aac
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
5 changed files with 57 additions and 30 deletions

View File

@ -26,22 +26,15 @@ var (
defaultReactions []string = []string{"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", "🔥", "🍒", "🎉", "🥳", "🎊"} defaultReactions []string = []string{"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", "🔥", "🍒", "🎉", "🥳", "🎊"}
C Config C command.Config
)
type ( handlers []command.CommandHandler = []command.CommandHandler{
Config struct { command.NewCoinHandler(),
Handler HandlerConfig `mapstructure:"handler"` command.NewPingHandler(),
} command.NewRollHandler(),
command.NewRouletteHandler(),
HandlerConfig struct { command.NewTimeHandler(),
Reaction ReactionConfig `mapstructure:"reaction"` command.NewWeatherHandler(),
Weather command.WeatherConfig `mapstructure:"weather"`
}
ReactionConfig struct {
Emojis []string
Channels []string
} }
) )
@ -93,17 +86,13 @@ func main() {
log.Fatalf("error creating Discord session: %v\n", err) log.Fatalf("error creating Discord session: %v\n", err)
} }
dg.AddHandler(command.NewPingHandler().Handle) for _, h := range handlers {
h.SetConfig(C)
dg.AddHandler(h.Handle)
}
dg.AddHandler(reactionHandler) dg.AddHandler(reactionHandler)
dg.AddHandler(praiseHandler) dg.AddHandler(praiseHandler)
dg.AddHandler(command.NewRollHandler().Handle)
dg.AddHandler(command.NewRouletteHandler().Handle)
h := command.NewWeatherHandler(C.Handler.Weather)
dg.AddHandler(h.Handle)
dg.AddHandler(command.NewCoinHandler().Handle)
dg.AddHandler(command.NewTimeHandler().Handle)
dg.Identify.Intents = discordgo.IntentsGuildMessages dg.Identify.Intents = discordgo.IntentsGuildMessages

8
command/command.go Normal file
View File

@ -0,0 +1,8 @@
package command
import "github.com/bwmarrin/discordgo"
type CommandHandler interface {
Handle(s *discordgo.Session, m *discordgo.MessageCreate)
SetConfig(config Config)
}

View File

@ -4,18 +4,23 @@ import (
"strings" "strings"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
type ( type (
PingHandler struct{} PingHandler struct {
config Config
}
) )
func NewPingHandler() *PingHandler { func NewPingHandler() *PingHandler {
return new(PingHandler) return new(PingHandler)
} }
func (h *PingHandler) SetConfig(config Config) {
h.config = config
}
func (h *PingHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { func (h *PingHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID { if m.Author.ID == s.State.User.ID {
return return

View File

@ -34,9 +34,16 @@ type (
N int N int
} }
CoinHandler struct{} CoinHandler struct {
RollHandler struct{} config Config
RouletteHandler struct{} }
RollHandler struct {
config Config
}
RouletteHandler struct {
config Config
}
) )
var ( var (
@ -146,6 +153,10 @@ func NewRollHandler() *RollHandler {
return new(RollHandler) return new(RollHandler)
} }
func (h *RollHandler) SetConfig(config Config) {
h.config = config
}
func (h *RollHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { func (h *RollHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
var ( var (
err error err error
@ -188,6 +199,10 @@ func NewRouletteHandler() *RouletteHandler {
return new(RouletteHandler) return new(RouletteHandler)
} }
func (h *RouletteHandler) SetConfig(config Config) {
h.config = config
}
func (h *RouletteHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { func (h *RouletteHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID { if m.Author.ID == s.State.User.ID {
return return
@ -214,6 +229,10 @@ func NewCoinHandler() *CoinHandler {
return new(CoinHandler) return new(CoinHandler)
} }
func (h *CoinHandler) SetConfig(config Config) {
h.config = config
}
func (h *CoinHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { func (h *CoinHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
var ( var (
c Coin c Coin

View File

@ -10,13 +10,19 @@ import (
) )
type ( type (
TimeHandler struct{} TimeHandler struct {
config Config
}
) )
func NewTimeHandler() *TimeHandler { func NewTimeHandler() *TimeHandler {
return new(TimeHandler) return new(TimeHandler)
} }
func (h *TimeHandler) SetConfig(config Config) {
h.config = config
}
func (h *TimeHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { func (h *TimeHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
var ( var (
t time.Time t time.Time