Skip to main content

Package for Alerts on Discord

Project description

DiscordAlerts Documentation

Table of Contents

Introduction

Welcome to the DiscordAlerts documentation. This document provides information on how to use the DiscordAlerts package.

Simple Usage

To implement an alert to one function, you need to call a DiscordAlert object and build this object with your Discord hook. Look at the example below:

from discord_alert.discord_alert import DiscordAlert
from alerts.alerts import alert

hook = DiscordAlert(hook="Your URL hook here")

@alert(hook=hook, custom_msg="You can add a custom message here")
def add(x, y):
    return x + y

Sub-modules

alerts

alert(hook: DiscordAlert, custom_msg='')

Decorator function for error handling and sending alerts via webhook.

This function takes a DiscordAlert object and an optional custom message as arguments. It returns a decorator function that wraps another function and handles any exceptions that occur during its execution. If an exception occurs, it sends an alert via a webhook using the DiscordAlert object.

Args:

  • hook: An instance of the DiscordAlert class representing a webhook URL.
  • custom_msg: An optional custom message to be included in the alert.

Returns:

A callable wrapper function.

Example Usage:

@alert(hook=DiscordAlert(hook="webhook_url"), custom_msg="Custom message: ")
def my_function():
    # Code to be executed
    pass

my_function()

Module discord_alert

Class DiscordAlert

This class represents a Discord alert and provides a method to send alert messages to a Discord webhook.

Example Usage

# Create a DiscordAlert object
alert = DiscordAlert(hook="Your Hook")

# Send an alert message
alert.send_alert("Alert Title", "Alert Message", 0x00ff00, "thumbnail_url")

Attributes

  • hook: The webhook URL for sending the alert message.

Method send_alert(title: str, message: str, color: int, thumbnail_url: str) -> bool

Sends an alert message to the Discord webhook.

Args:

  • title (str): The title of the alert message.
  • message (str): The message content of the alert.
  • color (int): The color of the alert message.
  • thumbnail_url (str): The URL of the thumbnail image for the alert.

Returns:

  • bool: True if the alert message was successfully sent, False otherwise.
def send_alert(
    self, title: str, message: str, color: int, thumbnail_url: str
) -> bool:
    """
    Sends an alert message to the Discord webhook.

    Args:
        title (str): The title of the alert message.
        message (str): The message content of the alert.
        color (int): The color of the alert message.
        thumbnail_url (str): The URL of the thumbnail image for the alert.

    Returns:
        bool: True if the alert message was successfully sent, False otherwise.
    """
    embeds = [
        {
            "title": title,
            "description": message,
            "color": color,
            "thumbnail": {"url": thumbnail_url},
        },
    ]
    try:
        webhook = DiscordWebhook(url=self.hook, embeds=embeds)
        webhook.execute()
        return True
    except Exception:
        return False

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

DiscordAlerts-0.1.1.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

DiscordAlerts-0.1.1-py3-none-any.whl (6.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page