better room management
This commit is contained in:
parent
0da253025e
commit
1140534496
8
main.go
8
main.go
@ -19,10 +19,12 @@ var GitCommit string
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Info("loading matrix client")
|
log.Info("loading matrix client")
|
||||||
conf, err := mbl.LoadMatrixClientConfig("config.yaml")
|
cnffile := "config.yaml"
|
||||||
|
conf, err := mbl.LoadMatrixClientConfig(cnffile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("no local config found, checking /etc/simpbot")
|
log.Info("no local config found, checking /etc/simpbot")
|
||||||
conf, err = mbl.LoadMatrixClientConfig("/etc/simpbot/config.yaml")
|
cnffile = "/etc/simpbot/config.yaml"
|
||||||
|
conf, err = mbl.LoadMatrixClientConfig(cnffile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -34,7 +36,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Info("loading simpbot")
|
log.Info("loading simpbot")
|
||||||
simp, err := newSimp("config.yaml", matrixClient)
|
simp, err := newSimp(cnffile, matrixClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
61
simp.go
61
simp.go
@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
mbl "git.saintnet.tech/stryan/matrixbotlib"
|
mbl "git.saintnet.tech/stryan/matrixbotlib"
|
||||||
"github.com/charmbracelet/log"
|
"github.com/charmbracelet/log"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
@ -134,6 +135,48 @@ func (s *simp) Sync() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *simp) PopulateRoom(room id.RoomID) error {
|
||||||
|
log.Info("updating room", "room", room)
|
||||||
|
for _, v := range s.State.vtubers {
|
||||||
|
log.Printf("Updating vtuber %v", v.Name)
|
||||||
|
if v.IsLive() {
|
||||||
|
if v.AnnounceLive {
|
||||||
|
s.State.client.SendText(room, v.LiveMsg)
|
||||||
|
} else {
|
||||||
|
if isValidURL(v.LiveMsg) {
|
||||||
|
s.State.client.SendNotice(room, fmt.Sprintf("%v has gone live", v.Name))
|
||||||
|
} else {
|
||||||
|
s.State.client.SendNotice(room, v.LiveMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.State.client.SendNotice(room, fmt.Sprintf("%v's Title: %v", v.Name, v.CurrentStreamTitle))
|
||||||
|
var subs string
|
||||||
|
for k := range v.Subs {
|
||||||
|
subs += k.String() + " "
|
||||||
|
}
|
||||||
|
if len(v.Subs) > 0 {
|
||||||
|
s.State.client.SendText(room, fmt.Sprintf("Pinging %v", subs))
|
||||||
|
}
|
||||||
|
resp, err := s.State.client.SendStateEvent(
|
||||||
|
room,
|
||||||
|
event.NewEventType("im.vector.modular.widgets"),
|
||||||
|
"dimension-m.video-simp-"+v.Name,
|
||||||
|
s.NewYT(v.Name+"'s stream", v.CurrentStream, string(room)),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
v.TotalStreams = v.TotalStreams + 1
|
||||||
|
s.State.currStream++
|
||||||
|
if s.State.currStream > s.State.maxStream {
|
||||||
|
s.State.maxStream = s.State.currStream
|
||||||
|
}
|
||||||
|
log.Info("Embed stream added", "event", resp, "vtuber", v.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *simp) Update(v *Vtuber) error {
|
func (s *simp) Update(v *Vtuber) error {
|
||||||
for _, room := range s.State.rooms {
|
for _, room := range s.State.rooms {
|
||||||
if v.IsLive() {
|
if v.IsLive() {
|
||||||
@ -197,6 +240,7 @@ func (s *simp) Run() {
|
|||||||
func (s *simp) SetupMatrix() error {
|
func (s *simp) SetupMatrix() error {
|
||||||
mbl.SetupAccountDataStore(s.State.client, "s.batch")
|
mbl.SetupAccountDataStore(s.State.client, "s.batch")
|
||||||
syncer := s.State.client.Syncer.(*mautrix.DefaultSyncer)
|
syncer := s.State.client.Syncer.(*mautrix.DefaultSyncer)
|
||||||
|
uid := s.State.client.UserID.String()
|
||||||
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
||||||
if evt.Sender == s.State.client.UserID {
|
if evt.Sender == s.State.client.UserID {
|
||||||
return // ignore events from self
|
return // ignore events from self
|
||||||
@ -269,8 +313,10 @@ func (s *simp) SetupMatrix() error {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) {
|
syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) {
|
||||||
log.Infof("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
|
//log.Infof("<%[1]s> %[4]s (%[2]s/%[3]s)", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
|
||||||
if evt.Content.AsMember().Membership.IsInviteOrJoin() {
|
mevt := evt.Content.AsMember()
|
||||||
|
log.Printf("FBL %v", mevt.Membership)
|
||||||
|
if mevt.Membership == event.MembershipInvite {
|
||||||
_, err := s.State.client.JoinRoomByID(evt.RoomID)
|
_, err := s.State.client.JoinRoomByID(evt.RoomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error joining room %v", evt.RoomID)
|
log.Errorf("error joining room %v", evt.RoomID)
|
||||||
@ -278,6 +324,16 @@ func (s *simp) SetupMatrix() error {
|
|||||||
log.Infof("joined room %v", evt.RoomID)
|
log.Infof("joined room %v", evt.RoomID)
|
||||||
s.State.rooms = append(s.State.rooms, evt.RoomID)
|
s.State.rooms = append(s.State.rooms, evt.RoomID)
|
||||||
}
|
}
|
||||||
|
} else if evt.Content.AsMember().Membership.IsLeaveOrBan() {
|
||||||
|
if evt.StateKey != nil && *evt.StateKey == uid {
|
||||||
|
log.Infof("leaving room %v", evt.RoomID)
|
||||||
|
index := slices.Index(s.State.rooms, evt.RoomID)
|
||||||
|
if index != -1 {
|
||||||
|
slices.Delete(s.State.rooms, index, index)
|
||||||
|
} else {
|
||||||
|
log.Warn("asked to leave room not in memory: %v", evt.RoomID)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
roomResp, err := s.State.client.JoinedRooms()
|
roomResp, err := s.State.client.JoinedRooms()
|
||||||
@ -285,6 +341,7 @@ func (s *simp) SetupMatrix() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
s.State.rooms = roomResp.JoinedRooms
|
s.State.rooms = roomResp.JoinedRooms
|
||||||
|
log.Infof("initial room count: %v", len(s.State.rooms))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user