prettied up the output sent to discord

This commit is contained in:
2025-11-12 11:02:36 +01:00
parent 5292527a1c
commit 9b1f8aacab

View File

@ -96,21 +96,40 @@ def send_to_discord_webhook(data, webhook_url):
def format_batch(events, title): def format_batch(events, title):
content = f"**{title}**\n" content = f"**{title}**\n"
for item in events: for item in events:
content += f"**{item['nation']}**\nEvent: {item['event']}\nTid: {item['open_time']}\n---\n" content += (
# Discord message limit is 2000 characters f"> **{item['nation']}**\n"
if len(content) > 1900: f"> **Event:** {item['event']}\n"
content = content[:1900] + "... (truncated)" f"> **Tid:** {item['open_time']}\n"
f"> ————————————————\n"
)
return content return content
def send_in_batches(content, webhook_url):
max_len = 2000
lines = content.split('\n')
batch = ''
for line in lines:
if len(batch) + len(line) + 1 > max_len:
payload = {"content": batch}
try:
response = requests.post(webhook_url, json=payload)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
print(f"Discord webhook error: {e}\nPayload: {payload}")
batch = ''
batch += line + '\n'
if batch.strip():
payload = {"content": batch}
try:
response = requests.post(webhook_url, json=payload)
response.raise_for_status()
except requests.exceptions.HTTPError as e:
print(f"Discord webhook error: {e}\nPayload: {payload}")
# Do not send non-pub events to Discord # Do not send non-pub events to Discord
# Send pub events second, as normal message # Send pub events second, as normal message
if pub_events: if pub_events:
payload = {"content": format_batch(pub_events, "Pub-aktiviteter")} content = format_batch(pub_events, "Pub-aktiviteter")
try: send_in_batches(content, webhook_url)
response = requests.post(webhook_url, json=payload)
response.raise_for_status()
print("Sent pub batch to Discord.")
except requests.exceptions.HTTPError as e:
print(f"Discord webhook error: {e}\nPayload: {payload}")
def main(): def main():