Skip to content

Commit

Permalink
Merge pull request #209 from shahab00x/patch-1
Browse files Browse the repository at this point in the history
allow more than one codeblock
  • Loading branch information
Zero6992 authored Mar 1, 2023
2 parents 78a5dfc + bc9bceb commit 74c4ebe
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -220,4 +220,4 @@ async def on_message(message):

TOKEN = os.getenv("DISCORD_BOT_TOKEN")

client.run(TOKEN)
client.run(TOKEN)

0 comments on commit 74c4ebe

Please sign in to comment.