From b93820e9d0313e4af81d6bc07fa60a0918ba06c7 Mon Sep 17 00:00:00 2001 From: Fluffy-Bean Date: Fri, 28 Feb 2025 00:34:09 +0000 Subject: [PATCH] Move message handler to own function Move intents into the config Reindent files with spaces --- app/app.go | 78 +++++++++++++++++++++++---------------------- commands/tinyfox.go | 2 +- main.go | 6 ++-- 3 files changed, 45 insertions(+), 41 deletions(-) diff --git a/app/app.go b/app/app.go index cd05e6e..8c3af1e 100644 --- a/app/app.go +++ b/app/app.go @@ -11,8 +11,9 @@ import ( ) type Config struct { - Token string - Prefix string + Prefix string + Token string + Intents discordgo.Intent } type App struct { @@ -20,11 +21,6 @@ type App struct { Commands map[string]func(h *Handler, args []string) } -type Handler struct { - Session *discordgo.Session - Message *discordgo.MessageCreate -} - func NewApp(config Config) *App { return &App{ Config: config, @@ -44,37 +40,8 @@ func (a *App) Run() { return } - dg.AddHandler(func(session *discordgo.Session, message *discordgo.MessageCreate) { - h := &Handler{ - Session: session, - Message: message, - } - - if h.Message.Author.ID == h.Session.State.User.ID { - return - } - - if h.Message.Author.Bot { - return - } - - var command string - var args string - - command = h.Message.Content - command = strings.TrimSpace(command) - command = strings.TrimPrefix(command, a.Config.Prefix) - command, args, _ = strings.Cut(command, " ") - - callback, ok := a.Commands[command] - if !ok { - return - } - - callback(h, strings.Split(args, " ")) - }) - - dg.Identify.Intents = discordgo.IntentsGuildMessages + dg.AddHandler(a.handler) + dg.Identify.Intents = a.Config.Intents err = dg.Open() if err != nil { @@ -90,3 +57,38 @@ func (a *App) Run() { dg.Close() } + +type Handler struct { + Session *discordgo.Session + Message *discordgo.MessageCreate +} + +func (a *App) handler(session *discordgo.Session, message *discordgo.MessageCreate) { + h := &Handler{ + Session: session, + Message: message, + } + + if h.Message.Author.ID == h.Session.State.User.ID { + return + } + + if h.Message.Author.Bot { + return + } + + var command string + var args string + + command = h.Message.Content + command = strings.TrimSpace(command) + command = strings.TrimPrefix(command, a.Config.Prefix) + command, args, _ = strings.Cut(command, " ") + + callback, ok := a.Commands[command] + if !ok { + return + } + + callback(h, strings.Split(args, " ")) +} diff --git a/commands/tinyfox.go b/commands/tinyfox.go index 78dc1af..df0c027 100644 --- a/commands/tinyfox.go +++ b/commands/tinyfox.go @@ -23,7 +23,7 @@ func registerAnimal(a *app.App) func(h *app.Handler, args []string) { } cmd := flag.NewFlagSet("pong", flag.ContinueOnError) - cmd.StringVar(&options.animal, "animal", "serval", "Get an image of an animal!") + cmd.StringVar(&options.animal, "animal", "wah", "Get an image of an animal!") cmd.Parse(args) req, err := http.NewRequest(http.MethodGet, "https://api.tinyfox.dev/img?animal="+options.animal, nil) diff --git a/main.go b/main.go index dae9d2e..0c7f46d 100644 --- a/main.go +++ b/main.go @@ -5,12 +5,14 @@ import ( "github.com/Fluffy-Bean/lynxie/app" "github.com/Fluffy-Bean/lynxie/commands" + "github.com/bwmarrin/discordgo" ) func main() { a := app.NewApp(app.Config{ - Token: os.Getenv("TOKEN"), - Prefix: "?", + Prefix: "?", + Token: os.Getenv("TOKEN"), + Intents: discordgo.IntentsGuildMessages, }) commands.RegisterMetaCommands(a)