Rewrite and refactor #1

Merged
stryan merged 6 commits from rewrite into master 2023-05-16 18:36:19 -04:00
3 changed files with 33 additions and 17 deletions
Showing only changes of commit ae50a68aea - Show all commits

View File

@ -63,13 +63,11 @@ func main() {
wg.Add(1)
go func() {
err = simp.client.Sync()
if err != nil {
log.Error(err)
}
wg.Done()
}()
wg.Add(1)
@ -81,7 +79,6 @@ func main() {
log.Info("simpbot running")
wg.Wait()
log.Info("shutting down")
}
func isValidURL(toTest string) bool {

36
simp.go
View File

@ -53,9 +53,14 @@ func (s *simp) Run() {
}
for _, room := range rooms {
if v.IsLive() {
//check to see if already embeded
// check to see if already embeded
var content youtubeWidget
err = s.client.StateEvent(room, event.NewEventType("im.vector.modular.widgets"), "dimension-m.video-simp-"+v.Name, &content)
err = s.client.StateEvent(
room,
event.NewEventType("im.vector.modular.widgets"),
"dimension-m.video-simp-"+v.Name,
&content,
)
if err != nil {
log.Errorf("error getting state event in room %v: %v", room, err)
}
@ -77,7 +82,12 @@ func (s *simp) Run() {
if len(v.Subs) > 0 {
s.client.SendText(room, fmt.Sprintf("Pinging %v", subs))
}
resp, err := s.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)))
resp, err := s.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 {
log.Errorf("error embeding video: %v", err)
}
@ -92,7 +102,7 @@ func (s *simp) Run() {
var content youtubeWidget
err = s.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
// event found, kill it
resp, err := s.client.SendStateEvent(room, event.NewEventType("im.vector.modular.widgets"), "dimension-m.video-simp-"+v.Name, struct{}{})
if err != nil {
log.Errorf("error removing embed: %v", err)
@ -106,7 +116,6 @@ func (s *simp) Run() {
}
}
}
}
func (s *simp) SetupMatrix(uname, pass, token, hs, domain, dserver string) error {
@ -164,7 +173,7 @@ func (s *simp) SetupMatrix(uname, pass, token, hs, domain, dserver string) error
syncer := s.client.Syncer.(*mautrix.DefaultSyncer)
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
if evt.Sender == s.client.UserID {
return //ignore events from self
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)
body := evt.Content.AsMessage().Body
@ -173,7 +182,7 @@ func (s *simp) SetupMatrix(uname, pass, token, hs, domain, dserver string) error
return
}
if len(bodyS) < 2 {
return //nothing to parse
return // nothing to parse
}
switch bodyS[1] {
case "info":
@ -197,13 +206,20 @@ func (s *simp) SetupMatrix(uname, pass, token, hs, domain, dserver string) error
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.startTime, strings.Join(vlist, "\n"), t, s.maxStream, len(s.vtubers))
statmsg = fmt.Sprintf(
"Current Stats Since %v:\n%v\n\nTotal Streams: %v\nMost Concurrent: %v/%v\n",
s.startTime,
strings.Join(vlist, "\n"),
t,
s.maxStream,
len(s.vtubers),
)
s.client.SendText(evt.RoomID, statmsg)
case "version":
s.client.SendText(evt.RoomID, "not implemented")
case "reload":
//reload config
// reload config
s.client.SendText(evt.RoomID, "Reloading config")
log.Info("Reload requested,reloading vtubers")
s.vtubers = loadVtubers()
@ -228,7 +244,7 @@ func (s *simp) SetupMatrix(uname, pass, token, hs, domain, dserver string) error
case "help":
s.client.SendText(evt.RoomID, "Supported commands: info,version,stats,reload,subscribe")
default:
//command not found
// command not found
s.client.SendText(evt.RoomID, "command not recognized")
}
})

View File

@ -18,7 +18,7 @@ type vtuberConfig struct {
Announce bool `mapstructure:"Announce"`
}
//Vtuber represents a vtuber
// Vtuber represents a vtuber
type Vtuber struct {
Name string
ChannelID string
@ -57,14 +57,17 @@ func loadVtubers() []*Vtuber {
return vtubers
}
//IsLive returns whether the specified Vtuber is live
// IsLive returns whether the specified Vtuber is live
func (v *Vtuber) IsLive() bool {
return v.CurrentStream != ""
}
//Update takes an apiKey and updates the vtuber struct
// Update takes an apiKey and updates the vtuber struct
func (v *Vtuber) Update(apiKey string) error {
url := fmt.Sprintf("https://holodex.net/api/v2/live?channel_id=%s&lang=all&sort=available_at&order=desc&limit=25&offset=0&paginated=%%3Cempty%%3E", v.ChannelID)
url := fmt.Sprintf(
"https://holodex.net/api/v2/live?channel_id=%s&lang=all&sort=available_at&order=desc&limit=25&offset=0&paginated=%%3Cempty%%3E",
v.ChannelID,
)
req, err := http.NewRequest("GET", url, nil)
req.Header.Set("X-APIKEY", apiKey)
if err != nil {