diff --git a/cmd/bb/main.go b/cmd/bb/main.go index c128aea..67e06d3 100644 --- a/cmd/bb/main.go +++ b/cmd/bb/main.go @@ -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 diff --git a/command/command.go b/command/command.go new file mode 100644 index 0000000..51b7dc8 --- /dev/null +++ b/command/command.go @@ -0,0 +1,8 @@ +package command + +import "github.com/bwmarrin/discordgo" + +type CommandHandler interface { + Handle(s *discordgo.Session, m *discordgo.MessageCreate) + SetConfig(config Config) +} diff --git a/command/ping.go b/command/ping.go index 7697a2a..92d72f1 100644 --- a/command/ping.go +++ b/command/ping.go @@ -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 diff --git a/command/random.go b/command/random.go index 49bd369..f6c7590 100644 --- a/command/random.go +++ b/command/random.go @@ -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 diff --git a/command/time.go b/command/time.go index 630d3c9..0a505f7 100644 --- a/command/time.go +++ b/command/time.go @@ -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