[templating] Add userExists function

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-04-18 00:26:14 +02:00
parent f56a7a3266
commit ebf734be40
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
3 changed files with 35 additions and 2 deletions

View file

@ -523,7 +523,7 @@ Send raw IRC message
## Send Whisper
Send a whisper (requires a verified bot!)
Send a whisper
```yaml
- type: whisper

View file

@ -467,7 +467,7 @@ Example:
```
# Your int this hour: {{ printf "%.0f" (mulf (seededRandom (list "int" .username (now | date "2006-01-02 15") | join ":")) 100) }}%
< Your int this hour: 88%
< Your int this hour: 72%
```
### `spotifyCurrentPlaying`
@ -567,6 +567,19 @@ Example:
* Weather for Hamburg, DE: Few clouds with a temperature of 22 C (71.6 F). [...]
```
### `userExists`
Checks whether the given user exists
Syntax: `userExists <username>`
Example:
```
# {{ userExists "luziferus" }}
* true
```
### `usernameForID`
Returns the current login name of an user-id

View file

@ -14,6 +14,7 @@ func init() {
tplTwitchDisplayName,
tplTwitchIDForUsername,
tplTwitchProfileImage,
tplTwitchUserExists,
tplTwitchUsernameForID,
)
}
@ -68,6 +69,25 @@ func tplTwitchProfileImage(args plugins.RegistrationArguments) {
})
}
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",
},
})
}
func tplTwitchUsernameForID(args plugins.RegistrationArguments) {
args.RegisterTemplateFunction("usernameForID", plugins.GenericTemplateFunctionGetter(func(id string) (string, error) {
username, err := args.GetTwitchClient().GetUsernameForID(context.Background(), id)