2021-06-10 13:27:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
httpHelpers "github.com/Luzifer/go_helpers/v2/http"
|
2021-08-25 22:31:20 +00:00
|
|
|
"github.com/Luzifer/go_helpers/v2/str"
|
2021-06-10 13:27:13 +00:00
|
|
|
"github.com/Luzifer/rconfig/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfg = struct {
|
2021-07-22 22:54:11 +00:00
|
|
|
Config string `flag:"config,c" default:"config.yaml" description:"Path to config file"`
|
2021-06-10 13:27:13 +00:00
|
|
|
Listen string `flag:"listen" default:":3000" description:"Port/IP to listen on"`
|
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
|
|
|
}{}
|
|
|
|
|
2021-07-22 22:54:11 +00:00
|
|
|
config *configFile
|
2021-08-07 14:16:14 +00:00
|
|
|
store *metaStore
|
2021-06-10 13:27:13 +00:00
|
|
|
|
|
|
|
version = "dev"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rconfig.AutoEnv(true)
|
|
|
|
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
|
|
|
log.Fatalf("Unable to parse commandline options: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.VersionAndExit {
|
2021-07-22 22:54:11 +00:00
|
|
|
fmt.Printf("discord-community %s\n", version)
|
2021-06-10 13:27:13 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if l, err := log.ParseLevel(cfg.LogLevel); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to parse log level")
|
|
|
|
} else {
|
|
|
|
log.SetLevel(l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-07-22 22:54:11 +00:00
|
|
|
var (
|
2021-08-07 13:24:11 +00:00
|
|
|
crontab = cron.New()
|
|
|
|
discord *discordgo.Session
|
|
|
|
err error
|
|
|
|
activeModules []module
|
2021-07-22 22:54:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if config, err = newConfigFromFile(cfg.Config); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to load config file")
|
|
|
|
}
|
2021-06-10 13:27:13 +00:00
|
|
|
|
2021-08-07 14:16:14 +00:00
|
|
|
if config.StoreLocation == "" {
|
|
|
|
log.Fatal("Config contains no store location")
|
|
|
|
}
|
|
|
|
|
|
|
|
if store, err = newMetaStoreFromDisk(config.StoreLocation); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to load store")
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:27:13 +00:00
|
|
|
// Connect to Discord
|
2021-07-22 22:54:11 +00:00
|
|
|
if discord, err = discordgo.New(strings.Join([]string{"Bot", config.BotToken}, " ")); err != nil {
|
2021-06-10 13:27:13 +00:00
|
|
|
log.WithError(err).Fatal("Unable to create discord client")
|
|
|
|
}
|
|
|
|
|
|
|
|
discord.Identify.Intents = discordgo.IntentsAll
|
|
|
|
|
2021-08-25 22:31:20 +00:00
|
|
|
var activeIDs []string
|
2021-07-25 11:42:38 +00:00
|
|
|
for i, mc := range config.ModuleConfigs {
|
|
|
|
logger := log.WithFields(log.Fields{
|
2021-08-07 14:16:14 +00:00
|
|
|
"id": mc.ID,
|
2021-07-25 11:42:38 +00:00
|
|
|
"idx": i,
|
|
|
|
"module": mc.Type,
|
|
|
|
})
|
2021-08-07 14:16:14 +00:00
|
|
|
|
2021-08-25 22:31:20 +00:00
|
|
|
if str.StringInSlice(mc.ID, activeIDs) {
|
|
|
|
logger.Error("Found duplicate module ID, module will be disabled")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:16:14 +00:00
|
|
|
if mc.ID == "" {
|
|
|
|
logger.Error("Module contains no ID and will be disabled")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-22 22:54:11 +00:00
|
|
|
mod := GetModuleByName(mc.Type)
|
|
|
|
if mod == nil {
|
|
|
|
logger.Fatal("Found configuration for unsupported module")
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:16:14 +00:00
|
|
|
if err = mod.Initialize(mc.ID, crontab, discord, mc.Attributes); err != nil {
|
2021-07-22 22:54:11 +00:00
|
|
|
logger.WithError(err).Fatal("Unable to initialize module")
|
|
|
|
}
|
2021-07-22 22:58:28 +00:00
|
|
|
|
2021-08-07 13:24:11 +00:00
|
|
|
activeModules = append(activeModules, mod)
|
2021-08-25 22:31:20 +00:00
|
|
|
activeIDs = append(activeIDs, mc.ID)
|
2021-08-07 13:24:11 +00:00
|
|
|
|
2021-07-22 22:58:28 +00:00
|
|
|
logger.Debug("Enabled module")
|
|
|
|
}
|
|
|
|
|
2021-08-07 13:24:11 +00:00
|
|
|
if len(activeModules) == 0 {
|
2021-07-22 22:58:28 +00:00
|
|
|
log.Warn("No modules were enabled, quitting now")
|
|
|
|
return
|
2021-06-10 13:27:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = discord.Open(); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to connect discord client")
|
|
|
|
}
|
|
|
|
defer discord.Close()
|
|
|
|
log.Debug("Discord connected")
|
|
|
|
|
2021-08-27 18:41:05 +00:00
|
|
|
guild, err := discord.Guild(config.GuildID)
|
|
|
|
if err != nil {
|
2021-08-27 18:44:19 +00:00
|
|
|
log.WithError(err).Fatal("Unable to get guild for given guild-id in config: Is the bot added and the ID correct?")
|
2021-08-27 18:41:05 +00:00
|
|
|
}
|
|
|
|
log.WithField("name", guild.Name).Info("Found specified guild for operation")
|
|
|
|
|
2021-06-10 13:27:13 +00:00
|
|
|
// Run Crontab
|
|
|
|
crontab.Start()
|
|
|
|
defer crontab.Stop()
|
|
|
|
log.Debug("Crontab started")
|
|
|
|
|
2021-08-07 13:24:11 +00:00
|
|
|
// Execute Setup methods now after we're connected
|
|
|
|
for i, mod := range activeModules {
|
|
|
|
if err = mod.Setup(); err != nil {
|
|
|
|
log.WithError(err).WithField("idx", i).Fatal("Unable to run setup for module")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 13:27:13 +00:00
|
|
|
// Run HTTP server
|
|
|
|
var h http.Handler = http.DefaultServeMux
|
|
|
|
h = httpHelpers.GzipHandler(h)
|
|
|
|
h = httpHelpers.NewHTTPLogHandler(h)
|
|
|
|
|
2021-08-25 22:38:20 +00:00
|
|
|
log.WithField("version", version).Info("Bot setup done, bot is now running")
|
2021-08-22 22:56:06 +00:00
|
|
|
|
2021-06-10 13:27:13 +00:00
|
|
|
http.ListenAndServe(cfg.Listen, h)
|
|
|
|
}
|