Build a Discord Bot in Python That Plays Music and Send GIFs
With the full source code so you can build one yourself too
In this tutorial, we'll make a discord bot that can play music in the voice channels. Discord is an instant messaging and digital distribution platform designed for creating communities. Users can easily enter chat rooms, initiate video calls, and create multiple groups for messaging friends.
We'll skip the basics and jump straight over to the music playing part. Check out the following Medium article to catch up on the basics of setting up your bot:
The final result, our bot playing music on the voice channel, will look like this:
One note before we dive in: remember to allow Administrator permissions for the bot.
Part 1: Importing all the libraries
First, create a virtual environment and install the requirements.
First let's set up the .env
file for our project. Create a .env
file so that we can separate the environment configuration variables (these are variables whose values are set outside the program) from the main code.
Then import all the needed libraries in the main file app.py
:
The module youtube_dl
is an open-source download manager for video and audio content from YouTube and other video hosting websites.
Now we need to set intents for our bot. Intents allow a bot to subscribe to specific buckets of events, allowing developers to choose which events the bot listens to and to which it doesn't. For example, sometimes we want the bot to listen to only messages and nothing else.
Part 3: Adding bot commands
Now let's add the join()
method to tell the bot to join the voice channel and the leave()
method to tell the bot to disconnect:
Here we first check if the user who wants to play music has already joined the voice channel or not. If not, we tell the user to join first.
Awesome! Give yourself a pat on the back if you've reached this far! You're doing great.
Next, we'll add the following methods:
- play()
- pause()
- resume()
- stop()
At this point, we need to have the ffmpeg binary in the base directory. It can be downloaded from https://ffmpeg.org/. In this case, I used the exe since I'm using a Windows machine.
Part 4: Deploying the bot locally
Add the final piece of code to start the bot and it's done:
To deploy the bot locally, activate the virtual environment and run the app.py file.
(venv1) C:\Github\Discord-Bot>python app.py
Bonus: send gifs on start-up and print server details
In this bonus section, we will set up our bot to listen to events such as start-up.
This example sends a previously downloaded GIF image to the text channel when the bot is activated.
To print server details such as owner name, number of users, server id we can add a bot command 'where_am_i'.
You can view and clone the complete results from my Discord-Bot GitHub Repository.
That's it! Thank you for reading. Don't hesitate to leave a reply or ask your questions in the comments section.
Source: https://medium.com/pythonland/build-a-discord-bot-in-python-that-plays-music-and-send-gifs-856385e605a1
Posted by: svetlanakounkele0193759.blogspot.com