321 lines
8.2 KiB
Go
321 lines
8.2 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
mbl "git.saintnet.tech/stryan/matrixbotlib"
|
|
"github.com/charmbracelet/log"
|
|
"gopkg.in/yaml.v2"
|
|
"maunium.net/go/mautrix"
|
|
"maunium.net/go/mautrix/event"
|
|
"maunium.net/go/mautrix/id"
|
|
)
|
|
|
|
//SimpConf is the simpbot conf that's not matrix
|
|
type SimpConf struct {
|
|
DimensionServer string `yaml:"dimension"`
|
|
HolodexToken string `yaml:"api_token"`
|
|
Vtubers []vtuberConfig `yaml:"vtubers"`
|
|
}
|
|
|
|
type simpState struct {
|
|
client *mautrix.Client
|
|
startTime time.Time
|
|
currStream int
|
|
maxStream int
|
|
vtubers []*Vtuber
|
|
stop chan bool
|
|
stopRun chan bool
|
|
change chan int
|
|
rooms []id.RoomID
|
|
filename string
|
|
}
|
|
|
|
type simp struct {
|
|
Config *SimpConf
|
|
State *simpState
|
|
}
|
|
|
|
func newSimp(filename string, client *mautrix.Client) (*simp, error) {
|
|
cnf := &SimpConf{}
|
|
state := &simpState{
|
|
startTime: time.Now(),
|
|
stop: make(chan bool),
|
|
stopRun: make(chan bool),
|
|
change: make(chan int),
|
|
client: client,
|
|
}
|
|
yamlFile, err := ioutil.ReadFile(filename)
|
|
if err == nil {
|
|
err = yaml.Unmarshal(yamlFile, cnf)
|
|
} else {
|
|
return nil, err
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
state.filename = filename
|
|
for _, vt := range cnf.Vtubers {
|
|
log.Infof("adding vtuber %v", vt)
|
|
v := newVtuber(vt.Name, vt.ChannelID, vt.LiveMsg, vt.Announce)
|
|
state.vtubers = append(state.vtubers, v)
|
|
}
|
|
log.Print(cnf)
|
|
if len(state.vtubers) == 0 {
|
|
return nil, fmt.Errorf("no vtubers defined")
|
|
}
|
|
if cnf.HolodexToken == "" {
|
|
return nil, fmt.Errorf("no holodex token")
|
|
}
|
|
if cnf.DimensionServer == "" {
|
|
return nil, fmt.Errorf("no dimension server")
|
|
}
|
|
|
|
return &simp{cnf, state}, nil
|
|
}
|
|
|
|
func (s *simp) Sync() error {
|
|
go s.Run()
|
|
for _, v := range s.State.vtubers {
|
|
err := v.Update(s.Config.HolodexToken)
|
|
if err != nil {
|
|
log.Error("error pinging holodex", "error", err)
|
|
continue
|
|
}
|
|
}
|
|
|
|
for _, r := range s.State.rooms {
|
|
for _, v := range s.State.vtubers {
|
|
var content YoutubeWidget
|
|
err := s.State.client.StateEvent(
|
|
r,
|
|
event.NewEventType("im.vector.modular.widgets"),
|
|
"dimension-m.video-simp-"+v.Name,
|
|
&content,
|
|
)
|
|
if err != nil {
|
|
if !errors.Is(err, mautrix.MNotFound) {
|
|
log.Errorf("error getting state event in room %v: %v", r, err)
|
|
}
|
|
}
|
|
embed := content.ID != ""
|
|
if v.IsLive() != embed {
|
|
s.Update(v)
|
|
}
|
|
|
|
}
|
|
}
|
|
log.Info("starting normal sync")
|
|
ticker := time.NewTicker(time.Second * 45)
|
|
for {
|
|
select {
|
|
case <-s.State.stop:
|
|
s.State.stopRun <- true
|
|
return nil
|
|
case <-ticker.C:
|
|
for i, v := range s.State.vtubers {
|
|
cur := v.IsLive()
|
|
err := v.Update(s.Config.HolodexToken)
|
|
if err != nil {
|
|
log.Error("error pinging holodex", "error", err)
|
|
continue
|
|
}
|
|
now := v.IsLive()
|
|
if cur != now {
|
|
s.State.change <- i
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *simp) Update(v *Vtuber) error {
|
|
for _, room := range s.State.rooms {
|
|
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)
|
|
} else {
|
|
resp, err := s.State.client.SendStateEvent(room, event.NewEventType("im.vector.modular.widgets"), "dimension-m.video-simp-"+v.Name, struct{}{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.State.currStream--
|
|
log.Info("Embed stream removed", "event", resp, "vtuber", v.Name)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *simp) Run() {
|
|
for {
|
|
select {
|
|
case <-s.State.stopRun:
|
|
log.Info("stopping runner")
|
|
return
|
|
case v := <-s.State.change:
|
|
s.Update(s.State.vtubers[v])
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *simp) SetupMatrix() error {
|
|
mbl.SetupAccountDataStore(s.State.client, "s.batch")
|
|
syncer := s.State.client.Syncer.(*mautrix.DefaultSyncer)
|
|
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
|
if evt.Sender == s.State.client.UserID {
|
|
return // ignore events from self
|
|
}
|
|
log.Debugf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
|
|
|
|
cmd, err := mbl.ParseCommand(evt, "simp")
|
|
if err != nil {
|
|
if err != mbl.ErrCmdParseNoPrefix {
|
|
log.Printf("invalid command: %v", err)
|
|
}
|
|
return
|
|
}
|
|
switch cmd[0] {
|
|
case "info":
|
|
// print info page
|
|
var infomsg string
|
|
vlist := []string{}
|
|
for _, vt := range s.State.vtubers {
|
|
ann := ""
|
|
if vt.AnnounceLive {
|
|
ann = "*"
|
|
}
|
|
vlist = append(vlist, fmt.Sprintf("%v%v", vt.Name, ann))
|
|
}
|
|
infomsg = fmt.Sprintf("Currently Simping For: \n%v", strings.Join(vlist, "\n"))
|
|
s.State.client.SendText(evt.RoomID, infomsg)
|
|
case "stats":
|
|
var statmsg string
|
|
vlist := []string{}
|
|
t := 0
|
|
for _, vt := range s.State.vtubers {
|
|
vlist = append(vlist, fmt.Sprintf("%v Total:%v", vt.Name, vt.TotalStreams))
|
|
t = t + vt.TotalStreams
|
|
}
|
|
statmsg = fmt.Sprintf(
|
|
"Current Stats Since %v:\n%v\n\nTotal Streams: %v\nMost Concurrent: %v/%v\n",
|
|
s.State.startTime,
|
|
strings.Join(vlist, "\n"),
|
|
t,
|
|
s.State.maxStream,
|
|
len(s.State.vtubers),
|
|
)
|
|
s.State.client.SendText(evt.RoomID, statmsg)
|
|
case "version":
|
|
s.State.client.SendText(evt.RoomID, "not implemented")
|
|
case "subscribe":
|
|
if len(cmd) < 3 {
|
|
s.State.client.SendText(evt.RoomID, "Need a member to subscribe to")
|
|
}
|
|
vt := cmd[1]
|
|
var subbed bool
|
|
for _, v := range s.State.vtubers {
|
|
if strings.ToUpper(v.Name) == strings.ToUpper(vt) {
|
|
v.Subs[evt.Sender] = true
|
|
subbed = true
|
|
}
|
|
}
|
|
if subbed {
|
|
s.State.client.SendText(evt.RoomID, "subbed")
|
|
} else {
|
|
s.State.client.SendText(evt.RoomID, "could not identify talent to subscribe to")
|
|
}
|
|
|
|
case "help":
|
|
s.State.client.SendText(evt.RoomID, "Supported commands: info,version,stats,subscribe")
|
|
default:
|
|
// command not found
|
|
s.State.client.SendText(evt.RoomID, "command not recognized")
|
|
}
|
|
})
|
|
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)
|
|
if evt.Content.AsMember().Membership.IsInviteOrJoin() {
|
|
_, err := s.State.client.JoinRoomByID(evt.RoomID)
|
|
if err != nil {
|
|
log.Errorf("error joining room %v", evt.RoomID)
|
|
} else {
|
|
log.Infof("joined room %v", evt.RoomID)
|
|
s.State.rooms = append(s.State.rooms, evt.RoomID)
|
|
}
|
|
}
|
|
})
|
|
roomResp, err := s.State.client.JoinedRooms()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.State.rooms = roomResp.JoinedRooms
|
|
return nil
|
|
}
|
|
|
|
func (s *simp) Stop() {
|
|
s.State.stop <- true
|
|
s.State.client.StopSync()
|
|
}
|
|
|
|
func (s *simp) NewYT(videoName, videoID, roomID string) *YoutubeWidget {
|
|
encodedVod := url.QueryEscape("https://youtube.com/embed/" + videoID)
|
|
return &YoutubeWidget{
|
|
Type: "im.vector.modular.widgets",
|
|
URL: "https://" + s.Config.DimensionServer + "/widgets/video?url=" + encodedVod,
|
|
Name: videoName,
|
|
Data: VideoData{
|
|
VideoURL: "https://www.youtube.com/watch?v=" + videoID,
|
|
URL: "https://youtube.com/embed/" + videoID,
|
|
DimensionAppMetadata: DimensionAppMetadata{
|
|
InRoomID: roomID,
|
|
WrapperURLBase: "https://" + s.Config.DimensionServer + "/widgets/video?url=",
|
|
WrapperID: "video",
|
|
ScalarWrapperID: "youtube",
|
|
Integration: Integration{
|
|
Category: "widget",
|
|
Type: "youtube",
|
|
},
|
|
LastUpdatedTs: time.Now().UnixNano() / int64(time.Millisecond),
|
|
},
|
|
},
|
|
CreatorUserID: string(s.State.client.UserID),
|
|
ID: "dimension-m.video-simp",
|
|
}
|
|
}
|