clessy-ng/main.go

84 lines
2.2 KiB
Go
Raw Normal View History

2022-03-24 11:47:37 +00:00
package main
import (
"flag"
"log"
2022-03-25 19:06:13 +00:00
"net/url"
2022-03-24 11:47:37 +00:00
"strings"
"git.fromouter.space/crunchy-rocks/clessy-ng/modules"
2022-03-24 15:28:00 +00:00
"git.fromouter.space/crunchy-rocks/clessy-ng/modules/macro"
2022-03-24 11:47:37 +00:00
"git.fromouter.space/crunchy-rocks/clessy-ng/modules/metafora"
2022-03-24 15:28:00 +00:00
"git.fromouter.space/crunchy-rocks/clessy-ng/modules/proverbio"
2022-03-25 14:29:14 +00:00
"git.fromouter.space/crunchy-rocks/clessy-ng/modules/remind"
2022-03-25 19:06:13 +00:00
"git.fromouter.space/crunchy-rocks/clessy-ng/utils"
2022-03-24 11:47:37 +00:00
"git.fromouter.space/hamcha/tg"
2022-03-25 19:06:13 +00:00
"github.com/cockroachdb/pebble"
2022-03-24 11:47:37 +00:00
)
var mods = map[string]modules.Module{
2022-03-24 15:28:00 +00:00
"metafora": &metafora.Module{},
"proverbio": &proverbio.Module{},
"macro": &macro.Module{},
2022-03-25 14:29:14 +00:00
"remind": &remind.Module{},
2022-03-24 11:47:37 +00:00
}
func checkErr(err error, message string, args ...interface{}) {
if err != nil {
args = append(args, err)
2022-03-24 15:28:00 +00:00
log.Fatalf("FATAL: "+message+":\n\t%s", args...)
2022-03-24 11:47:37 +00:00
}
}
func main() {
disable := flag.String("disable", "", "Blacklist mods (separated by comma)")
enable := flag.String("enable", "", "Whitelist mods (separated by comma)")
flag.Parse()
2022-03-25 19:06:13 +00:00
api := tg.MakeAPIClient(utils.RequireEnv("CLESSY_TOKEN"))
2022-03-24 11:47:37 +00:00
name, err := api.GetMe()
checkErr(err, "could not retrieve bot info")
2022-03-25 19:06:13 +00:00
// Initialize your database
db, err := pebble.Open(utils.RequireEnv("CLESSY_DB_DIR"), &pebble.Options{})
checkErr(err, "could not open database")
defer db.Close()
2022-03-24 11:47:37 +00:00
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]
}
2022-03-24 12:24:20 +00:00
} else {
toActivate = mods
2022-03-24 11:47:37 +00:00
}
for modname, mod := range toActivate {
log.Printf("Initializing %s", modname)
2022-03-25 19:06:13 +00:00
err := mod.Initialize(modules.ModuleOptions{
API: api,
Name: name.Username,
KV: db,
})
2022-03-24 11:47:37 +00:00
checkErr(err, "Starting module %s failed with error", modname)
}
2022-03-25 19:06:13 +00:00
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) {
2022-03-24 12:24:20 +00:00
for _, mod := range toActivate {
mod.OnUpdate(update)
}
}))
2022-03-24 11:47:37 +00:00
}