simpbot/main.go

96 lines
2.1 KiB
Go
Raw Normal View History

2021-05-21 12:45:24 -04:00
package main
import (
"fmt"
2021-10-26 14:14:26 -04:00
"net/url"
2021-08-07 13:02:50 -04:00
"os"
"os/signal"
2023-05-14 18:45:55 -04:00
"sync"
2021-05-21 12:45:24 -04:00
2023-05-14 18:45:55 -04:00
"github.com/charmbracelet/log"
2021-08-07 13:02:50 -04:00
"github.com/fsnotify/fsnotify"
2021-05-21 13:39:54 -04:00
"github.com/spf13/viper"
2021-05-21 12:45:24 -04:00
)
func main() {
2023-05-14 18:45:55 -04:00
simp := newSimp()
2023-05-14 19:09:49 -04:00
logger := log.NewWithOptions(os.Stderr, log.Options{
ReportCaller: true,
})
log.SetDefault(logger)
2021-05-21 13:39:54 -04:00
viper.SetConfigName("config")
viper.AddConfigPath(".")
2021-08-07 13:36:26 -04:00
viper.AddConfigPath("/etc/simpbot")
2021-05-21 13:39:54 -04:00
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Fatal error config file: %v \n", err)
2021-05-21 12:45:24 -04:00
}
2021-05-21 13:39:54 -04:00
viper.SetConfigType("yaml")
2023-05-14 18:45:55 -04:00
Homeserver := viper.GetString("homeserver")
2021-05-21 13:39:54 -04:00
viper.SetDefault("domain", Homeserver)
2023-05-14 18:45:55 -04:00
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")
2022-08-10 14:03:35 -04:00
2023-05-14 18:45:55 -04:00
log.Info("logging into matrix", "homeserver", Homeserver, "username", Username)
err = simp.SetupMatrix(Username, Password, Token, Homeserver, HomeserverDomain, DimensionServer)
if err != nil {
log.Fatal(err)
2021-05-21 12:45:24 -04:00
}
2023-05-14 18:45:55 -04:00
if simp.holodexToken == "" {
log.Warn("No holodex API token provided, unlikely to be able to get streams")
2023-04-24 18:01:40 -04:00
}
2023-05-14 18:45:55 -04:00
simp.vtubers = loadVtubers()
2021-08-07 13:02:50 -04:00
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed,reloading vtubers:", e.Name)
2023-05-14 18:45:55 -04:00
simp.vtubers = loadVtubers()
2021-08-07 13:02:50 -04:00
})
2023-05-14 18:45:55 -04:00
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
}()
2021-05-21 12:45:24 -04:00
2023-05-14 18:45:55 -04:00
wg.Add(1)
2021-05-21 12:45:24 -04:00
go func() {
2023-05-14 18:45:55 -04:00
err = simp.client.Sync()
if err != nil {
2023-05-14 19:09:49 -04:00
log.Error(err)
2021-05-21 12:45:24 -04:00
}
2023-05-14 18:45:55 -04:00
wg.Done()
2021-05-21 12:45:24 -04:00
}()
2023-05-14 18:45:55 -04:00
wg.Add(1)
go func() {
simp.Run()
wg.Done()
log.Info("simpbot shutdown")
}()
log.Info("simpbot running")
wg.Wait()
log.Info("shutting down")
2021-05-21 12:45:24 -04:00
}
2021-10-26 14:14:26 -04:00
2023-05-14 18:45:55 -04:00
func isValidURL(toTest string) bool {
2021-10-26 14:14:26 -04:00
_, 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
}