simpbot/main.go
Steve ae50a68aea
All checks were successful
continuous-integration/drone/push Build is passing
gofumpt
2023-05-14 19:13:23 -04:00

97 lines
2.1 KiB
Go

package main
import (
"fmt"
"net/url"
"os"
"os/signal"
"sync"
"github.com/charmbracelet/log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
func main() {
simp := newSimp()
logger := log.NewWithOptions(os.Stderr, log.Options{
ReportCaller: true,
})
log.SetDefault(logger)
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.AddConfigPath("/etc/simpbot")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Fatal error config file: %v \n", err)
}
viper.SetConfigType("yaml")
Homeserver := viper.GetString("homeserver")
viper.SetDefault("domain", Homeserver)
viper.SetDefault("statefile", "simpstate")
Username := viper.GetString("username")
Password := viper.GetString("password")
Token := viper.GetString("access_token")
simp.holodexToken = viper.GetString("api_token")
HomeserverDomain := viper.GetString("domain")
DimensionServer := viper.GetString("dimension")
log.Info("logging into matrix", "homeserver", Homeserver, "username", Username)
err = simp.SetupMatrix(Username, Password, Token, Homeserver, HomeserverDomain, DimensionServer)
if err != nil {
log.Fatal(err)
}
if simp.holodexToken == "" {
log.Warn("No holodex API token provided, unlikely to be able to get streams")
}
simp.vtubers = loadVtubers()
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed,reloading vtubers:", e.Name)
simp.vtubers = loadVtubers()
})
var wg sync.WaitGroup
stop := make(chan bool)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
log.Info("trying to shutdown cleanly")
simp.Stop()
stop <- true
}()
wg.Add(1)
go func() {
err = simp.client.Sync()
if err != nil {
log.Error(err)
}
wg.Done()
}()
wg.Add(1)
go func() {
simp.Run()
wg.Done()
log.Info("simpbot shutdown")
}()
log.Info("simpbot running")
wg.Wait()
log.Info("shutting down")
}
func isValidURL(toTest string) bool {
_, err := url.ParseRequestURI(toTest)
if err != nil {
return false
}
u, err := url.Parse(toTest)
if err != nil || u.Scheme == "" || u.Host == "" {
return false
}
return true
}