[templating] Add b64urldec and b64urlenc functions

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-08-25 13:14:23 +02:00
parent 6cb9ef7543
commit a39dc5e4c6
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
3 changed files with 55 additions and 0 deletions

View file

@ -47,6 +47,32 @@ Example:
< @tester please refrain from BSG < @tester please refrain from BSG
``` ```
### `b64urldec`
Decodes the input using base64 URL-encoding (like `b64dec` but using `URLEncoding` instead of `StdEncoding`)
Syntax: `b64urldec <input>`
Example:
```
# {{ b64urldec "bXlzdHJpbmc=" }}
< mystring
```
### `b64urlenc`
Encodes the input using base64 URL-encoding (like `b64enc` but using `URLEncoding` instead of `StdEncoding`)
Syntax: `b64urlenc <input>`
Example:
```
# {{ b64urlenc "mystring" }}
< bXlzdHJpbmc=
```
### `botHasBadge` ### `botHasBadge`
Checks whether bot has the given badge in the current channel Checks whether bot has the given badge in the current channel

View file

@ -0,0 +1,27 @@
package strings
import (
"encoding/base64"
"github.com/pkg/errors"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
func Register(args plugins.RegistrationArguments) error {
args.RegisterTemplateFunction("b64urlenc", plugins.GenericTemplateFunctionGetter(base64URLEncode))
args.RegisterTemplateFunction("b64urldec", plugins.GenericTemplateFunctionGetter(base64URLDecode))
return nil
}
func base64URLEncode(v string) string {
return base64.URLEncoding.EncodeToString([]byte(v))
}
func base64URLDecode(v string) (string, error) {
data, err := base64.URLEncoding.DecodeString(v)
if err != nil {
return "", errors.Wrap(err, "decoding string")
}
return string(data), nil
}

View file

@ -46,6 +46,7 @@ import (
"github.com/Luzifer/twitch-bot/v3/internal/template/numeric" "github.com/Luzifer/twitch-bot/v3/internal/template/numeric"
"github.com/Luzifer/twitch-bot/v3/internal/template/random" "github.com/Luzifer/twitch-bot/v3/internal/template/random"
"github.com/Luzifer/twitch-bot/v3/internal/template/slice" "github.com/Luzifer/twitch-bot/v3/internal/template/slice"
"github.com/Luzifer/twitch-bot/v3/internal/template/strings"
"github.com/Luzifer/twitch-bot/v3/internal/template/subscriber" "github.com/Luzifer/twitch-bot/v3/internal/template/subscriber"
"github.com/Luzifer/twitch-bot/v3/pkg/database" "github.com/Luzifer/twitch-bot/v3/pkg/database"
"github.com/Luzifer/twitch-bot/v3/pkg/twitch" "github.com/Luzifer/twitch-bot/v3/pkg/twitch"
@ -90,6 +91,7 @@ var (
numeric.Register, numeric.Register,
random.Register, random.Register,
slice.Register, slice.Register,
strings.Register,
subscriber.Register, subscriber.Register,
// API-only modules // API-only modules