Help me, ChatGPT, you are my only hope
This commit is contained in:
parent
994ff11655
commit
7663550108
@ -628,44 +628,38 @@ class ReginaldCog(commands.Cog):
|
|||||||
|
|
||||||
async def send_split_message(self, ctx, content: str, prefix: str = ""):
|
async def send_split_message(self, 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.
|
Sends a long message to Discord while ensuring it does not exceed the 2000-character limit.
|
||||||
|
This function prevents awkward mid-word or unnecessary extra message breaks.
|
||||||
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 # Keep buffer for formatting/safety
|
||||||
CHUNK_SIZE = 1900 # Slightly below 2000 to account for formatting/prefix
|
|
||||||
|
|
||||||
if prefix:
|
if prefix:
|
||||||
CHUNK_SIZE -= len(prefix) # Adjust chunk size if a prefix is used
|
CHUNK_SIZE -= len(prefix) # Account for prefix length
|
||||||
|
|
||||||
# If the message is short enough, send it directly
|
# If the message is short enough, send it directly
|
||||||
if len(content) <= CHUNK_SIZE:
|
if len(content) <= CHUNK_SIZE:
|
||||||
await ctx.send(f"{prefix}{content}")
|
await ctx.send(f"{prefix}{content}")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# **Improved Chunking Logic**
|
||||||
|
while content:
|
||||||
|
# Try to split at a newline first (prefer sentence breaks)
|
||||||
|
split_index = content.rfind("\n", 0, CHUNK_SIZE)
|
||||||
|
|
||||||
#chunks = []
|
# If no newline, split at the last space (avoid word-breaking)
|
||||||
while len(content) > 0:
|
|
||||||
# Find the nearest valid break point within CHUNK_SIZE
|
|
||||||
split_index = content[:CHUNK_SIZE].rfind("\n")
|
|
||||||
|
|
||||||
# If no newline, fall back to the nearest space
|
|
||||||
if split_index == -1:
|
if split_index == -1:
|
||||||
split_index = content[:CHUNK_SIZE].rfind(" ")
|
split_index = content.rfind(" ", 0, CHUNK_SIZE)
|
||||||
|
|
||||||
# If still no valid break point, hard split at CHUNK_SIZE
|
# If still no break point found, force chunk size limit
|
||||||
if split_index == -1:
|
if split_index == -1:
|
||||||
split_index = CHUNK_SIZE
|
split_index = CHUNK_SIZE
|
||||||
|
|
||||||
# Extract the chunk and send it
|
# Extract the message chunk and send it
|
||||||
chunk = content[:split_index].strip()
|
chunk = content[:split_index].strip()
|
||||||
content = content[split_index:].strip()
|
content = content[split_index:].strip()
|
||||||
|
|
||||||
await ctx.send(f"{prefix}{chunk}") # Send the chunk
|
await ctx.send(f"{prefix}{chunk}")
|
||||||
|
|
||||||
async def setup(bot):
|
async def setup(bot):
|
||||||
"""✅ Correct async cog setup for Redbot"""
|
"""✅ Correct async cog setup for Redbot"""
|
||||||
await bot.add_cog(ReginaldCog(bot))
|
await bot.add_cog(ReginaldCog(bot))
|
||||||
Loading…
x
Reference in New Issue
Block a user