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-11-02 21:38:14 +00:00
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2021-04-21 20:43:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-08-19 14:40:34 +00:00
|
|
|
tplFuncs.Register("displayName", plugins.GenericTemplateFunctionGetter(func(username string, v ...string) (string, error) {
|
2021-08-19 13:33:56 +00:00
|
|
|
displayName, err := twitchClient.GetDisplayNameForUser(strings.TrimLeft(username, "#"))
|
2021-05-24 19:47:10 +00:00
|
|
|
if len(v) > 0 && (err != nil || displayName == "") {
|
2021-05-24 19:42:55 +00:00
|
|
|
return v[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return displayName, err
|
|
|
|
}))
|
|
|
|
|
2021-10-13 12:30:45 +00:00
|
|
|
tplFuncs.Register("followDate", plugins.GenericTemplateFunctionGetter(func(from, to string) (time.Time, error) { return twitchClient.GetFollowDate(from, to) }))
|
|
|
|
|
2021-08-19 14:40:34 +00:00
|
|
|
tplFuncs.Register("recentGame", plugins.GenericTemplateFunctionGetter(func(username string, v ...string) (string, error) {
|
2021-08-19 13:33:56 +00:00
|
|
|
game, _, err := twitchClient.GetRecentStreamInfo(strings.TrimLeft(username, "#"))
|
2021-05-24 19:53:17 +00:00
|
|
|
if len(v) > 0 && (err != nil || game == "") {
|
2021-04-21 20:43:33 +00:00
|
|
|
return v[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return game, err
|
|
|
|
}))
|
2021-10-13 12:30:45 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}))
|
2021-04-21 20:43:33 +00:00
|
|
|
}
|