Add time to next stream to presence module

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-07-29 21:39:14 +02:00
parent fa6997f684
commit da6c0b35c2
Signed by: luzifer
GPG Key ID: 0066F03ED215AD7D
2 changed files with 42 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"context"
"fmt"
"math"
"strings"
@ -32,6 +33,9 @@ func (m *modPresence) Initialize(crontab *cron.Cron, discord *discordgo.Session,
if err := attrs.Expect(
"fallback_text",
"twitch_channel_id",
"twitch_client_id",
"twitch_token",
); err != nil {
return errors.Wrap(err, "validating attributes")
}
@ -47,7 +51,33 @@ func (m *modPresence) Initialize(crontab *cron.Cron, discord *discordgo.Session,
func (m modPresence) cronUpdatePresence() {
var nextStream *time.Time = nil
// FIXME: Get next stream status
twitch := newTwitchAdapter(
// @attr twitch_client_id required string "" Twitch client ID the token was issued for
m.attrs.MustString("twitch_client_id", nil),
// @attr twitch_token required string "" Token for the user the `twitch_channel_id` belongs to
m.attrs.MustString("twitch_token", nil),
)
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
}
nextStream = seg.StartTime
break
}
// @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)
@ -63,13 +93,16 @@ func (m modPresence) cronUpdatePresence() {
}
func (m modPresence) durationToHumanReadable(d time.Duration) string {
d = time.Duration(math.Abs(float64(d)))
if d > time.Hour*24 {
return fmt.Sprintf("%.0f Tagen", math.Ceil(float64(d)/float64(time.Hour*24)))
}
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,
time.Hour: true,
time.Minute: true,
} {
if d < div && !req {
continue

View File

@ -23,7 +23,11 @@ Updates the presence status of the bot to display the next stream
| Attribute | Req. | Type | Default Value | Description |
| --------- | :--: | ---- | ------------- | ----------- |
| `fallback_text` | ✅ | string | | What to set the text to when no stream is found (`playing <text>`) |
| `twitch_channel_id` | ✅ | string | | ID (not name) of the channel to fetch the schedule from |
| `twitch_client_id` | ✅ | string | | Twitch client ID the token was issued for |
| `twitch_token` | ✅ | string | | Token for the user the `twitch_channel_id` belongs to |
| `cron` | | string | `* * * * *` | When to execute the module |
| `schedule_past_time` | | duration | `15m` | How long in the past should the schedule contain an entry |
## Type: `schedule`