From 7ea1f2c5e3e19829ce494dc62f337ff14a3c96c2 Mon Sep 17 00:00:00 2001 From: AllfatherHatt Date: Fri, 21 Feb 2025 18:33:45 +0100 Subject: [PATCH] Added a hopefully better way of handling Discord constraints --- reginaldCog/reginald.py | 50 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/reginaldCog/reginald.py b/reginaldCog/reginald.py index 18382cc..aecc1e7 100644 --- a/reginaldCog/reginald.py +++ b/reginaldCog/reginald.py @@ -133,7 +133,7 @@ class ReginaldCog(commands.Cog): # ✅ Store updated short-term memory back short_memory[channel_id] = memory - await ctx.send(response_text[:2000]) + await self.send_split_message(ctx, response_text) @@ -147,8 +147,6 @@ class ReginaldCog(commands.Cog): "Prioritize compression while keeping essential nuances intact." ) - - summary_text = "\n".join(f"{msg['user']}: {msg['content']}" for msg in messages) try: @@ -374,7 +372,7 @@ class ReginaldCog(commands.Cog): f"📝 **Summary:**\n```{selected_summary['summary']}```" ) - await self.send_long_message(ctx, formatted_summary, prefix="📜 ") + await self.send_long_message(ctx, formatted_summary) @commands.command(name="reginald_summaries", help="Lists available summaries for this channel.") async def list_mid_term_summaries(self, ctx): @@ -393,9 +391,9 @@ class ReginaldCog(commands.Cog): await ctx.send(f"📚 **Available Summaries:**\n{summary_list[:2000]}") - async def send_long_message(self, ctx, message, prefix=""): + async def send_long_message(self, ctx, message, prefix: str = ""): """Splits and sends a long message to avoid Discord's 2000-character limit.""" - chunk_size = 1990 # Leave some space for formatting + chunk_size = 1900 # Leave some space for formatting if prefix: prefix_length = len(prefix) chunk_size -= prefix_length @@ -404,6 +402,46 @@ class ReginaldCog(commands.Cog): chunk = message[i:i + chunk_size] await ctx.send(f"{prefix}{chunk}") + + + async def send_split_message(ctx, content: str, prefix: str = ""): + """ + A unified function to handle sending long messages on Discord, ensuring they don't exceed the 2,000-character limit. + + Parameters: + - ctx: Discord command context (for sending messages) + - content: The message content to send + - prefix: Optional prefix for each message part (e.g., "📜 Summary:") + """ + # Discord message character limit (allowing a safety buffer) + CHUNK_SIZE = 1900 # Slightly below 2000 to account for formatting/prefix + + if prefix: + CHUNK_SIZE -= len(prefix) # Adjust chunk size if a prefix is used + + # If the message is short enough, send it directly + if len(content) <= CHUNK_SIZE: + await ctx.send(f"{prefix}{content}") + return + + # Splitting the message into chunks + chunks = [] + while len(content) > 0: + # Find a good breaking point (preferably at a sentence or word break) + split_index = content.rfind("\n", 0, CHUNK_SIZE) + if split_index == -1: + split_index = content.rfind(" ", 0, CHUNK_SIZE) + if split_index == -1: + split_index = CHUNK_SIZE # Fallback to max chunk size + + # Extract chunk and trim remaining content + chunks.append(content[:split_index].strip()) + content = content[split_index:].strip() + + # Send chunks sequentially + for chunk in chunks: + await ctx.send(f"{prefix}{chunk}") + async def setup(bot): """✅ Correct async cog setup for Redbot""" await bot.add_cog(ReginaldCog(bot))