bb/bot/bot.go

168 lines
2.9 KiB
Go
Raw Normal View History

package bot
2022-09-07 02:48:29 +00:00
import (
"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"
)
2022-09-07 05:29:28 +00:00
var C Config
2022-09-07 02:48:29 +00:00
2022-09-07 05:29:28 +00:00
type (
Bot struct {
Session *discordgo.Session
Config Config
2022-09-07 02:48:29 +00:00
}
2022-09-07 05:29:28 +00:00
MessageHandler func(s *discordgo.Session, m *discordgo.MessageCreate)
)
2022-09-08 06:45:16 +00:00
func init() {
pflag.Bool("debug", false, "enable debug mode")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
}
func NewBot(s *discordgo.Session, config Config) *Bot {
return &Bot{Session: s, Config: config}
}
func (b *Bot) RegisterCommands() {
AddCommand(&Command{
Name: "coin",
Func: b.CoinCommand(),
})
AddCommand(&Command{
Name: "deal",
Func: b.DealCommand(),
NArgs: 1,
})
AddCommand(&Command{
Name: "ping",
Func: b.PingCommand(),
})
AddCommand(&Command{
Name: "roll",
Func: b.RollCommand(),
NArgs: 1,
})
AddCommand(&Command{
Name: "time",
Func: b.TimeCommand(),
NArgs: 1,
})
AddCommand(&Command{
Name: "version",
Func: b.VersionCommand(),
})
AddCommand(&Command{
Name: "weather",
Func: b.WeatherCommand(),
NArgs: 1,
})
}
2022-09-07 02:48:29 +00:00
2022-09-07 05:29:28 +00:00
func (b *Bot) RegisterHandlers() {
b.Session.AddHandler(b.CommandHandler())
b.Session.AddHandler(b.ReactionHandler())
}
2022-09-07 02:48:29 +00:00
func Run() error {
initConfig()
2022-09-08 07:35:39 +00:00
go reloadConfig()
2022-09-07 02:48:29 +00:00
2022-09-07 05:54:58 +00:00
if err := lib.SeedMathRand(); err != nil {
log.Warn(err)
}
2022-09-07 02:48:29 +00:00
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)
}
b := NewBot(dg, C)
2022-09-07 05:29:28 +00:00
b.RegisterHandlers()
2022-09-07 02:48:29 +00:00
b.RegisterCommands()
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
2022-09-08 07:28:58 +00:00
if err = dg.Open(); err != nil {
2022-09-07 02:48:29 +00:00
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)
2022-09-07 05:54:58 +00:00
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
2022-09-07 02:48:29 +00:00
<-sc
log.Info("Shutting down")
return nil
}
func initConfig() {
2022-09-07 02:48:29 +00:00
C = NewConfig()
viper.SetEnvPrefix("BEEPBOOP")
viper.AutomaticEnv()
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath(".")
viper.SetDefault("debug", false)
2022-09-07 02:48:29 +00:00
viper.BindEnv("DISCORD_TOKEN")
viper.BindEnv("OPEN_WEATHER_MAP_TOKEN")
2022-09-08 07:26:47 +00:00
loadConfig()
}
func loadConfig() {
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.Fatalf("fatal error config file: %v", err)
}
2022-09-07 02:48:29 +00:00
}
2022-09-08 07:35:01 +00:00
log.WithField("filename", viper.ConfigFileUsed()).Info(
"loaded configuration file",
)
2022-09-08 07:26:47 +00:00
err := viper.Unmarshal(&C)
2022-09-07 02:48:29 +00:00
if err != nil {
log.Fatalf("unable to decode into struct: %v", err)
}
if viper.GetBool("debug") {
log.SetLevel(log.DebugLevel)
2022-09-08 07:35:39 +00:00
} else {
log.SetLevel(log.InfoLevel)
}
}
func reloadConfig() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGHUP)
for {
<-sc
loadConfig()
2022-09-07 02:48:29 +00:00
}
}