Rewrite and refactor #1

Merged
stryan merged 6 commits from rewrite into master 2023-05-16 18:36:19 -04:00
Showing only changes of commit fc274e665a - Show all commits

22
simp.go
View File

@ -22,6 +22,7 @@ type simp struct {
maxStream int maxStream int
vtubers []*Vtuber vtubers []*Vtuber
stop chan bool stop chan bool
rooms []id.RoomID
} }
func newSimp() *simp { func newSimp() *simp {
@ -32,26 +33,25 @@ func newSimp() *simp {
} }
func (s *simp) Run() { func (s *simp) Run() {
ticker := time.NewTicker(time.Second * 30) ticker := time.NewTicker(time.Second * 45)
roomResp, err := s.client.JoinedRooms()
if err != nil {
log.Errorf("error getting joined rooms: %v", err)
return
}
s.rooms = roomResp.JoinedRooms
for { for {
select { select {
case <-s.stop: case <-s.stop:
return return
case <-ticker.C: case <-ticker.C:
roomResp, err := s.client.JoinedRooms()
if err != nil {
log.Errorf("error getting joined rooms: %v, skipping iteration", err)
continue
}
rooms := roomResp.JoinedRooms
// We're going to assume they're only stream one video at a time // We're going to assume they're only stream one video at a time
for _, v := range s.vtubers { for _, v := range s.vtubers {
err = v.Update(s.holodexToken) err = v.Update(s.holodexToken)
if err != nil { if err != nil {
log.Error("error pinging holodex", "error", err) log.Error("error pinging holodex", "error", err)
} }
for _, room := range rooms { for _, room := range s.rooms {
if v.IsLive() { if v.IsLive() {
// check to see if already embeded // check to see if already embeded
var content YoutubeWidget var content YoutubeWidget
@ -61,10 +61,11 @@ func (s *simp) Run() {
"dimension-m.video-simp-"+v.Name, "dimension-m.video-simp-"+v.Name,
&content, &content,
) )
update := true
if err != nil { if err != nil {
log.Errorf("error getting state event in room %v: %v", room, err) log.Errorf("error getting state event in room %v: %v", room, err)
} }
if content.ID == "" { if update {
if v.AnnounceLive { if v.AnnounceLive {
s.client.SendText(room, v.LiveMsg) s.client.SendText(room, v.LiveMsg)
} else { } else {
@ -256,6 +257,7 @@ func (s *simp) SetupMatrix(uname, pass, token, hs, domain, dserver string) error
log.Errorf("error joining room %v", evt.RoomID) log.Errorf("error joining room %v", evt.RoomID)
} else { } else {
log.Infof("joined room %v", evt.RoomID) log.Infof("joined room %v", evt.RoomID)
s.rooms = append(s.rooms, evt.RoomID)
} }
} }
}) })