From d0ddca7fe10ee99c2fed9cd88bdf0e4e1efa00ee Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Wed, 27 Jul 2022 23:43:08 -0500 Subject: [PATCH] make all handlers a struct --- cmd/bb/main.go | 6 +++--- command/ping.go | 10 +++++++++- command/random.go | 16 +++++++++++++--- command/time.go | 3 +-- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/cmd/bb/main.go b/cmd/bb/main.go index f5aa9f4..c3d24df 100644 --- a/cmd/bb/main.go +++ b/cmd/bb/main.go @@ -94,11 +94,11 @@ func main() { log.Fatalf("error creating Discord session: %v\n", err) } - dg.AddHandler(command.PingHandler) + dg.AddHandler(command.NewPingHandler().Handle) dg.AddHandler(reactionHandler) dg.AddHandler(praiseHandler) - dg.AddHandler(command.RollHandler) - dg.AddHandler(command.RouletteHandler) + dg.AddHandler(command.NewRollHandler().Handle) + dg.AddHandler(command.NewRouletteHandler().Handle) h := command.NewWeatherHandler(C.Handler.Weather) dg.AddHandler(h.Handle) diff --git a/command/ping.go b/command/ping.go index 07e9faf..7697a2a 100644 --- a/command/ping.go +++ b/command/ping.go @@ -8,7 +8,15 @@ import ( log "github.com/sirupsen/logrus" ) -func PingHandler(s *discordgo.Session, m *discordgo.MessageCreate) { +type ( + PingHandler struct{} +) + +func NewPingHandler() *PingHandler { + return new(PingHandler) +} + +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 85f840a..49bd369 100644 --- a/command/random.go +++ b/command/random.go @@ -34,7 +34,9 @@ type ( N int } - CoinHandler struct{} + CoinHandler struct{} + RollHandler struct{} + RouletteHandler struct{} ) var ( @@ -140,7 +142,11 @@ func (g *Gun) IsEmpty() bool { return true } -func RollHandler(s *discordgo.Session, m *discordgo.MessageCreate) { +func NewRollHandler() *RollHandler { + return new(RollHandler) +} + +func (h *RollHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { var ( err error msg, roll string @@ -178,7 +184,11 @@ func RollHandler(s *discordgo.Session, m *discordgo.MessageCreate) { s.ChannelMessageSend(m.ChannelID, msg) } -func RouletteHandler(s *discordgo.Session, m *discordgo.MessageCreate) { +func NewRouletteHandler() *RouletteHandler { + return new(RouletteHandler) +} + +func (h *RouletteHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) { if m.Author.ID == s.State.User.ID { return } diff --git a/command/time.go b/command/time.go index d4ec7b0..630d3c9 100644 --- a/command/time.go +++ b/command/time.go @@ -14,8 +14,7 @@ type ( ) func NewTimeHandler() *TimeHandler { - var h *TimeHandler = new(TimeHandler) - return h + return new(TimeHandler) } func (h *TimeHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {