2021-04-21 20:43:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2021-10-13 12:30:45 +00:00
|
|
|
"time"
|
2021-08-19 14:40:34 +00:00
|
|
|
|
2022-12-26 20:19:48 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2022-12-26 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
2022-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2021-04-21 20:43:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-01-28 23:41:36 +00:00
|
|
|
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))
|
|
|
|
}
|
2021-05-24 19:42:55 +00:00
|
|
|
|
2023-01-28 23:41:36 +00:00
|
|
|
func tplTwitchDisplayName(username string, v ...string) (string, error) {
|
|
|
|
displayName, err := twitchClient.GetDisplayNameForUser(strings.TrimLeft(username, "#"))
|
|
|
|
if len(v) > 0 && (err != nil || displayName == "") {
|
2023-03-24 21:32:00 +00:00
|
|
|
return v[0], nil //nolint:nilerr // Default value, no need to return error
|
2023-01-28 23:41:36 +00:00
|
|
|
}
|
2021-05-24 19:42:55 +00:00
|
|
|
|
2023-01-28 23:41:36 +00:00
|
|
|
return displayName, err
|
|
|
|
}
|
2022-12-26 21:38:14 +00:00
|
|
|
|
2023-01-28 23:41:36 +00:00
|
|
|
func tplTwitchDoesFollow(from, to string) (bool, error) {
|
|
|
|
_, err := twitchClient.GetFollowDate(from, to)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return true, nil
|
2022-12-26 21:38:14 +00:00
|
|
|
|
2023-01-28 23:41:36 +00:00
|
|
|
case errors.Is(err, twitch.ErrUserDoesNotFollow):
|
|
|
|
return false, nil
|
2022-12-26 21:38:14 +00:00
|
|
|
|
2023-01-28 23:41:36 +00:00
|
|
|
default:
|
|
|
|
return false, errors.Wrap(err, "getting follow date")
|
|
|
|
}
|
|
|
|
}
|
2022-12-26 21:38:14 +00:00
|
|
|
|
2023-01-28 23:41:36 +00:00
|
|
|
func tplTwitchFollowAge(from, to string) (time.Duration, error) {
|
|
|
|
since, err := twitchClient.GetFollowDate(from, to)
|
|
|
|
return time.Since(since), errors.Wrap(err, "getting follow date")
|
|
|
|
}
|
2022-12-26 21:38:14 +00:00
|
|
|
|
2023-01-28 23:41:36 +00:00
|
|
|
func tplTwitchFollowDate(from, to string) (time.Time, error) {
|
|
|
|
return twitchClient.GetFollowDate(from, to)
|
|
|
|
}
|
2022-12-26 21:38:14 +00:00
|
|
|
|
2023-01-28 23:41:36 +00:00
|
|
|
func tplTwitchDoesFollowLongerThan(from, to string, t any) (bool, error) {
|
|
|
|
var (
|
|
|
|
age time.Duration
|
|
|
|
err error
|
|
|
|
)
|
2021-04-21 20:43:33 +00:00
|
|
|
|
2023-01-28 23:41:36 +00:00
|
|
|
switch v := t.(type) {
|
|
|
|
case int64:
|
|
|
|
age = time.Duration(v) * time.Second
|
2021-10-13 12:30:45 +00:00
|
|
|
|
2023-01-28 23:41:36 +00:00
|
|
|
case string:
|
|
|
|
if age, err = time.ParseDuration(v); err != nil {
|
|
|
|
return false, errors.Wrap(err, "parsing duration")
|
2021-10-13 12:30:45 +00:00
|
|
|
}
|
2023-01-28 23:41:36 +00:00
|
|
|
|
|
|
|
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
|
2021-04-21 20:43:33 +00:00
|
|
|
}
|