I’m trying to link my google spreadsheet to a discord forum threads and I have been getting into these errors saying that its an invalid form body for exceeding the character limit of 2000 or 4000 and if I change that, it’ll say that it cannot send an empty message. I’ve gotten it to send out the first message but I’m running into the issue of when it gets to the second message if it needs to.
async def post_to_discord(self, title, content):
"""Post the content to the forum channel on Discord."""
forum_channel = self.bot.get_channel(self.forum_channel_id)
if not forum_channel:
print(f"Error: Channel {self.forum_channel_id} not found.")
return
# Ensure that the title and content are not empty
if not title or not content.strip():
print(f"Error: Title or content for entry '{title}' is empty.")
return
try:
# Prepare the formatted content with the title at the top
thread_content = f"**Title:** {title}nn**Content:**n{content.strip()}"
# Create a thread with the initial message in the forum channel
thread = await forum_channel.create_thread(
name=f"New Entry - {title}",
content=thread_content, # The first post in the forum thread
auto_archive_duration=1440 # 24 hours
)
# Handle splitting the content if it exceeds 2000 characters
max_message_length = 1990
split_messages = [thread_content[i:i + max_message_length]
for i in range(0, len(thread_content), max_message_length)]
# Send additional messages in the same thread if necessary
for message in split_messages[1:]:
if message.strip(): # Ensure the message is not empty
print("Sending additional message to the thread...")
await thread.send(message.strip())
print("Additional message sent successfully.")
print(f"New entry '{title}' posted successfully.")
except Exception as e:
print(f"Error occurred while posting to Discord: {e}")
What it’s meant to be doing is to send the initial post of 2000 characters and if it goes over that to split the message into multiple ones within the thread. Within this order of post,message,message or even post, message or just the post depending on how many characters there is. In the photo you can see the log of what the bot is trying to do.