Discover how to interact with Discord using Python for tasks like sending, reading, updating, and deleting messages. We also guide you through enabling developer mode, getting your Discord token, and finding channel IDs.
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.
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.
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.
I am a freelance technical writer based in India. I write quality user guides and blog posts for software development startups. I have worked with more than 10 startups across the globe.