1
0
Fork 0
mirror of https://github.com/Luzifer/twitch-bot.git synced 2025-03-16 03:35:59 +00:00

[template] Add recentTitle template function

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-01-29 00:41:36 +01:00
parent 4886b90b62
commit 5753bfc115
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
2 changed files with 112 additions and 77 deletions

View file

@ -11,81 +11,102 @@ import (
)
func init() {
tplFuncs.Register("displayName", plugins.GenericTemplateFunctionGetter(func(username string, v ...string) (string, error) {
displayName, err := twitchClient.GetDisplayNameForUser(strings.TrimLeft(username, "#"))
if len(v) > 0 && (err != nil || displayName == "") {
return v[0], nil
}
return displayName, err
}))
tplFuncs.Register("doesFollow", plugins.GenericTemplateFunctionGetter(func(from, to string) (bool, error) {
_, err := twitchClient.GetFollowDate(from, to)
switch {
case err == nil:
return true, nil
case errors.Is(err, twitch.ErrUserDoesNotFollow):
return false, nil
default:
return false, errors.Wrap(err, "getting follow date")
}
}))
tplFuncs.Register("followAge", plugins.GenericTemplateFunctionGetter(func(from, to string) (time.Duration, error) {
since, err := twitchClient.GetFollowDate(from, to)
return time.Since(since), errors.Wrap(err, "getting follow date")
}))
tplFuncs.Register("followDate", plugins.GenericTemplateFunctionGetter(func(from, to string) (time.Time, error) { return twitchClient.GetFollowDate(from, to) }))
tplFuncs.Register("doesFollowLongerThan", plugins.GenericTemplateFunctionGetter(func(from, to string, t any) (bool, error) {
var (
age time.Duration
err error
)
switch v := t.(type) {
case int64:
age = time.Duration(v) * time.Second
case string:
if age, err = time.ParseDuration(v); err != nil {
return false, errors.Wrap(err, "parsing duration")
}
default:
return false, errors.Errorf("unexpected input for duration %t", t)
}
fd, err := twitchClient.GetFollowDate(from, to)
switch {
case err == nil:
return time.Since(fd) > age, nil
case errors.Is(err, twitch.ErrUserDoesNotFollow):
return false, nil
default:
return false, errors.Wrap(err, "getting follow date")
}
}))
tplFuncs.Register("recentGame", plugins.GenericTemplateFunctionGetter(func(username string, v ...string) (string, error) {
game, _, err := twitchClient.GetRecentStreamInfo(strings.TrimLeft(username, "#"))
if len(v) > 0 && (err != nil || game == "") {
return v[0], nil
}
return game, err
}))
tplFuncs.Register("streamUptime", plugins.GenericTemplateFunctionGetter(func(username string) (time.Duration, error) {
si, err := twitchClient.GetCurrentStreamInfo(strings.TrimLeft(username, "#"))
if err != nil {
return 0, err
}
return time.Since(si.StartedAt), nil
}))
tplFuncs.Register("displayName", plugins.GenericTemplateFunctionGetter(tplTwitchDisplayName))
tplFuncs.Register("doesFollow", plugins.GenericTemplateFunctionGetter(tplTwitchDoesFollow))
tplFuncs.Register("followAge", plugins.GenericTemplateFunctionGetter(tplTwitchFollowAge))
tplFuncs.Register("followDate", plugins.GenericTemplateFunctionGetter(tplTwitchFollowDate))
tplFuncs.Register("doesFollowLongerThan", plugins.GenericTemplateFunctionGetter(tplTwitchDoesFollowLongerThan))
tplFuncs.Register("recentGame", plugins.GenericTemplateFunctionGetter(tplTwitchRecentGame))
tplFuncs.Register("recentTitle", plugins.GenericTemplateFunctionGetter(tplTwitchRecentTitle))
tplFuncs.Register("streamUptime", plugins.GenericTemplateFunctionGetter(tplTwitchStreamUptime))
}
func tplTwitchDisplayName(username string, v ...string) (string, error) {
displayName, err := twitchClient.GetDisplayNameForUser(strings.TrimLeft(username, "#"))
if len(v) > 0 && (err != nil || displayName == "") {
return v[0], nil
}
return displayName, err
}
func tplTwitchDoesFollow(from, to string) (bool, error) {
_, err := twitchClient.GetFollowDate(from, to)
switch {
case err == nil:
return true, nil
case errors.Is(err, twitch.ErrUserDoesNotFollow):
return false, nil
default:
return false, errors.Wrap(err, "getting follow date")
}
}
func tplTwitchFollowAge(from, to string) (time.Duration, error) {
since, err := twitchClient.GetFollowDate(from, to)
return time.Since(since), errors.Wrap(err, "getting follow date")
}
func tplTwitchFollowDate(from, to string) (time.Time, error) {
return twitchClient.GetFollowDate(from, to)
}
func tplTwitchDoesFollowLongerThan(from, to string, t any) (bool, error) {
var (
age time.Duration
err error
)
switch v := t.(type) {
case int64:
age = time.Duration(v) * time.Second
case string:
if age, err = time.ParseDuration(v); err != nil {
return false, errors.Wrap(err, "parsing duration")
}
default:
return false, errors.Errorf("unexpected input for duration %t", t)
}
fd, err := twitchClient.GetFollowDate(from, to)
switch {
case err == nil:
return time.Since(fd) > age, nil
case errors.Is(err, twitch.ErrUserDoesNotFollow):
return false, nil
default:
return false, errors.Wrap(err, "getting follow date")
}
}
func tplTwitchRecentGame(username string, v ...string) (string, error) {
game, _, err := twitchClient.GetRecentStreamInfo(strings.TrimLeft(username, "#"))
if len(v) > 0 && (err != nil || game == "") {
return v[0], nil
}
return game, err
}
func tplTwitchRecentTitle(username string, v ...string) (string, error) {
_, title, err := twitchClient.GetRecentStreamInfo(strings.TrimLeft(username, "#"))
if len(v) > 0 && (err != nil || title == "") {
return v[0], nil
}
return title, err
}
func tplTwitchStreamUptime(username string) (time.Duration, error) {
si, err := twitchClient.GetCurrentStreamInfo(strings.TrimLeft(username, "#"))
if err != nil {
return 0, err
}
return time.Since(si.StartedAt), nil
}

View file

@ -283,6 +283,20 @@ Example:
< Metro Exodus - none
```
#### `recentTitle`
Returns the last stream title of the specified user or the `fallback` if the title could not be fetched. If no fallback was supplied the message will fail and not be sent.
Syntax: `recentTitle <username> [fallback]`
Example:
```
# {{ recentGame "luziferus" "none" }} - {{ recentGame "thisuserdoesnotexist123" "none" }}
< Die Oper haben wir überlebt, mal sehen was uns sonst noch alles töten möchte - none
```
#### `streamUptime`
Returns the duration the stream is online (causes an error if no current stream is found)