simpbot/main.go
2021-05-21 12:45:24 -04:00

112 lines
3.8 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
)
var Homeserver = flag.String("homeserver", "", "Matrix homeserver")
var Username = flag.String("username", "", "Matrix username localpart")
var Password = flag.String("password", "", "Matrix password")
var DimensionServer = flag.String("dimension", "", "Dimension server URL")
var HomeserverDomain = flag.String("domain", "", "Matrix homserver domain (the part after : )")
func main() {
flag.Parse()
if *Username == "" || *Password == "" || *Homeserver == "" || *DimensionServer == "" || *HomeserverDomain == "" {
_, _ = fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
}
fmt.Println("Logging into", *Homeserver, "as", *Username)
client, err := mautrix.NewClient(*Homeserver, "", "")
if err != nil {
panic(err)
}
_, err = client.Login(&mautrix.ReqLogin{
Type: "m.login.password",
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: *Username},
Password: *Password,
StoreCredentials: true,
})
if err != nil {
panic(err)
}
fmt.Println("Login successful")
syncer := client.Syncer.(*mautrix.DefaultSyncer)
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
fmt.Printf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
})
syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) {
fmt.Printf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
if evt.Content.AsMember().Membership.IsInviteOrJoin() {
_, err = client.JoinRoomByID(evt.RoomID)
if err != nil {
fmt.Printf("error joining room %v", evt.RoomID)
} else {
fmt.Println("joined room")
}
}
})
vtubers := []*Vtuber{NewVtuber("Ina", "UCMwGHR0BTZuLsmjY_NT5Pwg", "The High Priestess is Live"), NewVtuber("Kiara", "UCHsx4Hqa-1ORjQTh9TYDhww", "The Bird has Landed"), NewVtuber("Calliope", "UCL_qhgtOy0dy1Agp8vkySQg", "The Reapers here")}
go func() {
for {
time.Sleep(30 * time.Second)
roomResp, err := client.JoinedRooms()
if err != nil {
log.Println("error gettting joined rooms")
}
rooms := roomResp.JoinedRooms
// We're going to assume they're only stream one video at a time
for _, v := range vtubers {
v.Update()
if v.IsLive() {
for _, room := range rooms {
//check to see if already embeded
var content YoutubeWidget
err = client.StateEvent(room, event.NewEventType("im.vector.modular.widgets"), "dimension-m.video-simp-"+v.Name, &content)
if content.ID == "" {
client.SendText(room, v.LiveMsg)
resp, err := client.SendStateEvent(room, event.NewEventType("im.vector.modular.widgets"), "dimension-m.video-simp-"+v.Name, NewYT(v.Name+"'s stream", v.CurrentStream, string(room)))
if err != nil {
log.Println("error embeding video")
log.Println(err)
}
log.Printf("Embed event %v", resp)
}
}
} else {
//Not live, check to see if there's any embeds and remove them
for _, room := range rooms {
var content YoutubeWidget
err = client.StateEvent(room, event.NewEventType("im.vector.modular.widgets"), "dimension-m.video-simp-"+v.Name, &content)
if err == nil && content.ID != "" {
//event found, kill it
resp, err := client.SendStateEvent(room, event.NewEventType("im.vector.modular.widgets"), "dimension-m.video-simp-"+v.Name, struct{}{})
if err != nil {
log.Println("error removed video embed")
log.Println(err)
}
log.Printf("Emebed remove %v", resp)
}
}
}
}
}
}()
err = client.Sync()
if err != nil {
panic(err)
}
}