Discord-Messenger-CLI/send_discord_msg.py

51 lines
1.5 KiB
Python
Raw Normal View History

2024-01-02 21:39:21 -05:00
import sys
import argparse
import time
from discord_webhook import DiscordWebhook
from requests.exceptions import Timeout
def send_discord_webhook(
webhook_url, message, max_retries=3, retry_delay=5, ignore_errors=False
):
for attempt in range(max_retries):
try:
webhook = DiscordWebhook(url=webhook_url, content=message)
webhook.execute()
print("Webhook message sent successfully.")
sys.exit(0)
except Timeout as e:
print(f"Error sending webhook message: {e}")
if attempt < max_retries - 1:
print(f"Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
elif ignore_errors:
print("Ignoring errors. Exiting with success.")
sys.exit(0)
else:
print("Max retries reached. Exiting with failure.")
sys.exit(1)
def main():
parser = argparse.ArgumentParser(description="Send messages to Discord webhooks.")
parser.add_argument("webhook_url", help="Discord webhook URL")
parser.add_argument("message", help="Message to send")
parser.add_argument(
"-i",
"--ignore-errors",
action="store_true",
help="Ignore errors and exit with success",
)
args = parser.parse_args()
send_discord_webhook(
args.webhook_url, args.message, ignore_errors=args.ignore_errors
)
if __name__ == "__main__":
main()