Discord API
Discord Webhook API allows you to send messages to text channels on a Discord server using a webhook URL. You can use webhooks to notify a server about various events, such as updates on a website, new forum posts, game results, and so on.
Here are some of the primary methods you can use with Discord webhooks:
~ Sending Messages - This is the primary method for sending messages to a channel.
~ Editing Messages - If a webhook has already sent a message, you can edit it.
~ Deleting Messages - Messages that have been sent can also be deleted.
Examples of Using Discord Webhook API in Python
To work with the Discord Webhook API in Python, you will need the requests library. Make sure it is installed in your system. If not, install it using pip:
Command Line / Terminal
pip install requests
Sending Messages
Python
import requests
import json
url = 'YOUR_WEBHOOK_URL'
# Dictionary of message data
data = {
"content": "Hello, Discord!", # Text of the message
"username": "New Bot" # Name under which the message will be sent
}
# Sending the message
response = requests.post(url, json=data)
print('Status Code:', response.status_code)
Editing Messages
To edit a message, you will need the message ID that you want to edit.
message_id = 'MESSAGE_ID'
edit_url = f'{url}/messages/{message_id}'
# New message content
edit_data = {
"content": "Hello, Discord! This is an edited message."
}
# Sending the edit request
response = requests.patch(edit_url, json=edit_data)
print('Status Code:', response.status_code)
Deleting Messages
delete_url = f'{url}/messages/{message_id}'
# Sending the delete request
response = requests.delete(delete_url)
print('Status Code:', response.status_code)
Important Points:
~ Each method has its specifics. For instance, when editing and deleting messages, you need to know the message ID.
~ Ensure you properly handle server responses, such as checking HTTP status codes of responses.
~ When using webhooks, it is crucial to protect your webhook URL, as anyone with access to it can send messages on your behalf.
These examples demonstrate basic usage of Discord webhooks in Python and can be expanded according to your needs.