mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-09 16:50:01 +00:00
[templating] Add b64urldec
and b64urlenc
functions
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
6cb9ef7543
commit
a39dc5e4c6
3 changed files with 55 additions and 0 deletions
|
@ -47,6 +47,32 @@ Example:
|
|||
< @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`
|
||||
|
||||
Checks whether bot has the given badge in the current channel
|
||||
|
|
27
internal/template/strings/strings.go
Normal file
27
internal/template/strings/strings.go
Normal 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
|
||||
}
|
|
@ -46,6 +46,7 @@ import (
|
|||
"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/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/pkg/database"
|
||||
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
||||
|
@ -90,6 +91,7 @@ var (
|
|||
numeric.Register,
|
||||
random.Register,
|
||||
slice.Register,
|
||||
strings.Register,
|
||||
subscriber.Register,
|
||||
|
||||
// API-only modules
|
||||
|
|
Loading…
Reference in a new issue