aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArjun Satarkar <me@arjunsatarkar.net>2024-03-20 03:07:54 +0000
committerArjun Satarkar <me@arjunsatarkar.net>2024-03-20 03:07:54 +0000
commit16aeaa50db79e3c17194128a93d22370bf5daa33 (patch)
treecbcef052ca3afb49274513446514438fa1ed1604
parent92a0d3eb4fd919ff9ed33b81d379ccb77af43026 (diff)
downloadaps-cogs-16aeaa50db79e3c17194128a93d22370bf5daa33.tar
aps-cogs-16aeaa50db79e3c17194128a93d22370bf5daa33.tar.gz
aps-cogs-16aeaa50db79e3c17194128a93d22370bf5daa33.zip
markov: avoid float division in uint_to_bytes
It seems as though the float division could cause an issue where the actual value should be an integer but the float representation is slightly higher than that, causing the addition of an extra unnecessary byte. Although this works fine as long as the same function is used, it could cause problems if seemingly inconsequential details change. I switch to using divmod and checking for the presence of a remainder. This should ensure use of the exact minimum required number of bytes in the blob.
-rw-r--r--markov/markov.py31
-rw-r--r--simplestarboard/__init__.py5
-rw-r--r--starboard/__init__.py5
-rw-r--r--starboard/info.json (renamed from simplestarboard/info.json)0
-rw-r--r--starboard/starboard.py (renamed from simplestarboard/starboard.py)10
5 files changed, 27 insertions, 24 deletions
diff --git a/markov/markov.py b/markov/markov.py
index e206800..73ac08e 100644
--- a/markov/markov.py
+++ b/markov/markov.py
@@ -151,13 +151,29 @@ class Markov(commands.Cog):
def uint_to_bytes(self, x: int):
if x < 0:
raise ValueError(f"x must be non-negative (got {x})")
- return x.to_bytes(math.ceil(x.bit_length() / 8), byteorder="big", signed=False)
+ byte_length, remainder = divmod(x.bit_length(), 8)
+ if remainder:
+ byte_length += 1
+ return x.to_bytes(byte_length, byteorder="big", signed=False)
def get_base_channel(self, channel_or_thread):
if isinstance(channel_or_thread, discord.Thread):
return channel_or_thread.parent
return channel_or_thread
+ def append_token(self, text, token):
+ # NOTE: if changing PUNCTUATION, also change the regex in process_message() with the corresponding note
+ PUNCTUATION = r".,!?/;()"
+ if token == "/":
+ text = text[:-1] + token
+ elif token == "(":
+ text += token
+ elif token in PUNCTUATION:
+ text = text[:-1] + token + " "
+ else:
+ text += token + " "
+ return text
+
@commands.group()
async def markov(self, _ctx):
"""
@@ -385,19 +401,6 @@ class Markov(commands.Cog):
await db.commit()
await ctx.reply("All markov data for this guild has been deleted.")
- def append_token(self, text, token):
- # NOTE: if changing PUNCTUATION, also change the regex in process_message() with the corresponding note
- PUNCTUATION = r".,!?/;()"
- if token == "/":
- text = text[:-1] + token
- elif token == "(":
- text += token
- elif token in PUNCTUATION:
- text = text[:-1] + token + " "
- else:
- text += token + " "
- return text
-
@markov.command()
async def generate(self, ctx, member: discord.Member | None):
if not await self.config.guild(ctx.guild).use_messages():
diff --git a/simplestarboard/__init__.py b/simplestarboard/__init__.py
deleted file mode 100644
index 26d4ba8..0000000
--- a/simplestarboard/__init__.py
+++ /dev/null
@@ -1,5 +0,0 @@
-from .starboard import SimpleStarboard
-
-
-async def setup(bot):
- await bot.add_cog(SimpleStarboard(bot))
diff --git a/starboard/__init__.py b/starboard/__init__.py
new file mode 100644
index 0000000..eeb3d75
--- /dev/null
+++ b/starboard/__init__.py
@@ -0,0 +1,5 @@
+from .starboard import Starboard
+
+
+async def setup(bot):
+ await bot.add_cog(Starboard(bot))
diff --git a/simplestarboard/info.json b/starboard/info.json
index af11ade..af11ade 100644
--- a/simplestarboard/info.json
+++ b/starboard/info.json
diff --git a/simplestarboard/starboard.py b/starboard/starboard.py
index 3ae3c1c..751c7c3 100644
--- a/simplestarboard/starboard.py
+++ b/starboard/starboard.py
@@ -5,7 +5,7 @@ from redbot.core import Config
from redbot.core import commands
-class SimpleStarboard(commands.Cog):
+class Starboard(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.config = Config.get_conf(
@@ -22,10 +22,10 @@ class SimpleStarboard(commands.Cog):
return f"<:{emoji.name}:{emoji.id}>"
@commands.group()
- async def simplestarboard(self, _ctx):
+ async def starboard(self, _ctx):
pass
- @simplestarboard.command()
+ @starboard.command()
@commands.admin_or_permissions(manage_guild=True)
async def add(self, ctx, name: str, channel: discord.TextChannel, threshold: int):
if threshold < 1:
@@ -101,7 +101,7 @@ class SimpleStarboard(commands.Cog):
confirmation_text, allowed_mentions=discord.AllowedMentions.none()
)
- @simplestarboard.command()
+ @starboard.command()
@commands.admin_or_permissions(manage_guild=True)
async def remove(self, ctx, name: str):
async with self.config.guild(ctx.guild).starboards() as starboards:
@@ -114,7 +114,7 @@ class SimpleStarboard(commands.Cog):
else:
await ctx.reply("Removed that starboard.")
- @simplestarboard.command()
+ @starboard.command()
async def list(self, ctx):
starboards = await self.config.guild(ctx.guild).starboards()
list_text = "Name, Channel, Threshold, Reactions"