clessy-ng/main.go
Hamcha 42de49b13d
Some checks failed
continuous-integration/drone/push Build is failing
Use db not files!
2022-03-25 20:06:13 +01:00

83 lines
2.2 KiB
Go

package main
import (
"flag"
"log"
"net/url"
"strings"
"git.fromouter.space/crunchy-rocks/clessy-ng/modules"
"git.fromouter.space/crunchy-rocks/clessy-ng/modules/macro"
"git.fromouter.space/crunchy-rocks/clessy-ng/modules/metafora"
"git.fromouter.space/crunchy-rocks/clessy-ng/modules/proverbio"
"git.fromouter.space/crunchy-rocks/clessy-ng/modules/remind"
"git.fromouter.space/crunchy-rocks/clessy-ng/utils"
"git.fromouter.space/hamcha/tg"
"github.com/cockroachdb/pebble"
)
var mods = map[string]modules.Module{
"metafora": &metafora.Module{},
"proverbio": &proverbio.Module{},
"macro": &macro.Module{},
"remind": &remind.Module{},
}
func checkErr(err error, message string, args ...interface{}) {
if err != nil {
args = append(args, err)
log.Fatalf("FATAL: "+message+":\n\t%s", args...)
}
}
func main() {
disable := flag.String("disable", "", "Blacklist mods (separated by comma)")
enable := flag.String("enable", "", "Whitelist mods (separated by comma)")
flag.Parse()
api := tg.MakeAPIClient(utils.RequireEnv("CLESSY_TOKEN"))
name, err := api.GetMe()
checkErr(err, "could not retrieve bot info")
// Initialize your database
db, err := pebble.Open(utils.RequireEnv("CLESSY_DB_DIR"), &pebble.Options{})
checkErr(err, "could not open database")
defer db.Close()
toActivate := make(map[string]modules.Module)
if *disable != "" {
for _, modname := range strings.Split(*disable, ",") {
modname = strings.TrimSpace(modname)
delete(mods, modname)
}
toActivate = mods
} else if *enable != "" {
for _, modname := range strings.Split(*enable, ",") {
toActivate[modname] = mods[modname]
}
} else {
toActivate = mods
}
for modname, mod := range toActivate {
log.Printf("Initializing %s", modname)
err := mod.Initialize(modules.ModuleOptions{
API: api,
Name: name.Username,
KV: db,
})
checkErr(err, "Starting module %s failed with error", modname)
}
webhook := utils.RequireEnv("CLESSY_WEBHOOK")
uri, err := url.Parse(webhook)
checkErr(err, "Specified webhook is not a valid URL")
api.SetWebhook(webhook)
log.Fatal(api.HandleWebhook(utils.EnvFallback("CLESSY_BIND", ":8080"), uri.Path, func(update tg.APIUpdate) {
for _, mod := range toActivate {
mod.OnUpdate(update)
}
}))
}