Inject config into each command handler
This commit is contained in:
parent
4e69e241dd
commit
2895789aac
@ -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
8
command/command.go
Normal 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)
|
||||
}
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user