2021-06-10 13:27:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strings"
|
|
|
|
"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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *modPresence) Initialize(crontab *cron.Cron, discord *discordgo.Session, attrs moduleAttributeStore) error {
|
|
|
|
m.attrs = attrs
|
|
|
|
m.discord = discord
|
|
|
|
|
2021-07-25 12:39:53 +00:00
|
|
|
if err := attrs.Expect(
|
|
|
|
"fallback_text",
|
|
|
|
); 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-07-22 22:54:11 +00:00
|
|
|
func (m modPresence) cronUpdatePresence() {
|
2021-06-10 13:27:13 +00:00
|
|
|
var nextStream *time.Time = nil
|
|
|
|
|
|
|
|
// FIXME: Get next stream status
|
|
|
|
|
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-07-22 22:54:11 +00:00
|
|
|
status = fmt.Sprintf("in: %s", 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-06-10 13:27:13 +00:00
|
|
|
var elements []string
|
|
|
|
|
|
|
|
d = time.Duration(math.Abs(float64(d)))
|
|
|
|
for div, req := range map[time.Duration]bool{
|
|
|
|
time.Hour * 24: false,
|
|
|
|
time.Hour: true,
|
|
|
|
time.Minute: true,
|
|
|
|
} {
|
|
|
|
if d < div && !req {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
pt := d / div
|
|
|
|
d -= pt * div
|
|
|
|
elements = append(elements, fmt.Sprintf("%.2d", pt))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(elements, ":")
|
|
|
|
}
|