catbase/main.go

147 lines
4.2 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
import (
"flag"
"math/rand"
"net/http"
2019-03-07 16:35:42 +00:00
"os"
"time"
2019-10-21 00:55:13 +00:00
"github.com/velour/catbase/plugins/twitter"
2019-03-07 16:35:42 +00:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
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/slack"
"github.com/velour/catbase/connectors/slackapp"
"github.com/velour/catbase/plugins/admin"
2016-05-15 18:13:34 +00:00
"github.com/velour/catbase/plugins/babbler"
"github.com/velour/catbase/plugins/beers"
"github.com/velour/catbase/plugins/cli"
2018-06-22 18:25:43 +00:00
"github.com/velour/catbase/plugins/couldashouldawoulda"
2016-04-04 14:33:32 +00:00
"github.com/velour/catbase/plugins/counter"
"github.com/velour/catbase/plugins/dice"
"github.com/velour/catbase/plugins/emojifyme"
"github.com/velour/catbase/plugins/fact"
2017-08-18 09:59:46 +00:00
"github.com/velour/catbase/plugins/first"
"github.com/velour/catbase/plugins/fuck"
"github.com/velour/catbase/plugins/git"
2017-09-29 04:58:21 +00:00
"github.com/velour/catbase/plugins/inventory"
2016-03-25 16:25:00 +00:00
"github.com/velour/catbase/plugins/leftpad"
2019-01-19 00:48:12 +00:00
"github.com/velour/catbase/plugins/nerdepedia"
"github.com/velour/catbase/plugins/newsbid"
2017-12-19 18:37:47 +00:00
"github.com/velour/catbase/plugins/picker"
"github.com/velour/catbase/plugins/reaction"
"github.com/velour/catbase/plugins/remember"
2016-05-09 17:09:17 +00:00
"github.com/velour/catbase/plugins/reminder"
"github.com/velour/catbase/plugins/rpgORdie"
2017-05-10 19:15:24 +00:00
"github.com/velour/catbase/plugins/rss"
2017-11-03 18:01:49 +00:00
"github.com/velour/catbase/plugins/sisyphus"
2019-06-07 18:32:20 +00:00
"github.com/velour/catbase/plugins/stock"
"github.com/velour/catbase/plugins/talker"
2017-11-16 22:06:48 +00:00
"github.com/velour/catbase/plugins/tell"
"github.com/velour/catbase/plugins/tldr"
"github.com/velour/catbase/plugins/twitch"
"github.com/velour/catbase/plugins/your"
"github.com/velour/catbase/plugins/zork"
)
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")
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.
log.Logger = log.With().Caller().Stack().Logger()
2019-03-07 16:35:42 +00:00
if *prettyLog {
log.Logger = log.Logger.Output(zerolog.ConsoleWriter{Out: os.Stderr})
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)
2016-03-11 02:11:52 +00:00
case "slack":
client = slack.New(c)
case "slackapp":
client = slackapp.New(c)
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
b.AddPlugin(admin.New(b))
b.AddPlugin(emojifyme.New(b))
b.AddPlugin(first.New(b))
b.AddPlugin(leftpad.New(b))
b.AddPlugin(fuck.New(b))
b.AddPlugin(talker.New(b))
b.AddPlugin(dice.New(b))
b.AddPlugin(picker.New(b))
b.AddPlugin(beers.New(b))
b.AddPlugin(remember.New(b))
b.AddPlugin(your.New(b))
b.AddPlugin(counter.New(b))
b.AddPlugin(reminder.New(b))
b.AddPlugin(babbler.New(b))
b.AddPlugin(zork.New(b))
b.AddPlugin(rss.New(b))
b.AddPlugin(reaction.New(b))
b.AddPlugin(twitch.New(b))
b.AddPlugin(inventory.New(b))
b.AddPlugin(rpgORdie.New(b))
b.AddPlugin(sisyphus.New(b))
b.AddPlugin(tell.New(b))
b.AddPlugin(couldashouldawoulda.New(b))
b.AddPlugin(nerdepedia.New(b))
b.AddPlugin(tldr.New(b))
2019-06-07 18:32:20 +00:00
b.AddPlugin(stock.New(b))
b.AddPlugin(newsbid.New(b))
2019-10-21 00:55:13 +00:00
b.AddPlugin(twitter.New(b))
b.AddPlugin(git.New(b))
2019-05-27 23:21:53 +00:00
b.AddPlugin(cli.New(b))
// catches anything left, will always return true
b.AddPlugin(fact.New(b))
if err := client.Serve(); err != nil {
2019-03-07 16:35:42 +00:00
log.Fatal().Err(err)
}
addr := c.Get("HttpAddr", "127.0.0.1:1337")
2019-03-07 16:35:42 +00:00
log.Fatal().Err(http.ListenAndServe(addr, nil))
}