Discord-Go-Music-Bot

This project is a simple-to-use Discord bot you can deploy almost anywhere to play music and audio within Discord servers.

There is a small amount of configuration needed to get it up and running, and once set up, there is little maintenance required to keep it operational.

Table of Contents

Features

The bot supports the following features:

more features to come as they are requested

Installation

Native vs Docker

You may choose to run the bot natively on your system or in a Docker container. The native installation is simpler and easier to follow, as well as maintain, but the Docker installation provides a potentially easier-to-maintain system in the long run, especially if you are familiar with Docker and its commands.

Native

You must have the following programs/dependencies on your system:

On a Debian/Ubuntu-based system, you can install the required dependencies with:

sudo apt update
sudo apt install ffmpeg libopus0 libopus-dev libopusenc0 libopusfile-dev opus-tools golang make yt-dlp git

# You may need to install `yt-dlp` manually since YouTube sometimes interferes with yt-dlp's video download process.
# You may also opt for installing Go manually if your version from apt is too old.
# See github.com/yt-dlp/yt-dlp/wiki/Installation for yt-dlp
# and go.dev/doc/install for Go

After installing these, it’s a good idea to check yt-dlp is working, since it can be error prone, by testing it, e.g.

yt-dlp https://www.youtube.com/watch?v=dQw4w9WgXcQ

Then you need to clone the code repository and compile the bot. You can do this with the following commands:

git clone https://github.com/H-Edward/Discord-Go-Music-Bot
cd Discord-Go-Music-Bot
make

You should now see a new file called music-bot; this is the executable file.

Now you must make sure you have your bot token from Discord’s developer portal, which can be found at https://discord.com/developers/applications. Then click on your application, then click on Bot in the sidebar, then copy the token from this page (you may need to click Reset Token).

The token should be written to the file .env in the same directory as the music-bot executable.

This can be achieved by doing the following:

mv .env.sample .env 
nano .env

You will now see

DISCORD_BOT_TOKEN=

Paste in your token after the = sign and save the file.

You now have the bot ready and can run it using the following command:

./music-bot

## you may want to run it in the background using something like the screen command so the bot doesn't stop when you close the terminal
# sudo apt install screen
# screen -S music-bot
# ./music-bot
# then to detach from the screen session, press Ctrl+A then D

# to reattach to the screen session, run
# screen -r music-bot

To kill the bot safely, you can press Ctrl+C in the terminal where the bot is running.

Updating with Native

If you want to update the bot to the latest version, you can do so by running the following commands:

git pull origin main
make

Then you may run the bot again with:

./music-bot

Docker

First, you must have Docker installed on your system. You can find instructions for installing Docker at https://docs.docker.com/get-docker/.

Then you must clone the repository and change into the directory:

git clone https://github.com/H-Edward/Discord-Go-Music-Bot
cd Discord-Go-Music-Bot

Now you must make sure you have your bot token from Discord’s developer portal, which can be found at https://discord.com/developers/applications.

Then click on your application, then click on Bot in the sidebar, then copy the token from this page (you may need to click Reset Token).

cp .env.sample .env 
nano .env

You will now see

DISCORD_BOT_TOKEN=

Paste in your token after the = sign and save the file.

Then to build the bot in Docker:

make docker-network-create
make docker-build

and to deploy the bot, you can run:

make docker-run
## This command has additional set options for security and to prevent resource hogging

If you would like to use Docker but are unfamiliar, the Makefile has some additional commands to help manage the bot.

Updating with Docker

If you want to update the bot to the latest version, you can do so by running the following commands:

make docker-stop
make docker-rm 
git pull origin main
make docker-build

Then you may run the bot again with:

make docker-run

Alternatively,

git pull origin main
make docker-refresh-build

Configuration and Setup

You can set the following environment variables in the .env file to configure the bot:

DISCORD_BOT_TOKEN

Your Discord bot token (required)

UNKNOWN_COMMANDS

What the bot should do when faced with an unknown command. Without an explicit setting, the default is ignore.

Options are:

Ignore - If set to ignore, the bot will treat the command as if its just another message

Error - If set to error, the bot will respond with an error message if an unknown command is used

Help - If set to help, the bot will respond with a help message if an unknown command is used (same as !help command)

DISABLED_COMMANDS

A list of commands that should be disabled and not respond to user input.

This is a comma-separated list of command names, e.g.

DISABLED_COMMANDS=nuke,oss

Usage within Discord

First, you can test the bot is working by messaging:

!ping

as well as the slash command (to make sure they are registered)

/ping 

This should return a message saying Pong if the bot is running correctly.

The bot supports the usage of both old style commands with a simple ! before each command, as well as the more involved slash commands. Both can be used interchangeably with only minor differences in usage and responses.

Here are some other command examples for your reference:

Add a video to the queue by URL
/play https://www.youtube.com/watch?v=dQw4w9WgXcQ

Search for a video and add it to the queue
/search Rick Astley Never Gonna Give You Up

/pause

/resume

/volume 50

/volume 200

/volume 100

Show the current volume for the guild
/currentvolume

Skip the current video playing
/skip

Stop all playback and clear the queue
/stop

Show the current queue of videos excluding the current video
/queue

Delete the last 50 messages in the channel
/nuke 50

See the bot's uptime (requires server admin)
/uptime

See the bot's version (requires server admin)
/version

All the other commands can be seen by messaging:

/help

This will return a message with all the commands and their usage.

Technical Details

The bot is written mainly using the DiscordGo, with others being listed in go.mod.

The bot uses yt-dlp to download Youtube video and then uses ffmpeg to convert the video to audio and stream it to Discord.

Some OOP principles are in use, and if anyone would like to improve this aspect of the bot, please feel free to submit a pull request.

Much of the bots functionality relies on a Context struct, which is passed to each command handler and contains all the information needed to process the command.

Contributing

If you would like to contribute there are many ways you can help:

License

This project is licensed under the GNU General Public License v3.0 (GPL-3.0). You can find the full license text in the LICENSE file in the root of the repository.