Introduction
Discord is a group-chat platform similar to Skype, TeamSpeak, or Slack that allows users to communicate simple text messages as well as rich messaging.
In this tutorial, we will learn to create a Discord bot and add it to our channel. How we can send text, image, video, audio, files through the bot, and further when we send any file to the channel how we can get it at the server-side in our computer.
Here we learn how we can get the following responses from the bot:
- Text
- Image
- Video
- Document
- Audio
Steps to create a Discord Bot
Give the name to your bot and then click on create.
Step 3: Go to the “Bot” tab and then click “Add Bot”. You will have to confirm by clicking “Yes, do it!”.
Step 4: After the bot is added to your channel copy the bot token.
Step 5: If your bot is not verified, you can toggle those intents below to access them.
Step 6: Invite the bot to your server.
Now scroll down and in the bot permission select the following permission.
Scroll down and copy the generated link.
Paste the link on your browser. After that select the server to which you want to add your bot, allow permission to the bot and add the bot to your server.
Step 7: Now install some libraries.
$ pip install discord
$ python3 -m pip install -U discord.py
Step 8: Get the text response from the bot.
Run the following code given below.
import discord
intents = discord.Intents.all()
client = discord.Client(command_prefix='!', intents=intents)
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hi'):
await message.channel.send('Hello!')
client.run('')
After running the code, the bot that we had built will come online to respond to our messages. When you type “hi” in the channel, the bot will respond with the “Hello!” message.
Step 9: Send an image through the bot.
The image could be sent through the bot in the form of a file. We need to give the file name to it and the file should be in the same directory where our code is.
It could be done by running the given code.
import discord
intents = discord.Intents.all()
client = discord.Client(command_prefix='!', intents=intents)
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
print("message-->", message)
if message.author == client.user:
return
if message.content.startswith('hi'):
await message.channel.send('Hello!')
if message.content.startswith('image'):
await message.channel.send(file=discord.File('download.jpg'))
client.run('')
After running the code when we type “image” in the channel the bot will respond with an image that we had given in the file.
Step 10: Get the video, audio, and file through the bot.
To get the video, audio, and file through the bot, we must share the file name in the file section for the file that we need to share through the bot in order to acquire the video, audio, and file through the bot. And also we need to keep that file in the same directory where our code is there.
Here is the code to send video, audio, and files through the bot.
import discord
intents = discord.Intents.all()
client = discord.Client(command_prefix='!', intents=intents)
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
print("message-->", message)
if message.author == client.user:
return
if message.content.startswith('hi'):
await message.channel.send('Hello!')
if message.content.startswith('image'):
await message.channel.send(file=discord.File('download.jpg'))
if message.content.startswith('video'):
await message.channel.send(file=discord.File('sample-mp4-file-small.mp4'))
if message.content.startswith('audio'):
await message.channel.send(file=discord.File('file_example_MP3_700KB.mp3'))
if message.content.startswith('file'):
await message.channel.send(file=discord.File('sample.pdf'))
client.run('')
After running the code when we type “video” in the channel the bot will respond with a video that we had given in the file and similarly for audio and file i.e .pdf, .docx.
Step 11: Upload an image and get it at the server-side.
First, we will upload the image to our discord channel and see what response are we getting at our terminal.
For the message, we will get the response in our terminal as given below.
Now, we will need to get the URL for the image that we had uploaded and then save that image on our computer.
We can get the URL for the image in message attachments.
We will print the “message.attachments” and see the response.
Here we will run a for loop and get the URL for the image that we uploaded.
After getting the URL we will try to save that image on our computer.
But when we upload an image to the bot we will get the same response as when we get the image through the bot. So we will need to only save the image that we upload to the bot.
For that, when we send the image through the bot and when we upload the image, we receive the message at our terminal. In that message, we will be getting the user id which is different and unique for all users. In that, we need to collect the user id for the bot. After that, we will specify that we will need to save the image on our server-side only if the image was not sent by the bot.
Here is the following code.
import discord
import requests
intents = discord.Intents.all()
client = discord.Client(command_prefix='!', intents=intents)
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
print("message-->", message)
print("message content-->", message.content)
print("message attachments-->", message.attachments)
print("message id", message.author.id)
a_id = message.author.id
if a_id != :
for x in message.attachments:
print("attachment-->",x.url)
d_url = requests.get(x.url)
file_name = x.url.split('/')[-1]
with open(file_name, "wb") as f:
f.write(d_url.content)
if message.author == client.user:
return
if message.content.startswith('hi'):
await message.channel.send('Hello!')
if message.content.startswith('image'):
await message.channel.send(file=discord.File('download.jpg'))
if message.content.startswith('video'):
await message.channel.send(file=discord.File('sample-mp4-file-small.mp4'))
if message.content.startswith('audio'):
await message.channel.send(file=discord.File('file_example_MP3_700KB.mp3'))
if message.content.startswith('file'):
await message.channel.send(file=discord.File('sample.pdf'))
client.run('')
Here we are getting the file name from the URL and saving the file with that file name.
“message.content” will print the text content that you type to the bot.
“message.author.id” will return the user id for the user.
Step 12: Upload other types of files and get them at the server-side.
First, we will try to upload the video and see what type of response we are getting at our terminal.
As seen in the below screenshot, we will be receiving a similar response as when we were uploading the image to the channel.
So we will not need to do any changes to the code. The file that we upload to our channel will be saved on the server-side of our computer.
So similarly, we can do it for audio and files to get it on our server-side. We will be getting the URL for the file that we upload in the “message.attachment” and getting it on the server-side.
From the above tutorial, we learned how we can get the image, video, audio and file through the bot and also how we can get this all at our server-side when we upload it and get it at our server-side in our computer.
We hope that by following the above tutorial, you will be able to create your own Discord Bot. If you have any queries when following it, please leave a comment below.
Similar Bot you can also build for Telegram and Slack. Our Telegram Bot and Slack Bot tutorials are helpful to build the chatbot.
1 Comment
Pragnakalp Techlabs
September 24, 2024This tutorial on creating a Discord bot using Python is fantastic! It offers clear examples and step-by-step instructions that make it easy to follow, even for beginners. A great resource for anyone looking to dive into bot Development
and enhance their Discord experience.