Integration

Run the AudD Discord bot (deploy the official template)

Deploy AudD's official open-source Discord bot: clone the Go repo, configure your Discord and AudD tokens, run it, invite it to a server, and keep it up.

view .md audddiscord botdeploysong identification

AudD’s Discord bot recognizes music in audio and video files and in links posted on Discord. When someone replies to an audio/video file or a link with “!song”, it sends the file or the link to AudD’s Music Recognition API. The AudDMusic/DiscordBot repository contains the source code for the bot. The bot is written in Go. If you’d rather build the bot from scratch in Node with @audd/sdk, you can use the Build a Discord bot that identifies songs in voice channels recipe.

How the bot works

When the bot is started, it connects to the Discord gateway to get the new messages and commands. After that, it registers the slash commands (including the Recognize This Song menu action). Then, when someone asks for a song, it calls the RecognizeLongAudio() method, which sends the link to the file or the voice recording to the enterprise endpoint. AudD’s response will contain the title and artist of the song, the album, label and release date, the timecode, and the score (an enterprise feature, available on a Startup plan or higher, on request). The bot adds a link to the song’s lis.tn page (e.g., lis.tn/Warriors), which links to the streaming platforms.

Our servers split the audio from the file or the link into 12-second chunks. Every chunk the server recognizes counts as one request. The bot sets limit, the upper bound for the number of chunks the server will recognize. So for a file or a link, the bot recognizes up to 24 seconds and uses up to 2 requests: this parameter is limit=2 (limit=3, 36 seconds and up to 3 requests, for media.discordapp.net links). For a voice channel the bot records 12 seconds and sets limit=1, which is 1 request. To recognize all the songs in a DJ set or a mix, see Turn a DJ set or mix into a tracklist. For which endpoint does what, see Standard, enterprise, or streams: how to choose.

Voice channels

The bot’s Discord library connects to voice channels only with the xsalsa20_poly1305 encryption mode and doesn’t support DAVE, Discord’s end-to-end encryption for voice. According to Discord’s developer documentation, the voice gateway stopped accepting xsalsa20_poly1305 on November 18, 2024, and voice channels accept only end-to-end encrypted calls from March 1, 2026. So with the current code, the voice commands (/song-vc, !song @mention, /listen and !listen) can’t connect. Replying with !song to a file or a link, and Recognize This Song, don’t use voice and still work.

Prerequisites

You can get a token from our API Dashboard. The bot needs a paid plan: it sends files and links to AudD’s enterprise endpoint by URL, which works only on paid plans. The smallest paid plan, Indie, is $5 per month. The bot requires Go 1.17 or later and a C compiler such as gcc.

Walkthrough

Step 1: Create the Discord application

The bot user needs a Discord application. Go to the Discord Developer Portal and create a new application. Copy the Application ID from the General Information page, and keep the bot token for later. You can get your bot token by going to your app in the Dev Portal —> Bot —> Reset Token —> copy the token. For security reasons, you can only view your bot token once. Keep the token secret and treat it like a password.

To be able to identify music from the messages people reply to with !song, the bot needs the privileged Message Content intent. On the Bot page, scroll down to Privileged Gateway Intents and turn on Message Content Intent. Apps without the intent will receive empty values in fields that contain user-inputted content, so the bot never sees the file or the link.

Step 2: Clone and configure

Clone the repository to your server. The bot reads config.json from the working directory.

git clone https://github.com/AudDMusic/DiscordBot
cd DiscordBot
  1. Get a token from AudD and copy it to the AudDToken in config.json.
  2. Copy the bot token to the DiscordToken in config.json and the Application ID to DiscordAppID in config.json (the bot uses it to register the slash commands).
  3. Keep the CallbacksAddr at 127.0.0.1:4541 (if it’s empty, the bot tries to listen on port 80 and, without root, exits).
  4. Set the Triggers (the phrases the bot will react to) and the MaxReplyDepth (how many replies the bot follows to find a file or a link).
  5. Change the MinScore to 0 (it’s 65 in the repo’s config.json), unless your account has the match score, an enterprise feature available on a Startup plan or higher, on request.

A complete config.json can look like the following:

{
  "AudDToken": "your-api-token",
  "DiscordToken": "your-discord-bot-token",
  "DiscordAppID": "your-discord-application-id",
  "CallbacksAddr": "127.0.0.1:4541",
  "Triggers": ["whats the song", "whats this song", "!song", "!recognize", "!audd"],
  "MaxReplyDepth": 3,
  "MinScore": 0,
  "UncompressedLimit": 2
}

You can leave UncompressedLimit (the maximum number of songs posted as large embeds) set to 2 or change it to another number. Use SentryDSN to add a Sentry DSN to capture errors. SecretCallbackToken is only used for stream callbacks.

Step 3: Build and run

Build the bot from source:

go build -v ./...
./discordBot

The build prints some C compiler warnings from the Opus library (layeh.com/gopus); they are normal. After about 15 seconds you should see a line like Opened Discord session. Total servers: N where N is the number of servers the bot is on. If the process exits silently right after “Added the session to dSession”, the port in CallbacksAddr (4541) is unavailable. If you see panic: websocket: close 4004: Authentication failed., check the DiscordToken in config.json.

The bot only replies while it’s running, so leave that terminal open, or run it as a service (see Hosting it). Every time the bot starts, it deletes any other global commands of the application, so use an application made only for this bot.

Step 4: Invite the bot to a server

To add the bot to a server you are an admin on, open this URL with the Application ID you noted above instead of YOUR_CLIENT_ID:

https://discord.com/api/oauth2/authorize
  ?client_id=YOUR_CLIENT_ID
  &scope=bot+applications.commands
  &permissions=277026819136

bot adds the bot user to the server. With applications.commands, the slash commands and Recognize This Song will be available in the server. The permissions value lets the bot view channels, send messages (in threads too), embed links, attach files, read message history, add reactions, use external emojis, connect to voice channels and use application commands. The bot doesn’t need Speak: it joins voice channels muted. You can change the permissions in the Developer Portal, in the URL generator.

Step 5: Use it

To identify a song from an audio/video file or a link, reply to it with !song, or right-click on the message and pick Apps -> Recognize This Song. If the link has the time in the “t”, “time_continue”, or “start” parameters of the URL, the bot uses it as the skip_first_seconds: how many seconds of the file the server should skip at the beginning before starting the music recognition. You can also write the time in the message, like !song 1:30, or “at the end” to identify the end of the file.

With the current code, the voice channel commands below can’t connect (see Voice channels above). If you’re in a voice channel and someone is playing music there, use the /song-vc slash command and mention the user playing the music (!song @mention also works). The bot will record the sound for 12 seconds and then attempt to identify the song. If you want the bot to listen to a channel so it can immediately recognize the song from the last 12 seconds of audio, type !listen or use the /listen slash command, then /song-vc as usual. If you send /disconnect (or !disconnect), the bot will leave the voice channel.

The bot posts its help message to a channel with “bot” in its name when it joins a new server. You can change the help message in the help variable in main.go. If you type !here, the bot replies with the server (guild) ID and the ID of that channel.

Hosting it

A small VPS

Copy discordBot and config.json to /opt/audd-discord. Stop the copy you started in Step 3 first (Ctrl+C): two copies can’t share 127.0.0.1:4541. To run it as a systemd service, save the following unit as /etc/systemd/system/audd-discord.service:

[Unit]
Description=AudD Discord bot
After=network-online.target

[Service]
WorkingDirectory=/opt/audd-discord
ExecStart=/opt/audd-discord/discordBot
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
sudo systemctl enable --now audd-discord
journalctl -u audd-discord -f   # tail the logs

You can also use any other process supervisor. You don’t need to install ffmpeg on the host.

A container

Build the binary in one stage and run it in a second stage on a small base image with glibc (the binary is built with cgo). Mount config.json into the working directory.

Keeping it up

If the connection to Discord drops, the bot reconnects on its own, but if the process crashes, the service has to restart it. We recommend running no more than one copy per bot token (otherwise, both copies reply to every message and spend requests twice). A second copy on the same machine also can’t listen on 127.0.0.1:4541 and exits. Please check your logs if the bot doesn’t reply. You can find the usage stats for your API token on dashboard.audd.io; the bot doesn’t send any requests while it’s only listening to a voice channel. Let us know if you want a hard monthly limit on requests: send an email to [email protected].

When recognition fails

If there are no matches, the bot replies “Sorry, I couldn’t recognize the song.” The clip might be mostly speech or silence, or the song might not be in our database. If the bot can’t get any audio from the link, it replies “Sorry, I couldn’t get any audio from <link>”. If the token is wrong, or the account has no paid plan, the bot replies “Sorry, there’s been an error while processing the audio”, and the log shows the API returned an error: code 904, …. Check the AudDToken in config.json and your plan on dashboard.audd.io, then restart the bot. If a hard monthly limit has been reached, the reply is the same and the log shows the API error code.

If !song gets no reply at all while Recognize This Song works, check the Message Content Intent on the Bot page.


Related

Reading this as an AI agent? The raw Markdown is at integrations/discord-bot.md, and the full index is /resources/llms.txt.