Add randomString template function

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-08-16 17:55:23 +02:00
parent c0545d1d87
commit 07a83e2fdb
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
2 changed files with 22 additions and 0 deletions

View file

@ -12,10 +12,19 @@ import (
) )
func Register(args plugins.RegistrationArguments) error { func Register(args plugins.RegistrationArguments) error {
args.RegisterTemplateFunction("randomString", plugins.GenericTemplateFunctionGetter(randomString))
args.RegisterTemplateFunction("seededRandom", plugins.GenericTemplateFunctionGetter(stableRandomFromSeed)) args.RegisterTemplateFunction("seededRandom", plugins.GenericTemplateFunctionGetter(stableRandomFromSeed))
return nil return nil
} }
func randomString(lst ...string) (string, error) {
if len(lst) == 0 {
return "", errors.New("empty list given")
}
return lst[rand.Intn(len(lst))], nil
}
func stableRandomFromSeed(seed string) (float64, error) { func stableRandomFromSeed(seed string) (float64, error) {
seedValue, err := stringToSeed(seed) seedValue, err := stringToSeed(seed)
if err != nil { if err != nil {

View file

@ -251,6 +251,19 @@ Example:
< 10000 < 10000
``` ```
#### `randomString`
Randomly picks a string from a list of strings
Syntax: `randomString "a" [...]`
Example:
```
# {{ randomString "a" "b" "c" "d" }}
< a
```
#### `recentGame` #### `recentGame`
Returns the last played game name of the specified user (see shoutout example) or the `fallback` if the game could not be fetched. If no fallback was supplied the message will fail and not be sent. Returns the last played game name of the specified user (see shoutout example) or the `fallback` if the game could not be fetched. If no fallback was supplied the message will fail and not be sent.