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{"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", "🔥", "🍒", "🎉", "🥳", "🎊"}
C Config
)
C command.Config
type (
Config struct {
Handler HandlerConfig `mapstructure:"handler"`
}
HandlerConfig struct {
Reaction ReactionConfig `mapstructure:"reaction"`
Weather command.WeatherConfig `mapstructure:"weather"`
}
ReactionConfig struct {
Emojis []string
Channels []string
handlers []command.CommandHandler = []command.CommandHandler{
command.NewCoinHandler(),
command.NewPingHandler(),
command.NewRollHandler(),
command.NewRouletteHandler(),
command.NewTimeHandler(),
command.NewWeatherHandler(),
}
)
@ -93,17 +86,13 @@ func main() {
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(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

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"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
)
type (
PingHandler struct{}
PingHandler struct {
config Config
}
)
func NewPingHandler() *PingHandler {
return new(PingHandler)
}
func (h *PingHandler) SetConfig(config Config) {
h.config = config
}
func (h *PingHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return

View File

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

View File

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