From ddb62f08f869382583c6f81b8315f738f6fea4f6 Mon Sep 17 00:00:00 2001 From: DogeisCut <23178844+DogeisCut@users.noreply.github.com> Date: Thu, 5 Sep 2024 21:36:11 -0600 Subject: [PATCH] Compression macros :) --- src/cogs/macros.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/cogs/macros.py b/src/cogs/macros.py index 5f6fe8e1a..545e7af18 100644 --- a/src/cogs/macros.py +++ b/src/cogs/macros.py @@ -6,6 +6,8 @@ from typing import Optional, Callable import json import time +import base64 +import zlib from .. import constants, errors from ..types import Bot, BuiltinMacro @@ -456,6 +458,32 @@ def upper(text: str): def title(text: str): return text.title() + @builtin("base64.encode") + def base64encode(string: str): + text_bytes = string.encode('utf-8') + base64_bytes = base64.b64encode(text_bytes) + return base64_bytes.decode('utf-8') + + @builtin("base64.decode") + def base64decode(string: str): + base64_bytes = string.encode('utf-8') + text_bytes = base64.b64decode(base64_bytes) + return text_bytes.decode('utf-8') + + @builtin("zlib.compress") + def zlibcompress(data: str): + text_bytes = data.encode('utf-8') + compressed_bytes = zlib.compress(text_bytes) + base64_compressed = base64.b64encode(compressed_bytes) + return base64_compressed.decode('utf-8') + + @builtin("zlib.decompress") + def zlibdecompress(data: str): + base64_compressed = data.encode('utf-8') + compressed_bytes = base64.b64decode(base64_compressed) + text_bytes = zlib.decompress(compressed_bytes) + return text_bytes.decode('utf-8') + self.builtins = dict(sorted(self.builtins.items(), key=lambda tup: tup[0])) def parse_macros(self, objects: str, debug_info: bool, macros=None, cmd="x", init=True) -> tuple[Optional[str], Optional[list[str]]]: