From 087c5f01953a0ce5c625db03532f55db2b5478f3 Mon Sep 17 00:00:00 2001 From: chris_dodgers <173489754+chrisdodgers@users.noreply.github.com> Date: Mon, 23 Mar 2026 00:02:40 -0400 Subject: [PATCH 1/2] Add $mem for converting MB to lhex and vise-versa Aliases also include $fbmem, $stolenmem, $unifiedmem, $cursormem Usage example: $stolenmem 26m $fbmem 00009000 --- Cogs/Encode.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 ++ 2 files changed, 52 insertions(+) mode change 100755 => 100644 Cogs/Encode.py diff --git a/Cogs/Encode.py b/Cogs/Encode.py old mode 100755 new mode 100644 index 6729f065..06dcf45c --- a/Cogs/Encode.py +++ b/Cogs/Encode.py @@ -530,3 +530,53 @@ async def encode(self, ctx, from_type = None, to_type = None, *, value = None): return await ctx.send(Nullify.escape_all(self._convert_value(value,from_type,to_type))) except Exception as e: return await ctx.send(Nullify.escape_all("I couldn't make that conversion:\n{}".format(e))) + + @commands.command(aliases=["fbmem", "stolenmem", "unifiedmem", "cursormem"]) + async def mem(self, ctx, *, input=None): + """Converts between MiB and little-endian hexadecimal (lhex) for calculating stolenmem, fbmem, etc. values.""" + usage = 'Usage: `{}mem [input] (e.g. 26MB or 0000A001)`'.format(ctx.prefix) + if input is None: + return await ctx.send(usage) + + try: + val = input.strip() + mib = ["mib", "mb", "m"] + + # Determine if the input value is MiB or lhex. + is_mib = any(x in val.lower() for x in mib) + is_lhex = len(self._check_hex(val)) + if not is_mib and not is_lhex: + return await ctx.send(usage) + # Set the from and to type based on the above check. + if is_mib: + from_type = "mib" + to_type = "lhex" + else: + from_type = "lhex" + to_type = "mib" + + # Convert MiB to lhex + if from_type == "mib" and to_type == "lhex": + # Store as a float and remove mib/mb/m + num = float(re.sub('|'.join(mib), '', val.lower()).strip()) + # Convert to bytes + bytes_val = int(num * 1024 * 1024) + # Format from an int to a hex value and pre-pad with 8 characters + hex_val = "{:08x}".format(bytes_val) + # Convert from hex to lhex + out = self._convert_value(hex_val, "hex", "lhex") + return await ctx.send(Nullify.escape_all(self._check_hex(out))) + # Seacrest out! + + # Convert lhex to MiB + elif from_type == "lhex" and to_type == "mib": + # Convert from lhex to decimal + dec_val = self._convert_value(val, "lhex", "decimal") + # Convert to MiB + mib_val = round(int(dec_val) / (1024 * 1024), 4) + return await ctx.send(f"{mib_val} MiB") + else: + return await ctx.send(usage) + except Exception: + return await ctx.send( + Nullify.escape_all("I couldn't make that conversion!")) \ No newline at end of file diff --git a/README.md b/README.md index 51676471..c9b06bce 100644 --- a/README.md +++ b/README.md @@ -453,6 +453,8 @@ A list of cogs, commands, and descriptions: └─ Byte swaps the passed hex value. $intbin [input_int] └─ Converts the input integer to its binary representation. + $mem [input] (AKA: fbmem, stolenmem, unifiedmem, cursormem) + └─ Converts between MiB and little-endian hexadecimal (lhex) for calculating fbm... $randomcolor └─ Selects a random color. $strbin [input_string] From 97bdb5f392c7e5625c661903afc20bc6c58bc0ab Mon Sep 17 00:00:00 2001 From: chris_dodgers <173489754+chrisdodgers@users.noreply.github.com> Date: Wed, 25 Mar 2026 18:49:25 -0400 Subject: [PATCH 2/2] Cleaned up $mem MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Removed `mib = [“mib”, “mb”, “m”]` as it is unnecessarily redundant. Just checking for if `”m”` exists in the input value serves the same expected results determining if dealing with MiB input . - Removed `is_lhex`. Kind of pointless as if the input value doesn’t fall under `is_mib`, just assume its lhex. Any bad input values resulting in an error would just be caught in the try/except. - Removed the unnecessary string comparisons of `from_type` and `to_type`. Conversion logic for MiB -> lhex happens now in `if is_mib`, and `else` we do lhex -> MiB. - Removed the regex handling in `num` to strip `mib` from the input value. Instead, just simply only storing `0-9` and `.` in the float and ignoring any other character that may be in the input value. - Cleaned up the calculation for `bytes_val` by using `num * 1024 ** 2` instead of `num * 1024 *1024`. (And cleaned this up likewise in `mib_val`). - Removed `hex_val` and instead directly feeding `bytes_val` to `_convert_value` using `decimal` to `lhex32`. Using `lhex32` instead of originally `lhex` replaces the previous need of pre-formatting to get proper padding. - Removed the use of Nullify. The output of `_convert_value()` is already sanitized and can be fed directly to the ctx. Also it is not needed in the particular response in `except Exception`. Huge credit and thanks to @corpnewt for these clean up suggestions and points I originally overlooked. --- Cogs/Encode.py | 52 ++++++++++++++++---------------------------------- 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/Cogs/Encode.py b/Cogs/Encode.py index 06dcf45c..7e9f98d8 100644 --- a/Cogs/Encode.py +++ b/Cogs/Encode.py @@ -534,49 +534,29 @@ async def encode(self, ctx, from_type = None, to_type = None, *, value = None): @commands.command(aliases=["fbmem", "stolenmem", "unifiedmem", "cursormem"]) async def mem(self, ctx, *, input=None): """Converts between MiB and little-endian hexadecimal (lhex) for calculating stolenmem, fbmem, etc. values.""" + usage = 'Usage: `{}mem [input] (e.g. 26MB or 0000A001)`'.format(ctx.prefix) if input is None: return await ctx.send(usage) try: val = input.strip() - mib = ["mib", "mb", "m"] - - # Determine if the input value is MiB or lhex. - is_mib = any(x in val.lower() for x in mib) - is_lhex = len(self._check_hex(val)) - if not is_mib and not is_lhex: - return await ctx.send(usage) - # Set the from and to type based on the above check. + # Determine if the input value is MiB. If not MiB, will assume the input is lhex and will catch bad input with the try/except. + is_mib = "m" in val.lower() + + # Convert MiB to lhex: if is_mib: - from_type = "mib" - to_type = "lhex" + # Store as a float and only store "0-9" and "." - ignoring other characters. + num = float("".join([x for x in val if x in "01234596789."])) + # Convert to bytes: + bytes_val = int(num * 1024 ** 2) + # Convert from decimal to lhex (using _check_hex to omit `0x` in the output). Using lhex32 which will auto-pad. + return await ctx.send(self._check_hex(self._convert_value(bytes_val, "decimal", "lhex32"))) else: - from_type = "lhex" - to_type = "mib" - - # Convert MiB to lhex - if from_type == "mib" and to_type == "lhex": - # Store as a float and remove mib/mb/m - num = float(re.sub('|'.join(mib), '', val.lower()).strip()) - # Convert to bytes - bytes_val = int(num * 1024 * 1024) - # Format from an int to a hex value and pre-pad with 8 characters - hex_val = "{:08x}".format(bytes_val) - # Convert from hex to lhex - out = self._convert_value(hex_val, "hex", "lhex") - return await ctx.send(Nullify.escape_all(self._check_hex(out))) - # Seacrest out! - - # Convert lhex to MiB - elif from_type == "lhex" and to_type == "mib": - # Convert from lhex to decimal - dec_val = self._convert_value(val, "lhex", "decimal") - # Convert to MiB - mib_val = round(int(dec_val) / (1024 * 1024), 4) + # Convert from lhex to decimal: + dec_val = self._convert_value(val, "lhex32", "decimal") + # Convert to MiB (using abs to ensure we always get a positive value): + mib_val = round(abs(int(dec_val)) / (1024 ** 2), 4) return await ctx.send(f"{mib_val} MiB") - else: - return await ctx.send(usage) except Exception: - return await ctx.send( - Nullify.escape_all("I couldn't make that conversion!")) \ No newline at end of file + return await ctx.send("I couldn't make that conversion!")