Move code out of main.go

This commit is contained in:
Ryan Cavicchioni 2022-09-06 21:48:29 -05:00
parent 5651df37ef
commit 31cf6f6c9a
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D
3 changed files with 107 additions and 102 deletions

View File

@ -1,6 +1,26 @@
package bot package bot
import "github.com/bwmarrin/discordgo" import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
C Config
handlers []MessageCreateHandler = []MessageCreateHandler{
NewReactionHandler(),
}
)
type Bot struct { type Bot struct {
Session *discordgo.Session Session *discordgo.Session
@ -45,3 +65,84 @@ func (b *Bot) RegisterCommands() {
NArgs: 1, NArgs: 1,
}) })
} }
func Run() error {
setupConfig()
lib.SeedMathRand()
if C.DiscordToken == "" {
log.Fatalf("Discord token is not set")
}
dg, err := discordgo.New(fmt.Sprintf("Bot %s", C.DiscordToken))
if err != nil {
log.Fatalf("error creating Discord session: %v\n", err)
}
for _, h := range handlers {
h.SetConfig(C)
dg.AddHandler(h.Handle)
}
b := NewBot(dg, C)
b.RegisterCommands()
dg.AddHandler(NewCommandHandler(b))
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
err = dg.Open()
if err != nil {
log.Fatalf("error opening connection: %v\n", err)
}
log.Info("The bot is now running. Press CTRL-C to exit.")
defer dg.Close()
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
log.Info("Shutting down")
return nil
}
func setupConfig() {
var err error
C = NewConfig()
flag.Bool("debug", false, "enable debug logging")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
viper.SetEnvPrefix("BEEPBOOP")
viper.AutomaticEnv()
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath(".")
err = viper.ReadInConfig()
viper.BindEnv("DEBUG")
viper.BindEnv("DISCORD_TOKEN")
viper.BindEnv("OPEN_WEATHER_MAP_TOKEN")
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.Fatalf("fatal error config file: %v", err)
}
err = viper.Unmarshal(&C)
if err != nil {
log.Fatalf("unable to decode into struct: %v", err)
}
if viper.GetBool("debug") {
log.SetLevel(log.DebugLevel)
}
}

View File

@ -1,10 +1,9 @@
package handler package bot
import ( import (
"math/rand" "math/rand"
"strings" "strings"
"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,7 +12,7 @@ import (
type ( type (
ReactionHandler struct { ReactionHandler struct {
Config bot.Config Config Config
} }
) )
@ -21,7 +20,7 @@ func NewReactionHandler() *ReactionHandler {
return new(ReactionHandler) return new(ReactionHandler)
} }
func (h *ReactionHandler) SetConfig(config bot.Config) { func (h *ReactionHandler) SetConfig(config Config) {
h.Config = config h.Config = config
} }

View File

@ -1,108 +1,13 @@
package main package main
import ( import (
"flag"
"fmt"
//"log"
"os" "os"
"os/signal"
"syscall"
"git.kill0.net/chill9/beepboop/bot" "git.kill0.net/chill9/beepboop/bot"
"git.kill0.net/chill9/beepboop/bot/handler"
"git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
var (
C bot.Config
handlers []bot.MessageCreateHandler = []bot.MessageCreateHandler{
handler.NewReactionHandler(),
}
) )
func main() { func main() {
setupConfig() if err := bot.Run(); err != nil {
os.Exit(1)
lib.SeedMathRand()
if C.DiscordToken == "" {
log.Fatalf("Discord token is not set")
}
dg, err := discordgo.New(fmt.Sprintf("Bot %s", C.DiscordToken))
if err != nil {
log.Fatalf("error creating Discord session: %v\n", err)
}
for _, h := range handlers {
h.SetConfig(C)
dg.AddHandler(h.Handle)
}
b := bot.NewBot(dg, C)
b.RegisterCommands()
dg.AddHandler(bot.NewCommandHandler(b))
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
err = dg.Open()
if err != nil {
log.Fatalf("error opening connection: %v\n", err)
}
log.Info("The bot is now running. Press CTRL-C to exit.")
defer dg.Close()
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
log.Info("Shutting down")
}
func setupConfig() {
var err error
C = bot.NewConfig()
flag.Bool("debug", false, "enable debug logging")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
viper.SetEnvPrefix("BEEPBOOP")
viper.AutomaticEnv()
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath(".")
err = viper.ReadInConfig()
viper.BindEnv("DEBUG")
viper.BindEnv("DISCORD_TOKEN")
viper.BindEnv("OPEN_WEATHER_MAP_TOKEN")
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.Fatalf("fatal error config file: %v", err)
}
err = viper.Unmarshal(&C)
if err != nil {
log.Fatalf("unable to decode into struct: %v", err)
}
if viper.GetBool("debug") {
log.SetLevel(log.DebugLevel)
} }
} }