diff --git a/internal/template/random/random.go b/internal/template/random/random.go index 5a65a31..c157b39 100644 --- a/internal/template/random/random.go +++ b/internal/template/random/random.go @@ -12,10 +12,19 @@ import ( ) func Register(args plugins.RegistrationArguments) error { + args.RegisterTemplateFunction("randomString", plugins.GenericTemplateFunctionGetter(randomString)) args.RegisterTemplateFunction("seededRandom", plugins.GenericTemplateFunctionGetter(stableRandomFromSeed)) 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) { seedValue, err := stringToSeed(seed) if err != nil { diff --git a/wiki/Templating.md b/wiki/Templating.md index 1107fd9..092a0dc 100644 --- a/wiki/Templating.md +++ b/wiki/Templating.md @@ -251,6 +251,19 @@ Example: < 10000 ``` +#### `randomString` + +Randomly picks a string from a list of strings + +Syntax: `randomString "a" [...]` + +Example: + +``` +# {{ randomString "a" "b" "c" "d" }} +< a +``` + #### `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.