Handle long text for discord hook
parent
9f40078223
commit
57a06d8abb
|
|
@ -468,6 +468,40 @@ def update_fs_version_mgr(build_info, config):
|
||||||
except ValueError:
|
except ValueError:
|
||||||
print("API response is not valid JSON")
|
print("API response is not valid JSON")
|
||||||
|
|
||||||
|
def split_discord_text_on_separator(discord_text, max_length=2000, separator=None):
|
||||||
|
if separator is None:
|
||||||
|
separator = "-" * 103
|
||||||
|
|
||||||
|
# Split the text on the separator lines
|
||||||
|
chunks = discord_text.split(separator + '\n')
|
||||||
|
|
||||||
|
messages = []
|
||||||
|
|
||||||
|
for index, chunk in enumerate(chunks):
|
||||||
|
# Strip leading/trailing whitespace
|
||||||
|
chunk = chunk.strip('\n')
|
||||||
|
|
||||||
|
# Re-add the separator to the end of each chunk except the last one
|
||||||
|
if index < len(chunks) - 1:
|
||||||
|
chunk += '\n' + separator
|
||||||
|
|
||||||
|
# Split the chunk further if it exceeds max_length
|
||||||
|
while len(chunk) > max_length:
|
||||||
|
# Find the last newline before max_length
|
||||||
|
split_point = chunk.rfind('\n', 0, max_length)
|
||||||
|
if split_point == -1 or split_point == 0:
|
||||||
|
# Can't find a newline, split at max_length
|
||||||
|
split_point = max_length
|
||||||
|
|
||||||
|
part = chunk[:split_point].rstrip('\n')
|
||||||
|
messages.append(part)
|
||||||
|
chunk = chunk[split_point:].lstrip('\n')
|
||||||
|
|
||||||
|
if chunk:
|
||||||
|
messages.append(chunk)
|
||||||
|
|
||||||
|
return messages
|
||||||
|
|
||||||
# parse args first arg optional -r (release) second arg mandatory string path_to_directory
|
# parse args first arg optional -r (release) second arg mandatory string path_to_directory
|
||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
|
|
@ -502,13 +536,12 @@ def main():
|
||||||
|
|
||||||
discord_text = create_discord_message(build_info, config)
|
discord_text = create_discord_message(build_info, config)
|
||||||
if args.webhook:
|
if args.webhook:
|
||||||
# Add the message to the webhook
|
messages = split_discord_text_on_separator(discord_text)
|
||||||
webhook.set_content(content=discord_text)
|
for message in messages:
|
||||||
# Send the webhook
|
webhook.set_content(content=message)
|
||||||
response = webhook.execute()
|
response = webhook.execute()
|
||||||
# Print the response
|
if not response.ok:
|
||||||
if not response.ok:
|
print(f"Webhook Error {response.status_code}: {response.text}")
|
||||||
print(f"Webhook Error {response.status_code}: {response.text}")
|
|
||||||
print(discord_text)
|
print(discord_text)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"An error occurred: {e}")
|
print(f"An error occurred: {e}")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue