Ever thought of automating tasks on Discord or integrating it with your own application through API? In this tutorial, we'll show you how to interact with Discord using Python. You'll learn how to send messages, read them back, update them, and even delete them.
How to use the Discord API in Python
To work with the Discord API, first, create a server on your Discord account. Then, enable developer mode by clicking on the settings button in the bottom left corner -> Advanced -> Enable Developer Mode.
Now, to work with the Discord API, you also need a token and the channel ID (where you want to send the message or retrieve the message from). To get the channel ID, right-click on any channel (such as #general) and copy the channel ID.
For the token, follow these steps:
1. Open Discord in a browser and open developer mode by pressing Ctrl+Shift+I.
2. Go to Application -> Local Storage -> Click on https://discord.com/ -> Click on Filter.
3. Search for "token" in the filter and copy the token.
Now you have the token and channel ID. Let's send some messages using a Python POST request.
import requests
def message_post(token, channel_id, message):
url = f"https://discord.com/api/v9/channels/{channel_id}/messages"
headers = {
"Authorization": f"{token}",
}
data = {
"content": message
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
print("Message sent successfully.")
else:
print("Failed to send the message.")
print(response.text)
token = "YOUR DISCORD TOKEN"
channel_id = "CHANNEL ID"
message = "Hey, How are you?"
message_post(token, channel_id, message)
The code output is this:
The message has been sent successfully, as you can see above. Now, read/retrieve the messages from the channel using a Python GET request.
import requests
def read_message(channel_id, token):
url = f"https://discord.com/api/v9/channels/{channel_id}/messages"
header = {
"Authorization": f"{token}"
}
response = requests.get(url, headers=header)
if response.status_code == 200:
messages = response.json()
return messages
else:
print("Failed to fetch messages.")
print(response.text)
return None
token = "YOUR DISCORD TOKEN"
channel_id = "CHANNEL ID"
messages = read_message(channel_id, token)
if messages:
for message in messages:
print(f"{message['content']}")
else:
print("Failed to read messages.")
The above code will read all the messages that are in the channel.
Next, update the message using a Python PATCH request. To do this, you also need a message ID of the message that is to be updated. Go to the channel and right-click on the message to copy the message ID.
msg_id = response.json()
msg_id = f"{url}/{msg_id['id']}"
Use the requests.patch() function to make a PATCH request and pass the token, channel_id, message_id, and new message to be updated.
import requests
def update_message(token, channel_id, message_id, new_message):
url = f"https://discord.com/api/v9/channels/{channel_id}/messages/{message_id}"
headers = {
"Authorization": f"{token}",
}
data = {
"content": new_message
}
response = requests.patch(url, headers=headers, json=data)
if response.status_code == 200:
print(f"Message has been updated successfully.")
else:
print("Failed to edit the message.")
token = "YOUR DISCORD TOKEN"
channel_id = "CHANNEL ID"
message_id = "YOUR MESSAGE ID"
new_message = "Hey, this message has been updated using the PATCH request."
update_message(token, channel_id, message_id, new_message)
Here’s the code output:
Next, delete the message using the requests.delete() function.
import requests
def delete_message(token, channel_id, message_id):
url = f"https://discord.com/api/v9/channels/{channel_id}/messages/{message_id}"
headers = {
"Authorization": f"{token}",
}
response = requests.delete(url, headers=headers)
if response.status_code == 204:
print(f"Message has been deleted successfully.")
else:
print("Failed to delete the message.")
print(response.text)
token = "YOUR DISCORD TOKEN"
channel_id = "CHANNEL ID"
message_id = "YOUR MESSAGE ID"
delete_message(token, channel_id, message_id)
That's it. You've just learned how to interact with Discord API using Python. From setting up your server and enabling developer mode to managing tokens and channel IDs, you're now equipped to send, read, update, and even delete messages. We hope that this gives you lots of ideas for automating tasks or enhancing your Discord communities.
If you're into using Python, you might like to explore web scraping with Python or learn about Python and machine learning.