vega/testbot/main.go

86 lines
1.7 KiB
Go
Raw Normal View History

2020-04-11 14:39:19 -04:00
package main
import (
"log"
2020-04-11 16:26:59 -04:00
"os"
"os/signal"
"sync"
"syscall"
2020-04-11 14:39:19 -04:00
2020-04-11 16:26:59 -04:00
"git.saintnet.tech/stryan/vega/botlib"
"github.com/spf13/viper"
2020-04-11 14:39:19 -04:00
"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")
2020-04-11 14:39:19 -04:00
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"),
2020-04-11 14:39:19 -04:00
}
b := botlib.NewBot(c)
2020-04-11 16:26:59 -04:00
tb := &TestBot{
Bot: b,
Conf: c,
Jobs: make(chan *mautrix.Event, 100),
}
2020-04-14 16:54:31 -04:00
tb.InitActions()
tb.InitScheduler()
2020-04-11 14:39:19 -04:00
syncer := botlib.NewCustomSyncer("@testbot:saintnet.tech", b.Client.Store)
2020-04-11 16:26:59 -04:00
syncer.OnEventType(mautrix.EventMessage, tb.QueueMessage)
syncer.OnEventType(mautrix.StateMember, b.DefaultHandleMember)
2020-04-11 14:39:19 -04:00
b.Client.Syncer = syncer
2020-04-11 16:26:59 -04:00
wg := new(sync.WaitGroup)
2020-04-11 14:39:19 -04:00
go func() {
2020-04-11 16:26:59 -04:00
wg.Add(1)
2020-04-11 14:39:19 -04:00
err := b.Client.Sync()
if err != nil {
log.Fatal(err)
}
2020-04-11 16:26:59 -04:00
wg.Done()
2020-04-11 14:39:19 -04:00
}()
log.Println("Syncing enabled")
err = b.LeaveEmptyRooms()
2020-04-11 14:39:19 -04:00
if err != nil {
log.Fatal(err)
}
2020-04-11 16:26:59 -04:00
tb.LoadState()
_, err = b.CreateManagementRoom()
2020-04-11 14:39:19 -04:00
if err != nil {
log.Fatal(err)
}
2020-04-11 16:26:59 -04:00
log.Println("Begining main phase")
cancelChan := make(chan bool)
for i := 0; i < 4; i++ {
2020-04-11 16:26:59 -04:00
go func() {
wg.Add(1)
tb.MessageHandler(cancelChan)
wg.Done()
}()
2020-04-11 14:39:19 -04:00
}
2020-04-11 16:26:59 -04:00
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()
}()
2020-04-11 14:39:19 -04:00
2020-04-11 16:26:59 -04:00
wg.Wait()
2020-04-11 14:39:19 -04:00
}