Use HasCommand() helper

This commit is contained in:
Ryan Cavicchioni 2022-08-27 09:00:35 -05:00
parent 424b191ebc
commit af10ef4dc5
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
7 changed files with 41 additions and 25 deletions

View File

@ -1,8 +1,6 @@
package handler package handler
import ( import (
"strings"
"git.kill0.net/chill9/beepboop/bot" "git.kill0.net/chill9/beepboop/bot"
"git.kill0.net/chill9/beepboop/lib" "git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
@ -13,6 +11,7 @@ type (
CoinHandler struct { CoinHandler struct {
config bot.Config config bot.Config
Name string
} }
) )
@ -21,8 +20,10 @@ func (c *Coin) Flip() bool {
return bool(*c) return bool(*c)
} }
func NewCoinHandler() *CoinHandler { func NewCoinHandler(s string) *CoinHandler {
return new(CoinHandler) h := new(CoinHandler)
h.Name = s
return h
} }
func (h *CoinHandler) SetConfig(config bot.Config) { func (h *CoinHandler) SetConfig(config bot.Config) {
@ -39,7 +40,7 @@ func (h *CoinHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
return return
} }
if !strings.HasPrefix(m.Content, "!coin") { if !lib.HasCommand(m.Content, h.config.Prefix, h.Name) {
return return
} }

View File

@ -28,6 +28,7 @@ type (
RollHandler struct { RollHandler struct {
config bot.Config config bot.Config
Name string
} }
) )
@ -83,8 +84,10 @@ func (r *Roll) RollDice() {
} }
} }
func NewRollHandler() *RollHandler { func NewRollHandler(s string) *RollHandler {
return new(RollHandler) h := new(RollHandler)
h.Name = s
return h
} }
func (h *RollHandler) SetConfig(config bot.Config) { func (h *RollHandler) SetConfig(config bot.Config) {
@ -102,7 +105,7 @@ func (h *RollHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
return return
} }
if !strings.HasPrefix(m.Content, "!roll") { if !lib.HasCommand(m.Content, h.config.Prefix, h.Name) {
return return
} }

View File

@ -1,9 +1,8 @@
package handler package handler
import ( import (
"strings"
"git.kill0.net/chill9/beepboop/bot" "git.kill0.net/chill9/beepboop/bot"
"git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -11,11 +10,14 @@ import (
type ( type (
PingHandler struct { PingHandler struct {
config bot.Config config bot.Config
Name string
} }
) )
func NewPingHandler() *PingHandler { func NewPingHandler(s string) *PingHandler {
return new(PingHandler) h := new(PingHandler)
h.Name = s
return h
} }
func (h *PingHandler) SetConfig(config bot.Config) { func (h *PingHandler) SetConfig(config bot.Config) {
@ -27,7 +29,7 @@ func (h *PingHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
return return
} }
if !strings.HasPrefix(m.Content, "!ping") { if !lib.HasCommand(m.Content, h.config.Prefix, h.Name) {
return return
} }

View File

@ -24,6 +24,7 @@ type (
RouletteHandler struct { RouletteHandler struct {
config bot.Config config bot.Config
Name string
} }
) )
@ -73,8 +74,10 @@ func (g *Gun) IsEmpty() bool {
return true return true
} }
func NewRouletteHandler() *RouletteHandler { func NewRouletteHandler(s string) *RouletteHandler {
return new(RouletteHandler) h := new(RouletteHandler)
h.Name = s
return h
} }
func (h *RouletteHandler) SetConfig(config bot.Config) { func (h *RouletteHandler) SetConfig(config bot.Config) {

View File

@ -13,11 +13,14 @@ import (
type ( type (
TimeHandler struct { TimeHandler struct {
config bot.Config config bot.Config
Name string
} }
) )
func NewTimeHandler() *TimeHandler { func NewTimeHandler(s string) *TimeHandler {
return new(TimeHandler) h := new(TimeHandler)
h.Name = s
return h
} }
func (h *TimeHandler) SetConfig(config bot.Config) { func (h *TimeHandler) SetConfig(config bot.Config) {

View File

@ -24,6 +24,7 @@ var (
type ( type (
WeatherHandler struct { WeatherHandler struct {
Config bot.Config Config bot.Config
Name string
} }
Temperature float32 Temperature float32
@ -66,8 +67,10 @@ func (t *Temperature) Celcius() float32 {
return float32(*t) - 273.15 return float32(*t) - 273.15
} }
func NewWeatherHandler() *WeatherHandler { func NewWeatherHandler(s string) *WeatherHandler {
return new(WeatherHandler) h := new(WeatherHandler)
h.Name = s
return h
} }
func (h *WeatherHandler) SetConfig(config bot.Config) { func (h *WeatherHandler) SetConfig(config bot.Config) {

View File

@ -24,14 +24,15 @@ var (
C bot.Config C bot.Config
handlers []bot.MessageCreateHandler = []bot.MessageCreateHandler{ handlers []bot.MessageCreateHandler = []bot.MessageCreateHandler{
handler.NewCoinHandler(), handler.NewCoinHandler("coin"),
handler.NewPingHandler(), handler.NewPingHandler("ping"),
handler.NewRollHandler(), handler.NewRollHandler("roll"),
handler.NewRouletteHandler(), handler.NewRouletteHandler("roulette"),
handler.NewTimeHandler(), handler.NewTimeHandler("time"),
handler.NewVersionHandler("version"), handler.NewVersionHandler("version"),
handler.NewWeatherHandler(), handler.NewWeatherHandler("weather"),
handler.NewReactionHandler(), handler.NewReactionHandler(),
handler.NewDealHandler("deal"),
} }
) )