[templating] Add scheduleSegments function

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-11-05 13:15:42 +01:00
parent cb68b029ec
commit 9ebdaa8a71
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
4 changed files with 92 additions and 16 deletions

View file

@ -55,6 +55,28 @@ title: "Rule Examples"
- moderator - moderator
``` ```
## Display Stream-Schedule in Chat
```yaml
- actions:
- type: respond
attributes:
message: |-
{{- $segs := scheduleSegments .channel 3 -}}
{{- $fmtSegs := list -}}
{{- range $segs -}}
{{- $fmtSegs = mustAppend $fmtSegs (
printf "%s @ %s"
(.Category.Name)
(dateInZone "02.01. 15:40" .StartTime "Europe/Berlin")
) -}}
{{- end -}}
Next streams are: {{ $fmtSegs | join ", " }}
- See more in the Twitch schedule:
https://www.twitch.tv/{{ fixUsername .channel }}/schedule
match_message: '!schedule\b'
```
## Game death counter with dynamic name ## Game death counter with dynamic name
```yaml ```yaml

View file

@ -418,6 +418,19 @@ Example:
* Die Oper haben wir überlebt, mal sehen was uns sonst noch alles töten möchte… - none * Die Oper haben wir überlebt, mal sehen was uns sonst noch alles töten möchte… - none
``` ```
### `scheduleSegments`
Returns the next n segments in the channels schedule. If n is not given, returns all known segments.
Syntax: `scheduleSegments <channel> [n]`
Example:
```
# {{ $seg := scheduleSegments "luziferus" 1 | first }}Next Stream: {{ $seg.Title }} @ {{ dateInZone "2006-01-02 15:04" $seg.StartTime "Europe/Berlin" }}
* Next Stream: Little Nightmares @ 2023-11-05 18:00
```
### `seededRandom` ### `seededRandom`
Returns a float value stable for the given seed Returns a float value stable for the given seed
@ -428,7 +441,7 @@ Example:
``` ```
# Your int this hour: {{ printf "%.0f" (mulf (seededRandom (list "int" .username (now | date "2006-01-02 15") | join ":")) 100) }}% # Your int this hour: {{ printf "%.0f" (mulf (seededRandom (list "int" .username (now | date "2006-01-02 15") | join ":")) 100) }}%
< Your int this hour: 37% < Your int this hour: 73%
``` ```
### `streamUptime` ### `streamUptime`

View file

@ -0,0 +1,39 @@
package twitch
import (
"context"
"math"
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
"github.com/Luzifer/twitch-bot/v3/plugins"
"github.com/pkg/errors"
)
func init() {
regFn = append(
regFn,
tplTwitchScheduleSegments,
)
}
func tplTwitchScheduleSegments(args plugins.RegistrationArguments) {
args.RegisterTemplateFunction("scheduleSegments", plugins.GenericTemplateFunctionGetter(func(channel string, n ...int) ([]twitch.ChannelStreamScheduleSegment, error) {
schedule, err := args.GetTwitchClient().GetChannelStreamSchedule(context.Background(), channel)
if err != nil {
return nil, errors.Wrap(err, "getting schedule")
}
if len(n) > 0 {
return schedule.Segments[:int(math.Min(float64(n[0]), float64(len(schedule.Segments))))], nil
}
return schedule.Segments, nil
}), plugins.TemplateFuncDocumentation{
Description: "Returns the next n segments in the channels schedule. If n is not given, returns all known segments.",
Syntax: "scheduleSegments <channel> [n]",
Example: &plugins.TemplateFuncDocumentationExample{
Template: `{{ $seg := scheduleSegments "luziferus" 1 | first }}Next Stream: {{ $seg.Title }} @ {{ dateInZone "2006-01-02 15:04" $seg.StartTime "Europe/Berlin" }}`,
FakedOutput: "Next Stream: Little Nightmares @ 2023-11-05 18:00",
},
})
}

View file

@ -14,26 +14,28 @@ type (
// ChannelStreamSchedule represents the schedule of a channels with // ChannelStreamSchedule represents the schedule of a channels with
// its segments represening single planned streams // its segments represening single planned streams
ChannelStreamSchedule struct { ChannelStreamSchedule struct {
Segments []struct { Segments []ChannelStreamScheduleSegment `json:"segments"`
ID string `json:"id"` BroadcasterID string `json:"broadcaster_id"`
StartTime time.Time `json:"start_time"` BroadcasterName string `json:"broadcaster_name"`
EndTime time.Time `json:"end_time"` BroadcasterLogin string `json:"broadcaster_login"`
Title string `json:"title"`
CanceledUntil *time.Time `json:"canceled_until"`
Category struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"category"`
IsRecurring bool `json:"is_recurring"`
} `json:"segments"`
BroadcasterID string `json:"broadcaster_id"`
BroadcasterName string `json:"broadcaster_name"`
BroadcasterLogin string `json:"broadcaster_login"`
Vacation struct { Vacation struct {
StartTime time.Time `json:"start_time"` StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"` EndTime time.Time `json:"end_time"`
} `json:"vacation"` } `json:"vacation"`
} }
ChannelStreamScheduleSegment struct {
ID string `json:"id"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
Title string `json:"title"`
CanceledUntil *time.Time `json:"canceled_until"`
Category struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"category"`
IsRecurring bool `json:"is_recurring"`
}
) )
// GetChannelStreamSchedule gets the broadcasters streaming schedule // GetChannelStreamSchedule gets the broadcasters streaming schedule