[template] Add doesFollow and doesFollowLongerThan functions

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-12-26 22:38:14 +01:00
parent 63ea08a4cd
commit 37a82002df
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
3 changed files with 84 additions and 2 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/pkg/errors"
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
@ -19,12 +20,58 @@ func init() {
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 == "") {

View file

@ -19,6 +19,8 @@ type (
}
)
var ErrUserDoesNotFollow = errors.New("no follow-relation found")
func (c *Client) GetAuthorizedUser() (userID string, userName string, err error) {
var payload struct {
Data []User `json:"data"`
@ -104,8 +106,15 @@ func (c *Client) GetFollowDate(from, to string) (time.Time, error) {
return time.Time{}, errors.Wrap(err, "request follow info")
}
if l := len(payload.Data); l != 1 {
return time.Time{}, errors.Errorf("unexpected number of records returned: %d", l)
switch len(payload.Data) {
case 0:
return time.Time{}, ErrUserDoesNotFollow
case 1:
// Handled below, no error
default:
return time.Time{}, errors.Errorf("unexpected number of records returned: %d", len(payload.Data))
}
// Follow date will not change that often, cache for a long time

View file

@ -108,6 +108,32 @@ Example:
< Luziferus - foobar
```
#### `doesFollow`
Returns whether `from` follows `to`
Syntax: `doesFollow <from> <to>`
Example:
```
# {{ doesFollow "tezrian" "luziferus" }}
< true
```
#### `doesFollowLongerThan`
Returns whether `from` follows `to` for more than `duration`
Syntax: `doesFollowLongerThan <from> <to> <duration>`
Example:
```
# {{ doesFollowLongerThan "tezrian" "luziferus" "168h" }}
< true
```
#### `fixUsername`
Ensures the username no longer contains the `@` or `#` prefix