From 74b3f5321267b0b156a36e621a9b023a132340ce Mon Sep 17 00:00:00 2001 From: Bildcraft1 Date: Thu, 6 Jun 2024 10:19:06 +0200 Subject: [PATCH] chore: Refactor message processing logic and split output into chunks Refactor the `process_message` function in `bot.py` to improve performance and handle longer messages. The function now appends extra characters to the base message to ensure it is always 4000 characters long. This change improves the processing of messages and prevents truncation. In `messageCreate.js`, split the output into chunks of 2000 characters using the `splitMessage` function. This allows sending each part as a separate message, ensuring that the output is not cut off. --- bot.py | 5 +++-- events/messageCreate.js | 31 ++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/bot.py b/bot.py index 2472bab..da3bfd9 100644 --- a/bot.py +++ b/bot.py @@ -1,8 +1,9 @@ import sys def process_message(message): - # Replace this with your actual processing logic - return f"Processed message: {message}" + base_message = f"Processed message: {message}" + extra_chars = 'a' * (4000 - len(base_message)) + return base_message + extra_chars if __name__ == "__main__": if len(sys.argv) > 1: diff --git a/events/messageCreate.js b/events/messageCreate.js index 31254ac..631515c 100644 --- a/events/messageCreate.js +++ b/events/messageCreate.js @@ -37,15 +37,36 @@ module.exports = { return message.reply("There was an error processing your request."); } - // Log the output from the Python script - logAction(`GPT: ${stdout.trim()}`, message.author.id, message.author.tag); + let output = stdout.trim(); - // Reply with the output from the Python script - message.reply(stdout.trim()); + // Log the output from the Python script + logAction(`GPT: ${output}`, message.author.id, message.author.tag); + + // Function to split the output into chunks of 2000 characters + function splitMessage(message, maxLength) { + const parts = []; + while (message.length > 0) { + if (message.length > maxLength) { + parts.push(message.slice(0, maxLength)); + message = message.slice(maxLength); + } else { + parts.push(message); + message = ''; + } + } + return parts; + } + + // Split the output if it's longer than 2000 characters + const maxMessageLength = 2000; + const messageParts = splitMessage(output, maxMessageLength); + + // Send each part as a separate message + messageParts.forEach(part => message.reply(part)); }); } else { message.reply("Not whitelisted"); } } }, -}; +} \ No newline at end of file