Код: Выделить всё
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}\n\n**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}")
Подробнее здесь: https://stackoverflow.com/questions/790 ... -form-body