mirror of
https://github.com/Fluffy-Bean/Lynxie.git
synced 2025-05-14 08:02:17 +00:00
Move message handler to own function
Move intents into the config Reindent files with spaces
This commit is contained in:
parent
78dcdc0b46
commit
b93820e9d0
3 changed files with 45 additions and 41 deletions
50
app/app.go
50
app/app.go
|
@ -11,8 +11,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Token string
|
|
||||||
Prefix string
|
Prefix string
|
||||||
|
Token string
|
||||||
|
Intents discordgo.Intent
|
||||||
}
|
}
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
|
@ -20,11 +21,6 @@ type App struct {
|
||||||
Commands map[string]func(h *Handler, args []string)
|
Commands map[string]func(h *Handler, args []string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Handler struct {
|
|
||||||
Session *discordgo.Session
|
|
||||||
Message *discordgo.MessageCreate
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewApp(config Config) *App {
|
func NewApp(config Config) *App {
|
||||||
return &App{
|
return &App{
|
||||||
Config: config,
|
Config: config,
|
||||||
|
@ -44,7 +40,30 @@ func (a *App) Run() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
dg.AddHandler(func(session *discordgo.Session, message *discordgo.MessageCreate) {
|
dg.AddHandler(a.handler)
|
||||||
|
dg.Identify.Intents = a.Config.Intents
|
||||||
|
|
||||||
|
err = dg.Open()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error opening connection,", err)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Bot is now running. Press CTRL-C to exit.")
|
||||||
|
sc := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
||||||
|
<-sc
|
||||||
|
|
||||||
|
dg.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
type Handler struct {
|
||||||
|
Session *discordgo.Session
|
||||||
|
Message *discordgo.MessageCreate
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) handler(session *discordgo.Session, message *discordgo.MessageCreate) {
|
||||||
h := &Handler{
|
h := &Handler{
|
||||||
Session: session,
|
Session: session,
|
||||||
Message: message,
|
Message: message,
|
||||||
|
@ -72,21 +91,4 @@ func (a *App) Run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(h, strings.Split(args, " "))
|
callback(h, strings.Split(args, " "))
|
||||||
})
|
|
||||||
|
|
||||||
dg.Identify.Intents = discordgo.IntentsGuildMessages
|
|
||||||
|
|
||||||
err = dg.Open()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("error opening connection,", err)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Bot is now running. Press CTRL-C to exit.")
|
|
||||||
sc := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
|
||||||
<-sc
|
|
||||||
|
|
||||||
dg.Close()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ func registerAnimal(a *app.App) func(h *app.Handler, args []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := flag.NewFlagSet("pong", flag.ContinueOnError)
|
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)
|
cmd.Parse(args)
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, "https://api.tinyfox.dev/img?animal="+options.animal, nil)
|
req, err := http.NewRequest(http.MethodGet, "https://api.tinyfox.dev/img?animal="+options.animal, nil)
|
||||||
|
|
4
main.go
4
main.go
|
@ -5,12 +5,14 @@ import (
|
||||||
|
|
||||||
"github.com/Fluffy-Bean/lynxie/app"
|
"github.com/Fluffy-Bean/lynxie/app"
|
||||||
"github.com/Fluffy-Bean/lynxie/commands"
|
"github.com/Fluffy-Bean/lynxie/commands"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
a := app.NewApp(app.Config{
|
a := app.NewApp(app.Config{
|
||||||
Token: os.Getenv("TOKEN"),
|
|
||||||
Prefix: "?",
|
Prefix: "?",
|
||||||
|
Token: os.Getenv("TOKEN"),
|
||||||
|
Intents: discordgo.IntentsGuildMessages,
|
||||||
})
|
})
|
||||||
|
|
||||||
commands.RegisterMetaCommands(a)
|
commands.RegisterMetaCommands(a)
|
||||||
|
|
Loading…
Add table
Reference in a new issue