catbase/main.go

99 lines
2.3 KiB
Go
Raw Normal View History

2016-01-17 18:00:44 +00:00
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package main
2024-02-27 15:11:01 +00:00
//go:generate templ generate
import (
"flag"
"github.com/velour/catbase/plugins"
2024-09-28 14:28:58 +00:00
"github.com/velour/catbase/plugins/talklikeapirate"
"io"
"math/rand"
"os"
"time"
2019-03-07 16:35:42 +00:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2024-03-07 16:53:49 +00:00
"github.com/velour/catbase/bot/msg"
"github.com/velour/catbase/connectors/discord"
2019-03-07 16:35:42 +00:00
2016-01-17 18:00:44 +00:00
"github.com/velour/catbase/bot"
"github.com/velour/catbase/config"
"github.com/velour/catbase/connectors/irc"
"github.com/velour/catbase/connectors/slackapp"
)
var (
2019-03-07 16:35:42 +00:00
key = flag.String("set", "", "Configuration key to set")
val = flag.String("val", "", "Configuration value to set")
initDB = flag.Bool("init", false, "Initialize the configuration db")
2019-03-07 16:35:42 +00:00
prettyLog = flag.Bool("pretty", false, "Use pretty console logger")
debug = flag.Bool("debug", false, "Turn on debug logging")
)
func main() {
rand.Seed(time.Now().Unix())
var dbpath = flag.String("db", "catbase.db",
"Database file to load. (Defaults to catbase.db)")
flag.Parse() // parses the logging flags.
2021-05-04 17:57:57 +00:00
var output io.Writer = os.Stdout
2019-03-07 16:35:42 +00:00
if *prettyLog {
2021-05-04 17:57:57 +00:00
output = zerolog.ConsoleWriter{Out: output}
2019-03-07 16:35:42 +00:00
}
2021-05-04 17:57:57 +00:00
log.Logger = log.Output(output).With().Caller().Stack().Logger()
2019-03-07 16:35:42 +00:00
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
c := config.ReadConfig(*dbpath)
if *key != "" && *val != "" {
c.Set(*key, *val)
2019-03-07 16:35:42 +00:00
log.Info().Msgf("Set config %s: %s", *key, *val)
return
}
2019-01-22 00:16:57 +00:00
if (*initDB && len(flag.Args()) != 2) || (!*initDB && c.GetInt("init", 0) != 1) {
2019-03-07 16:35:42 +00:00
log.Fatal().Msgf(`You must run "catbase -init <channel> <nick>"`)
} else if *initDB {
c.SetDefaults(flag.Arg(0), flag.Arg(1))
return
}
2016-03-10 18:37:07 +00:00
var client bot.Connector
switch c.Get("type", "slackapp") {
2016-03-10 18:37:07 +00:00
case "irc":
client = irc.New(c)
case "slackapp":
client = slackapp.New(c)
2020-08-31 16:20:52 +00:00
case "discord":
2024-09-28 14:28:58 +00:00
d := discord.New(c)
d.Pirate = talklikeapirate.New(c)
client = d
2016-03-10 18:37:07 +00:00
default:
2019-03-07 16:35:42 +00:00
log.Fatal().Msgf("Unknown connection type: %s", c.Get("type", "UNSET"))
}
b := bot.New(c, client)
2016-03-10 18:37:07 +00:00
if r, path := client.GetRouter(); r != nil {
2024-02-27 19:29:54 +00:00
b.GetWeb().RegisterWeb(r, path)
}
2024-03-07 16:53:49 +00:00
plugins.Register(b)
if err := client.Serve(); err != nil {
2019-03-07 16:35:42 +00:00
log.Fatal().Err(err)
}
2022-07-23 04:17:44 +00:00
log.Debug().Msgf("Sending bot.Startup message")
b.Receive(client, bot.Startup, msg.Message{})
b.ListenAndServe()
}