A Discord bot that plays music from YouTube videos in a voice channel. The bot supports basic music control commands like play, stop, pause, resume, and skip. It also manages an embed queue to display the current song and the next song in the queue.
- Play music from YouTube links.
- Pause, resume, and skip music.
- Display the current song and the next song using embeds.
- Clear all embeds when the stop command is initiated to keep the chat clean.
- Node.js (version 16 or higher)
- npm (comes with Node.js)
- A Discord bot token. You can get one by creating a bot on the Discord Developer Portal.
-
Clone the repository or download the source code.
-
Navigate to the project directory.
cd your-project-directory
-
Install the required dependencies.
npm install discord.js ytdl-core @discordjs/voice dotenv
-
Create a
.env
file in the root directory and add your Discord bot token and the command prefix.DISCORD_TOKEN=your-bot-token PREFIX=!
-
Run the bot.
node index.js
-
Invite the bot to your Discord server. You can use the OAuth2 URL generator on the Discord Developer Portal to get an invite link.
-
Use the following commands in your Discord server:
!play [YouTube URL]
- Add a song from YouTube to the queue and start playing if not already playing.!stop
- Stop the currently playing song and clear the queue.!pause
- Pause the currently playing song.!resume
- Resume the currently paused song.!skip
- Skip to the next song in the queue.!help
- Display the list of available commands.
-
!play [YouTube URL]
- Adds a song from YouTube to the queue and starts playing if not already playing.
-
!stop
- Stops the currently playing song and clears the queue. Deletes all embed messages to keep the chat clean.
-
!pause
- Pauses the currently playing song.
-
!resume
- Resumes the currently paused song.
-
!skip
- Skips to the next song in the queue.
-
!help
- Displays a list of available commands.
This is the main file that initializes the bot and handles all the commands.
-
Dependencies and Initialization:
- Requires necessary modules:
discord.js
,ytdl-core
,fs
,@discordjs/voice
, anddotenv
. - Creates a new
Discord.Client
with appropriate intents. - Creates an audio player using
createAudioPlayer
from@discordjs/voice
.
- Requires necessary modules:
-
Event Handlers:
player.on('error')
: Handles any errors that occur during audio playback.client.once('ready')
: Loads the queue from a JSON file and logs in the bot.client.on('messageCreate')
: Handles incoming messages and executes the appropriate commands.
-
Command Functions:
playSong(guildId, textChannel, retry)
: Plays the next song in the queue and updates the embeds.deleteLastEmbeds(guildId)
: Deletes the last "Now Playing", "Song Added to Queue", and "Up Next" embeds to keep the chat clean.
-
Commands:
!play [YouTube URL]
: Adds a song to the queue and starts playback.!stop
: Stops the music and clears the queue.!pause
: Pauses the currently playing song.!resume
: Resumes the currently paused song.!skip
: Skips to the next song in the queue.!help
: Displays the help message with available commands.
This project is licensed under the MIT License.