vega/testbot/main.go

87 lines
1.7 KiB
Go

package main
import (
"log"
"os"
"os/signal"
"sync"
"syscall"
"git.saintnet.tech/stryan/vega/botlib"
"github.com/spf13/viper"
"maunium.net/go/mautrix"
)
func main() {
viper.SetConfigName("config")
viper.AddConfigPath("/etc/testbot")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Fatal error config file: %v \n", err)
}
viper.SetConfigType("yaml")
c := &botlib.Config{
Userid: viper.GetString("userid"),
Server: viper.GetString("server"),
Token: viper.GetString("token"),
Name: viper.GetString("name"),
Owner: viper.GetString("owner"),
Prefix: viper.GetString("prefix"),
}
b := botlib.NewBot(c)
tb := &TestBot{
Bot: b,
Conf: c,
Jobs: make(chan *mautrix.Event, 100),
StateFile: viper.GetString("statefile"),
}
tb.InitActions()
tb.InitScheduler()
syncer := botlib.NewCustomSyncer("@testbot:saintnet.tech", b.Client.Store)
syncer.OnEventType(mautrix.EventMessage, tb.QueueMessage)
syncer.OnEventType(mautrix.StateMember, b.DefaultHandleMember)
b.Client.Syncer = syncer
wg := new(sync.WaitGroup)
go func() {
wg.Add(1)
err := b.Client.Sync()
if err != nil {
log.Fatal(err)
}
wg.Done()
}()
log.Println("Syncing enabled")
err = b.LeaveEmptyRooms()
if err != nil {
log.Fatal(err)
}
tb.LoadState()
_, err = b.CreateManagementRoom()
if err != nil {
log.Fatal(err)
}
log.Println("Begining main phase")
cancelChan := make(chan bool)
for i := 0; i < 4; i++ {
go func() {
wg.Add(1)
tb.MessageHandler(cancelChan)
wg.Done()
}()
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
log.Printf("Received sig %v", sig)
cancelChan <- true
tb.Shutdown()
}()
wg.Wait()
}