2021-06-10 13:27:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-29 19:39:14 +00:00
|
|
|
"context"
|
2021-06-10 13:27:13 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"time"
|
|
|
|
|
2021-07-22 22:54:11 +00:00
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/robfig/cron/v3"
|
2021-06-10 13:27:13 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-07-25 12:39:53 +00:00
|
|
|
/*
|
|
|
|
* @module presence
|
|
|
|
* @module_desc Updates the presence status of the bot to display the next stream
|
|
|
|
*/
|
|
|
|
|
2021-07-31 18:58:11 +00:00
|
|
|
const (
|
|
|
|
presenceTimeDay = 24 * time.Hour
|
|
|
|
)
|
|
|
|
|
2021-06-10 13:27:13 +00:00
|
|
|
func init() {
|
2021-07-22 22:54:11 +00:00
|
|
|
RegisterModule("presence", func() module { return &modPresence{} })
|
|
|
|
}
|
|
|
|
|
|
|
|
type modPresence struct {
|
|
|
|
attrs moduleAttributeStore
|
|
|
|
discord *discordgo.Session
|
2021-08-07 14:16:14 +00:00
|
|
|
id string
|
2021-07-22 22:54:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 14:16:14 +00:00
|
|
|
func (m modPresence) ID() string { return m.id }
|
|
|
|
|
|
|
|
func (m *modPresence) Initialize(id string, crontab *cron.Cron, discord *discordgo.Session, attrs moduleAttributeStore) error {
|
2021-07-22 22:54:11 +00:00
|
|
|
m.attrs = attrs
|
|
|
|
m.discord = discord
|
2021-08-07 14:16:14 +00:00
|
|
|
m.id = id
|
2021-07-22 22:54:11 +00:00
|
|
|
|
2021-07-25 12:39:53 +00:00
|
|
|
if err := attrs.Expect(
|
|
|
|
"fallback_text",
|
2021-07-29 19:39:14 +00:00
|
|
|
"twitch_channel_id",
|
|
|
|
"twitch_client_id",
|
2021-08-22 22:57:31 +00:00
|
|
|
"twitch_client_secret",
|
2021-07-25 12:39:53 +00:00
|
|
|
); err != nil {
|
|
|
|
return errors.Wrap(err, "validating attributes")
|
|
|
|
}
|
|
|
|
|
|
|
|
// @attr cron optional string "* * * * *" When to execute the module
|
2021-07-22 22:54:11 +00:00
|
|
|
if _, err := crontab.AddFunc(attrs.MustString("cron", ptrString("* * * * *")), m.cronUpdatePresence); err != nil {
|
|
|
|
return errors.Wrap(err, "adding cron function")
|
2021-06-10 13:27:13 +00:00
|
|
|
}
|
2021-07-22 22:54:11 +00:00
|
|
|
|
|
|
|
return nil
|
2021-06-10 13:27:13 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 13:24:11 +00:00
|
|
|
func (m modPresence) Setup() error { return nil }
|
|
|
|
|
2021-07-22 22:54:11 +00:00
|
|
|
func (m modPresence) cronUpdatePresence() {
|
2021-07-31 18:58:11 +00:00
|
|
|
var nextStream *time.Time
|
2021-06-10 13:27:13 +00:00
|
|
|
|
2021-07-29 19:39:14 +00:00
|
|
|
twitch := newTwitchAdapter(
|
|
|
|
// @attr twitch_client_id required string "" Twitch client ID the token was issued for
|
|
|
|
m.attrs.MustString("twitch_client_id", nil),
|
2021-08-22 22:57:31 +00:00
|
|
|
// @attr twitch_client_secret required string "" Secret for the Twitch app identified with twitch_client_id
|
|
|
|
m.attrs.MustString("twitch_client_secret", nil),
|
|
|
|
"", // No User-Token used
|
2021-07-29 19:39:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
data, err := twitch.GetChannelStreamSchedule(
|
|
|
|
context.Background(),
|
|
|
|
// @attr twitch_channel_id required string "" ID (not name) of the channel to fetch the schedule from
|
|
|
|
m.attrs.MustString("twitch_channel_id", nil),
|
|
|
|
// @attr schedule_past_time optional duration "15m" How long in the past should the schedule contain an entry
|
|
|
|
ptrTime(time.Now().Add(-m.attrs.MustDuration("schedule_past_time", defaultStreamSchedulePastTime))),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Unable to fetch stream schedule")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, seg := range data.Data.Segments {
|
|
|
|
if seg.StartTime == nil || seg.CanceledUntil != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-10 21:21:48 +00:00
|
|
|
if seg.StartTime.Before(time.Now()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-29 19:39:14 +00:00
|
|
|
nextStream = seg.StartTime
|
|
|
|
break
|
|
|
|
}
|
2021-06-10 13:27:13 +00:00
|
|
|
|
2021-07-25 12:39:53 +00:00
|
|
|
// @attr fallback_text required string "" What to set the text to when no stream is found (`playing <text>`)
|
|
|
|
status := m.attrs.MustString("fallback_text", nil)
|
2021-06-10 13:27:13 +00:00
|
|
|
if nextStream != nil {
|
2021-08-02 11:34:19 +00:00
|
|
|
status = m.durationToHumanReadable(time.Since(*nextStream))
|
2021-06-10 13:27:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 22:54:11 +00:00
|
|
|
if err := m.discord.UpdateGameStatus(0, status); err != nil {
|
2021-06-10 13:27:13 +00:00
|
|
|
log.WithError(err).Error("Unable to update status")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Updated presence")
|
|
|
|
}
|
|
|
|
|
2021-07-22 22:54:11 +00:00
|
|
|
func (m modPresence) durationToHumanReadable(d time.Duration) string {
|
2021-07-29 19:39:14 +00:00
|
|
|
d = time.Duration(math.Abs(float64(d)))
|
2021-08-02 11:34:19 +00:00
|
|
|
|
2021-07-31 18:58:11 +00:00
|
|
|
if d > presenceTimeDay {
|
2021-08-02 11:34:19 +00:00
|
|
|
return fmt.Sprintf("in %.0f Tagen", math.Round(float64(d)/float64(presenceTimeDay)))
|
2021-07-29 19:39:14 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 11:34:19 +00:00
|
|
|
if d > time.Hour {
|
|
|
|
return fmt.Sprintf("in %.0f Stunden", math.Round(float64(d)/float64(time.Hour)))
|
|
|
|
}
|
2021-06-10 13:27:13 +00:00
|
|
|
|
2021-08-02 11:34:19 +00:00
|
|
|
if d > time.Minute {
|
|
|
|
return fmt.Sprintf("in %.0f Minuten", math.Round(float64(d)/float64(time.Minute)))
|
2021-06-10 13:27:13 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 11:34:19 +00:00
|
|
|
return "gleich"
|
2021-06-10 13:27:13 +00:00
|
|
|
}
|