Move message handler to own function

Move intents into the config
Reindent files with spaces
This commit is contained in:
Michał Gdula 2025-02-28 00:34:09 +00:00
parent 78dcdc0b46
commit b93820e9d0
3 changed files with 45 additions and 41 deletions

View file

@ -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, " "))
}