Discord is a popular communication platform initially developed for gamers but now other communities use it too. It allows users to create and join servers, like virtual meeting places where people can chat, voice call, and share media with one another.
Let us make our discord bot play audio from YouTube.
To make your Discord bot play YouTube audio, create a new Discord bot application, connect it to your server, and write Python code using the discord module to handle events. Set up Privileged Gateway Intents, add “Connect” and “Speak” permissions, and use the bot’s token within the Python code. Run the code, and then input “!play youtube_URL” in the text channel to have the bot play the audio.
Setting up Your Discord Bot and Connecting to a Server
Create a new Discord bot application and connect it to a server
Open Discord Developer Portal (https://discord.com/developers/applications/)

- Click on “New Application,” give it a name, and click “Create”

Navigate to the “Bot” tab, click “Add Bot,” and create a Bot User

Enable desired Privileged Gateway Intents, such as SERVER MEMBERS INTENT and MESSAGE CONTENT INTENT

CONNECT: This permission allows your bot to connect to a voice channel.
SPEAK: This permission allows your bot to speak in a voice channel.

Save changes, reset the bot token, and copy it (for use in Python code)


Add bot to server by going to OAuth2 tab, selecting “bot” under scope, and choosing “Connect” and “Speak” permissions

Scroll down to select permissions Connect
and Speak

Use generated URL to connect the bot to your server

To connect the bot we paste the above URL in the browser where we open our discord and select the server
Below is a video of the entire steps mentioned above.
Do not forget to reset the token and copy it somewhere as we will be using it later. After the bot is connected to server you can find the bot at left side bar.
Python Code for Discord Bot
import discord
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'Logged in as {client.user} (ID: {client.user.id})')
@client.event
async def on_message(message):
if message.content == '!hello':
await message.channel.send(f'Hello, {message.author.mention}!')
client.run('YOUR_TOKEN')
print("Bot is connected to server")
This code imports the discord
module we then create a Client
instance with the intents
enabled.
Intents enable the bot to receive certain events such as on_member_join
and on_member_remove
events. We set intents.members
to True
as to enable members
intent since its disable by default.
Then we define two handlers the first one being on_read()
which is triggered when the bot has successfully connected to discord and is ready to receive events.It prints a message to console to alert that the bot has logged in and its user ID.
The second event handler,on_message()
is triggered when a message is sent in any channel that the bot has access to.In this case,it check if the message content is equal to “!hello” if so the bot sends message back to the same channel and greet the user who send it .
At last to connect the bot to server we call the client.run()
method with bot’s token as parameter .(Input the token which you copied earlier here)
Running the Discord Bot and Playing YouTube Audio

Replace ‘YOUR_TOKEN’ with the copied token and run the Python code. Type !play youtube_URL in the server text channel, and the bot will play the YouTube audio



Summary
In this tutorial, we’ve learned how to create a Discord bot, connect it to a server, and make it play audio from YouTube. With this foundation, you can now explore other possibilities and additional features for your Discord bot. What other capabilities will you add to enhance your bot’s functionality?
I combined the below mentioned references in this article , go through if any doubt: