mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-12-20 11:51:17 +00:00
[templating] Add scheduleSegments
function
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
cb68b029ec
commit
9ebdaa8a71
4 changed files with 92 additions and 16 deletions
|
@ -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
|
||||
|
|
|
@ -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`
|
||||
|
|
39
internal/template/twitch/schedule.go
Normal file
39
internal/template/twitch/schedule.go
Normal 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",
|
||||
},
|
||||
})
|
||||
}
|
|
@ -14,26 +14,28 @@ type (
|
|||
// ChannelStreamSchedule represents the schedule of a channels with
|
||||
// its segments represening single planned streams
|
||||
ChannelStreamSchedule struct {
|
||||
Segments []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"`
|
||||
} `json:"segments"`
|
||||
BroadcasterID string `json:"broadcaster_id"`
|
||||
BroadcasterName string `json:"broadcaster_name"`
|
||||
BroadcasterLogin string `json:"broadcaster_login"`
|
||||
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"`
|
||||
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 broadcaster’s streaming schedule
|
||||
|
|
Loading…
Reference in a new issue