5.3 KiB
Discord Music Bot (TypeScript)
Discord music bot template written in TypeScript using discord.js and shoukaku, with Lavalink support.
Features
- Slash commands (e.g.,
/ping,/join,/play,/leave) shoukakuintegration for robust Lavalink audio playback- Modular command and event handlers written in TypeScript
- Basic Docker support (
Dockerfile,docker-compose.yml)
Prerequisites
- Node.js (>=16 recommended, check
package.jsonfor specific engine requirements) - pnpm (recommended) or npm
- TypeScript (
typescriptpackage, usually installed as a dev dependency) - A Discord application with bot token and client ID
- A running Lavalink server
Setup
-
Clone the repository:
git clone <repository_url> cd discord-music-bot -
Install dependencies:
pnpm install -
Configure Environment: Copy
.env.exampleto.envand fill in your credentials:# Discord Bot Token (Required) DISCORD_TOKEN=your_discord_bot_token # Discord Application Client ID (Required for command deployment) CLIENT_ID=your_discord_application_id # Discord Guild ID (Optional, for deploying commands to a specific test server) # GUILD_ID=your_guild_id_here # Lavalink Configuration (Required) LAVALINK_HOST=lavalink # Or 127.0.0.1 if running locally without Docker Compose LAVALINK_PORT=2333 LAVALINK_PASSWORD=your_lavalink_password # LAVALINK_SECURE=false # Set to true if Lavalink uses SSL/WSS # Logging Level (Optional, defaults typically to 'info') # LOG_LEVEL=info # YouTube OAuth Token (Optional, needed for YouTube Music via specific plugins) # See note below on how to obtain this. YOUTUBE_OAUTH_TOKEN=your_youtube_oauth_token_hereNote on YouTube OAuth Token: The
YOUTUBE_OAUTH_TOKENis required by some Lavalink plugins (like theyoutube-pluginpotentially used here) to access YouTube Music tracks directly. Obtaining this involves:- Go to the Google Cloud Console.
- Create a new project or select an existing one.
- Navigate to "APIs & Services" -> "Credentials".
- Click "Create Credentials" -> "OAuth client ID".
- Select Application type: "TVs and Limited Input devices".
- Give it a name (e.g., "Lavalink YouTube Music Access").
- Click "Create". You'll get a Client ID and Client Secret (you likely won't need the secret directly for the token flow).
- Follow the on-screen instructions or Google's documentation for the "OAuth 2.0 for TV and Limited Input devices" flow. This usually involves visiting a specific URL with your client ID, getting a user code, authorizing the application on a separate device/browser logged into your Google account, and then exchanging the device code for a refresh token.
- Paste the obtained refresh token as the value for
YOUTUBE_OAUTH_TOKENin your.envfile.
-
Build TypeScript (if needed): Many setups use
ts-nodefor development, but for production, you might need to compile:pnpm build # Check package.json for the exact build script -
Register Slash Commands: Run the deployment script (ensure
CLIENT_IDandDISCORD_TOKENare set in.env).pnpm deploy # Check package.json for the exact deploy script (might be node/ts-node deploy-commands.ts) -
Start the Bot:
pnpm start # Check package.json for the exact start script (might run compiled JS or use ts-node)
Docker
A Dockerfile and docker-compose.yml are provided for containerized deployment.
- Ensure your
.envfile is configured correctly. - Build and run with Docker Compose:
docker-compose up --build -d # Use -d to run in detached mode - The
docker-compose.ymlincludes both the bot service and a Lavalink service.
Project Structure
.
├── src/ # Source code directory
│ ├── commands/ # Slash command modules (.ts)
│ ├── events/ # Discord.js and Shoukaku event handlers (.ts)
│ ├── structures/ # Custom structures or base classes (e.g., Shoukaku event handlers)
│ ├── utils/ # Utility functions (e.g., logger.ts)
│ └── index.ts # Main application entry point
├── plugins/ # Lavalink plugins (e.g., youtube-plugin-*.jar)
├── tests/ # Test files
├── .env.example # Example environment variables
├── .gitignore
├── application.yml # Lavalink server configuration
├── deploy-commands.ts # Script to register slash commands
├── docker-compose.yml # Docker Compose configuration for bot + Lavalink
├── Dockerfile # Dockerfile for building the bot image
├── LICENSE # Project License (GPLv3)
├── package.json # Node.js project manifest
├── tsconfig.json # TypeScript compiler options
└── update-plugin.sh # Script to update Lavalink plugins (example)
License
This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.