[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
```
## 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
```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
```
### `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`
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: 37%
< Your int this hour: 73%
```
### `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,7 +14,17 @@ type (
// ChannelStreamSchedule represents the schedule of a channels with
// its segments represening single planned streams
ChannelStreamSchedule struct {
Segments []struct {
Segments []ChannelStreamScheduleSegment `json:"segments"`
BroadcasterID string `json:"broadcaster_id"`
BroadcasterName string `json:"broadcaster_name"`
BroadcasterLogin string `json:"broadcaster_login"`
Vacation struct {
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
} `json:"vacation"`
}
ChannelStreamScheduleSegment struct {
ID string `json:"id"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
@ -25,14 +35,6 @@ type (
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 {
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
} `json:"vacation"`
}
)