From bc9bcebc3d06c0f0488a8d6276b45eb70589f089 Mon Sep 17 00:00:00 2001 From: shahab Date: Tue, 28 Feb 2023 22:07:45 +0330 Subject: [PATCH] allow more than one codeblock previously if a response had more than one code block it would not display in discord. Now it's fixed. --- src/bot.py | 74 +++++++++++++++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/bot.py b/src/bot.py index 722f4fcd..1118d865 100644 --- a/src/bot.py +++ b/src/bot.py @@ -29,50 +29,50 @@ async def send_message(message, user_message): response = '> **' + user_message + '** - <@' + \ str(author) + '> \n\n' response = f"{response}{await responses.handle_response(user_message)}" - if len(response) > 1900: + char_limit = 1900 + if len(response) > char_limit: # Split the response into smaller chunks of no more than 1900 characters each(Discord limit is 2000 per chunk) if "```" in response: # Split the response if the code block exists + parts = response.split("```") - # Send the first message - if isReplyAll: - await message.channel.send(parts[0]) - else: - await message.followup.send(parts[0]) - # Send the code block in a seperate message - code_block = parts[1].split("\n") - formatted_code_block = "" - for line in code_block: - while len(line) > 1900: - # Split the line at the 50th character - formatted_code_block += line[:1900] + "\n" - line = line[1900:] - formatted_code_block += line + "\n" # Add the line and seperate with new line - - # Send the code block in a separate message - if (len(formatted_code_block) > 2000): - code_block_chunks = [formatted_code_block[i:i+1900] - for i in range(0, len(formatted_code_block), 1900)] - for chunk in code_block_chunks: + + for i in range(0, len(parts)): + if i%2 == 0: # indices that are even are not code blocks if isReplyAll: - await message.channel.send("```" + chunk + "```") + await message.channel.send(parts[i]) else: - await message.followup.send("```" + chunk + "```") - else: - if isReplyAll: - await message.channel.send("```" + formatted_code_block + "```") - else: - await message.followup.send("```" + formatted_code_block + "```") - # Send the remaining of the response in another message + await message.followup.send(parts[i]) + + # Send the code block in a seperate message + else: # Odd-numbered parts are code blocks + code_block = parts[i].split("\n") + formatted_code_block = "" + for line in code_block: + while len(line) > char_limit: + # Split the line at the 50th character + formatted_code_block += line[:char_limit] + "\n" + line = line[char_limit:] + formatted_code_block += line + "\n" # Add the line and seperate with new line + + # Send the code block in a separate message + if (len(formatted_code_block) > char_limit+100): + code_block_chunks = [formatted_code_block[i:i+char_limit] + for i in range(0, len(formatted_code_block), char_limit)] + for chunk in code_block_chunks: + if isReplyAll: + await message.channel.send("```" + chunk + "```") + else: + await message.followup.send("```" + chunk + "```") + else: + if isReplyAll: + await message.channel.send("```" + formatted_code_block + "```") + else: + await message.followup.send("```" + formatted_code_block + "```") - if len(parts) >= 3: - if isReplyAll: - await message.channel.send(parts[2]) - else: - await message.followup.send(parts[2]) else: - response_chunks = [response[i:i+1900] - for i in range(0, len(response), 1900)] + response_chunks = [response[i:i+char_limit] + for i in range(0, len(response), char_limit)] for chunk in response_chunks: if isReplyAll: await message.channel.send(chunk) @@ -220,4 +220,4 @@ async def on_message(message): TOKEN = os.getenv("DISCORD_BOT_TOKEN") - client.run(TOKEN) \ No newline at end of file + client.run(TOKEN)