Discord Bot with Python: Musical Knowledge Game

0 likes

Like @Steven Oh mentioned in his latest post Perfect Pitch Game In Swift UI , I was also assigned to create a music-related game for my music class. Since I was getting used to the Python discord library at the time, I decided to create a musical Discord bot using Spotify.

Discord is a communication platform which lets developers create their own bots - automated users that can perform certain actions like playing songs in voice channels, kicking people out of channels, and more.

I had this idea to test people's knowledge of music by combining Discord with Spotify. They can test their understanding of their own playlists - for example, do you know the name of all the songs in your playlist when you hear the music? - or test their understanding of today's trending playlists.

Game Modes

Make sure to join a voice channel when playing this game. It's a music game after all!

  1. Enter !featured to play a random playlist from Spotify's featured playlists.
    • Note: According to a Spotify admin,

      We recently changed the way some of our Spotify-curated playlists work, so that now they will be personalized according to individual users' tastes and listening habits. In practice, this means that the same Spotify playlist can contain different songs depending on the user who is viewing the playlist.

      Essentially, it means that songs from any playlists created by Spotify may be different for different users.
    • The game will randomly choose a playlist out of today's featured playlists. To move on to the next random featured playlist, type !nextplaylist or !n for short
  2. Enter !playlist spotify_uri to play a specific playlist
    • You can find the Spotify URI of a playlist here: Copy Spotify URI diagram
    • For example, !playlist spotify:playlist:37i9dQZF1DX30qK0lxZtlE will play a specific playlist
    • The !nextplaylist command will not be used for this game mode.

Types of Questions

Once you choose what playlist you want to play, the bot will randomly choose a song from that playlist and a question from one of the 3 choices below.

  1. What is the album name of X?
  2. Who is the artist of song X?
  3. What is the name of the song currently playing? (in the voice channel)

Sample Gameplay

Featured playlist Sample game play with featured playlist

Specific playlist Sample game play with specific playlist

The Code

The full code can be found on GitHub here

An overview of libraries used:

Challenge #1: Class Structure

The biggest challenge was determining which classes I need to create and what roles each one should be responsible for. In the end I settled with User , Playlist and Song classes with no hierarchies, but I am still not sure if this was the right choice.

Reflection

This is my third project with the Python Discord library, but I am still not 100% comfortable with asynchronous programming using Python's await and async keywords. In addition, I often have doubts as to the structure of my code, as I often find myself using global variables, something which is not considered best practice in Python. I read somewhere that this can be resolved by using Cogs , so I will give this a try on my next discord bot.


Leave a Comment
/200 Characters