Aswin Semiya

Business Analyst

IT Consultant

Data Analyst

Aswin Semiya

Business Analyst

IT Consultant

Data Analyst

Blog Post

A simple Music Bot for Discord

September 2, 2024 Side Projects
A simple Music Bot for Discord

If you’ve ever spent time on Discord, you know how much more fun a server can be with the right music playing in the background. That was the inspiration behind my latest project—a Discord bot that brings together the best of both worlds, playing music from YouTube and Spotify. I’m excited to share how I built this bot, the challenges I faced, and how you can host your own.

The Idea Behind the Music Bot

Music has this magical way of bringing people together, especially in online communities like Discord. Whether it’s a gaming session, a study group, or just a casual hangout, having a shared playlist or background music can really set the vibe. I wanted to create something that would let people seamlessly queue up their favourite tracks, no matter if they were on YouTube or Spotify, and have them play right in their Discord server.

The YouTube Music Bot Ban: A Little Backstory

Before diving into the technical aspects, it’s worth mentioning a significant event in the Discord music bot scene: the banning of popular YouTube music bots like Groovy and Rythm. These bots were beloved by many Discord communities for their ability to stream YouTube music directly into voice channels. However, due to copyright issues and pressure from YouTube, these bots were taken down, leaving a void for those who relied on them for their musical fix.

This ban highlighted the challenges of integrating third-party content into platforms like Discord. It was a wake-up call for developers who had to rethink how they could provide similar functionality without running afoul of content rights holders. For me, this event was a catalyst to explore alternative ways of bringing music into Discord, leading to the development of my bot that can handle both YouTube and Spotify streams.

Crafting the Bot: The Technical Journey

At the heart of this bot is a set of Python libraries that made everything possible. Here’s a peek into what went on under the hood:

  • discord.py: This is the core library that lets the bot interact with Discord. It’s what makes the bot respond to commands, join voice channels, and interact with users.
  • yt-dlp: A tool for downloading YouTube videos. But in this case, I’m using it to grab just the audio, which is then played in the Discord channel.
  • youtube-search-python: This makes it easy for the bot to search for music on YouTube based on user commands.
  • spotipy: Handling Spotify was a bit trickier, but spotipy came to the rescue. It connects the bot to the Spotify API, allowing it to fetch and play tracks directly from Spotify.
  • PyNaCl: Discord voice channels require audio to be processed in a certain way, and PyNaCl handles that behind the scenes.
  • asyncio: This library is crucial for making sure the bot can handle multiple tasks simultaneously—like playing music while still listening for new commands.

These tools work together to create a bot that’s responsive, versatile, and most importantly, fun to use.

Hosting Your Own Discord Music Bot

Interested in setting up your own bot? Here’s how you can do it:

  1. Clone the Repository: Start by grabbing the code from my GitHub repository. Make sure you have Python installed on your machine.
  2. Install the Dependencies: The bot relies on several Python libraries, which you can install using the requirements.txt file. Simply run: pip install -r requirements.txt
  3. Set Up Your Bot on Discord: You’ll need to create a bot account on the Discord Developer Portal and add it to your server. Once you have the bot token, you can plug it into the code.
  4. Spotify Integration: If you want the bot to play music from Spotify, you’ll need to set up a Spotify Developer account and create an application. This will give you the necessary credentials to connect the bot with Spotify’s API.
  5. Run the Bot: With everything set up, you can run the bot by simply executing the main python main.py
  6. Deploy the Bot: To keep the bot running 24/7, consider hosting it on a cloud service like Heroku, AWS, or even on your own VPS.

Technical Challenges and Solutions

Building this bot wasn’t without its challenges. One of the trickiest parts was managing the different audio formats from YouTube and Spotify. YouTube provides audio in formats that need to be processed differently compared to Spotify streams. Ensuring smooth playback without lag or distortion required careful handling of these audio streams.

Another challenge was making sure the bot remained responsive while streaming music. Discord bots, especially those handling audio, need to juggle multiple tasks at once. By using asynchronous programming (thanks to asyncio), I was able to keep the bot responsive even when it was busy downloading or playing music.

Making Music More Accessible

In the end, this project was all about making music more accessible and enjoyable on Discord. Whether you’re jamming out with friends or just need some background tunes while you chat, this bot is designed to make it easy to queue up your favorite tracks from two of the biggest music platforms out there.

If you’re curious to see the bot in action or want to tinker with the code yourself, check out the GitHub repository. I hope this inspires you to build your own bots or take this one and make it even better. After all, the best projects are the ones that keep evolving with the community.


This journey taught me a lot about integrating APIs, handling asynchronous tasks, and most importantly, the joy of sharing music with others. Whether you’re a seasoned developer or just starting out, there’s something incredibly satisfying about seeing your bot come to life in a real-world setting like Discord. Happy coding!

Tags:
Write a comment