2023-09-03 11:01:57 +00:00
|
|
|
package twitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
regFn = append(
|
|
|
|
regFn,
|
|
|
|
tplTwitchDisplayName,
|
|
|
|
tplTwitchIDForUsername,
|
|
|
|
tplTwitchProfileImage,
|
2024-04-17 22:26:14 +00:00
|
|
|
tplTwitchUserExists,
|
2023-09-03 11:01:57 +00:00
|
|
|
tplTwitchUsernameForID,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tplTwitchDisplayName(args plugins.RegistrationArguments) {
|
|
|
|
args.RegisterTemplateFunction("displayName", plugins.GenericTemplateFunctionGetter(func(username string, v ...string) (string, error) {
|
2024-01-01 16:52:18 +00:00
|
|
|
displayName, err := args.GetTwitchClient().GetDisplayNameForUser(context.Background(), strings.TrimLeft(username, "#"))
|
2023-09-03 11:01:57 +00:00
|
|
|
if len(v) > 0 && (err != nil || displayName == "") {
|
|
|
|
return v[0], nil //nolint:nilerr // Default value, no need to return error
|
|
|
|
}
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
return displayName, errors.Wrap(err, "getting display name")
|
2023-09-03 11:01:57 +00:00
|
|
|
}), plugins.TemplateFuncDocumentation{
|
|
|
|
Description: "Returns the display name the specified user set for themselves",
|
|
|
|
Syntax: "displayName <username> [fallback]",
|
|
|
|
Example: &plugins.TemplateFuncDocumentationExample{
|
|
|
|
Template: `{{ displayName "luziferus" }} - {{ displayName "notexistinguser" "foobar" }}`,
|
|
|
|
FakedOutput: "Luziferus - foobar",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func tplTwitchIDForUsername(args plugins.RegistrationArguments) {
|
|
|
|
args.RegisterTemplateFunction("idForUsername", plugins.GenericTemplateFunctionGetter(func(username string) (string, error) {
|
2024-01-01 16:52:18 +00:00
|
|
|
id, err := args.GetTwitchClient().GetIDForUsername(context.Background(), username)
|
|
|
|
return id, errors.Wrap(err, "getting ID for username")
|
2023-09-03 11:01:57 +00:00
|
|
|
}), plugins.TemplateFuncDocumentation{
|
|
|
|
Description: "Returns the user-id for the given username",
|
|
|
|
Syntax: "idForUsername <username>",
|
|
|
|
Example: &plugins.TemplateFuncDocumentationExample{
|
|
|
|
Template: `{{ idForUsername "twitch" }}`,
|
|
|
|
FakedOutput: "12826",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func tplTwitchProfileImage(args plugins.RegistrationArguments) {
|
|
|
|
args.RegisterTemplateFunction("profileImage", plugins.GenericTemplateFunctionGetter(func(username string) (string, error) {
|
2024-01-01 16:52:18 +00:00
|
|
|
user, err := args.GetTwitchClient().GetUserInformation(context.Background(), strings.TrimLeft(username, "#@"))
|
2023-09-03 11:01:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "getting user info")
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.ProfileImageURL, nil
|
|
|
|
}), plugins.TemplateFuncDocumentation{
|
|
|
|
Description: "Gets the URL of the given users profile image",
|
|
|
|
Syntax: "profileImage <username>",
|
|
|
|
Example: &plugins.TemplateFuncDocumentationExample{
|
|
|
|
Template: `{{ profileImage .username }}`,
|
|
|
|
FakedOutput: "https://static-cdn.jtvnw.net/jtv_user_pictures/[...].png",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-17 22:26:14 +00:00
|
|
|
func tplTwitchUserExists(args plugins.RegistrationArguments) {
|
|
|
|
args.RegisterTemplateFunction("userExists", plugins.GenericTemplateFunctionGetter(func(username string) bool {
|
|
|
|
user, err := args.GetTwitchClient().GetUserInformation(context.Background(), strings.TrimLeft(username, "#@"))
|
|
|
|
if err != nil {
|
|
|
|
// Well, they probably don't exist
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.EqualFold(username, user.Login)
|
|
|
|
}), plugins.TemplateFuncDocumentation{
|
|
|
|
Description: "Checks whether the given user exists",
|
|
|
|
Syntax: "userExists <username>",
|
|
|
|
Example: &plugins.TemplateFuncDocumentationExample{
|
|
|
|
Template: `{{ userExists "luziferus" }}`,
|
|
|
|
FakedOutput: "true",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-03 11:01:57 +00:00
|
|
|
func tplTwitchUsernameForID(args plugins.RegistrationArguments) {
|
|
|
|
args.RegisterTemplateFunction("usernameForID", plugins.GenericTemplateFunctionGetter(func(id string) (string, error) {
|
2024-01-01 16:52:18 +00:00
|
|
|
username, err := args.GetTwitchClient().GetUsernameForID(context.Background(), id)
|
|
|
|
return username, errors.Wrap(err, "getting username for ID")
|
2023-09-03 11:01:57 +00:00
|
|
|
}), plugins.TemplateFuncDocumentation{
|
|
|
|
Description: "Returns the current login name of an user-id",
|
|
|
|
Syntax: "usernameForID <user-id>",
|
|
|
|
Example: &plugins.TemplateFuncDocumentationExample{
|
|
|
|
Template: `{{ usernameForID "12826" }}`,
|
|
|
|
FakedOutput: "twitch",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|