Run a Reddit music-recognition bot (the AudD RedditBot template)

Deploy AudD's open-source Reddit bot — the one that powers u/auddbot — to answer 'what's this song?' on posts and comments by recognizing music in linked videos and streams.

6 min read View as Markdown
On this page

u/auddbot identifies music on Reddit. When someone mentions it or writes a question like “what’s the song”, it sends the URL of the video (or livestream) to AudD’s Music Recognition API. The source code, in Go, is in the AudDMusic/RedditBot repository.

How the bot works

When the bot starts, it connects to Reddit to get the new mentions (including the mentions of u/RecognizeSong). After that, it connects to the Reddit r/all streams to get new comments and posts. For a comment or post with one of the Triggers or a mention, it checks first for the links in the comment, then for the links in the comment to which it was a reply, then for the links in the post. Then it calls the RecognizeLongAudio() method, which sends the video link to the enterprise endpoint.

The reply will contain the title and artist of the song, the album, label and release date, the score, and the timecode. We add URLs of song pages with links to the streaming platforms (for example, lis.tn/Warriors) to the replies. Lots of subreddits ban links to the platforms, so unless the bot was mentioned or the subreddit is in ApprovedOn, it posts the links in a second reply.

If the server has successfully processed the request, but there are no matches, the bot stays silent after a trigger phrase and replies “Sorry, I couldn’t recognize the song” after a mention. The bot never answers comments from its own accounts.

Why the enterprise endpoint

We have a separate endpoint that handles large files. The standard endpoint accepts files up to 10 MB and analyzes the first 12 seconds; the enterprise endpoint accepts even days-long files. The server splits the audio into 12-second chunks and counts 1 request per chunk. The bot sets limit, the upper bound for the number of chunks the server will recognize: limit=3 for a Reddit video (v.redd.it), so at most 36 seconds and 3 requests, limit=1 for livestreams, and limit=2 for other links.

Before you start

Please note that to be able to sign in, the bot needs two Reddit accounts without two-factor authentication, and both have to be listed as developers of the Reddit app. To send requests, receive a token from the Dashboard. Any paid plan works: you don’t need an enterprise account to use the enterprise endpoint. You also need Go 1.21 or newer and a server that keeps a process running.

Walkthrough

Step 1: Register a Reddit app

Sign in as one of the bot accounts and create a new app of type script at reddit.com/prefs/apps. The bot can only sign in as accounts listed as developers of the app, so add the other bot account as a developer. Then get the client ID (the short string under the app name) and client secret for the new application. Please note that Reddit requires a descriptive user agent on every request, so set UserAgent in config.json to your bot’s name and your Reddit username. Requests with a default or spoofed user agent will be throttled or blocked.

Step 2: Clone and configure

Clone the repository to your server.

Shell
git clone https://github.com/AudDMusic/RedditBot
cd RedditBot

Replace the repo’s config.json with the one below: the repo’s file has a trailing comma and doesn’t parse. Then get a token from AudD and put it in the AudDToken field.

JSON
{
  "ClientID": "your-reddit-client-id",
  "ClientSecret": "your-reddit-client-secret",
  "UserAgent": "reddit-music-bot/1.0 by u/your-bot-account",
  "BotPasswords": { "your-bot-account": "your-bot-password", "your-second-bot-account": "your-second-bot-password" },
  "AudDToken": "your-api-token",
  "Triggers": [
    "whats the song", "what is the song", "whats this song", "what is this song",
    "what song is this", "what song is playing", "what track is this"
  ],
  "AudDTAntiTriggers": [
    "has been automatically removed", "your comment was removed", "I am a bot",
    "comment here with such low karma", "bot wants to find the best and worst bots"
  ],
  "CommentsMinScore": 0,
  "LiveStreamMinScore": 0,
  "ReplySettings": {
    "mention": { "SendLinks": true, "ReplyAlways": true },
    "comment": { "SendLinks": false, "ReplyAlways": false },
    "post": { "SendLinks": false, "ReplyAlways": false }
  },
  "IgnoreSubreddits": [
    "// subreddits bots should generally avoid",
    "suicidewatch",
    "depression",

    "// subreddits with account age thresholds",
    "wallstreetbets",        "// 45 days",
    "yeagerbomb",            "// 30 days",
    "NormalDayInArabia",     "// 30 days"
  ],
  "SubredditsBannedOn": [],
  "ApprovedOn": [],
  "MaxTriggerTextLength": 400,
  "DontPostPatreonLinkOn": ["BLACKPINK"],
  "DontUseFormattingOn": ["tipofmytongue"],
  "RavenDSN": ""
}

By default, the bot signs in as u/auddbot and u/RecognizeSong. Change this in the AudDBotUsername and RecognizeSongBotUsername constants in main.go before you build, and use exactly the same names as the BotPasswords keys in config.json. If they don’t match, the bot prints missing password and retries every 10 seconds. The quoted names auddbot, RecognizeSong and recognizesong are also in the mention and author checks in main.go, so replace them too, keeping the case.

Put the client ID and secret in ClientID and ClientSecret, and the passwords of both bot accounts in BotPasswords. The Triggers are phrases the bot reacts to; the AudDTAntiTriggers are phrases that stop it from replying. The bot reads the anti-triggers from AudDTAntiTriggers, not from the AntiTriggers key in the repo’s file, so the sample uses that name.

Please note that the score is one of the enterprise features, available on a Startup plan or higher, on request from [email protected]. The bot replies only with matches that have at least CommentsMinScore (for links in comments, posts and mentions) or LiveStreamMinScore (for livestreams), so raise them to make the bot quieter. If the score is missing, the bot reads every score as 0, so keep both keys at 0 (as in the sample); the replies will then show a 0% score. For more information, see “Choosing a score threshold.”

The IgnoreSubreddits are subreddits to ignore (keep the repo’s entries, such as suicidewatch and depression), the SubredditsBannedOn are subreddits where your bot is banned, and the ApprovedOn are subreddits where the bot can post links in the first reply.

You can leave MaxTriggerTextLength, DontPostPatreonLinkOn and DontUseFormattingOn as they are in the sample: the repo’s values work for your own copy. Use RavenDSN to add a Sentry DSN to capture errors. By default, ReplySettings lets the bot share links in the first reply and reply when it hasn’t recognized a song only for mentions, not for comments and posts.

Step 3: Build and run (Go 1.21+)

Shell
go build -v ./...
./redditBot

When the bot is ready, it prints “You are now logged in, /u/…” for both accounts and then “started”. It keeps running until you stop it. To test it, reply “whats the song” to a post with a music video from a third account, since the bot never answers comments from its own accounts. It should take the bot a few seconds to a minute to reply. You can find the usage stats for your API token on dashboard.audd.io.

If the bot doesn’t reply

If the bot prints missing password every 10 seconds, the BotPasswords keys don’t match the two names in main.go. If it panics at start (invalid character ']' looking for beginning of value), config.json isn’t valid JSON. If the log says oauth2: cannot fetch token: 401 Unauthorized, check the client ID, the secret and the passwords, and that both accounts are developers of the app.

If the bot logs code 904 when someone summons it, the token is wrong or isn’t on a paid plan: “You haven’t passed a valid API token. Music Recognition by URL on our enterprise endpoint is only available to paid users.”

If a trigger phrase gets no reply, the bot usually found no match. It scans only the first 24-36 seconds of the video, unless the comment or the URL has a timestamp (at 1:48, or ?t=1m48s), so the song may play later. The track may also not be in the database, or the audio may be mostly speech. A mention gets “Sorry, I couldn’t recognize the song. I tried to identify music from the link at 00:00-00:24.” If the reply names the wrong song, mention the bot again with a timestamp.

Check the logs for Reddit API errors: throttling usually traces back to a default UserAgent or to two copies running on the same accounts, and a banned account shows up there too.

Hosting it

By default, the bot reads config.json from the working directory. You can change this in the configFile constant in main.go. Stop the copy you started in Step 3, then create /opt/audd-reddit and copy redditBot and config.json into it: we recommend running no more than one copy per pair of bot accounts (otherwise, both copies reply to every comment and spend requests twice). Save this unit as /etc/systemd/system/audd-reddit.service:

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

[Service]
WorkingDirectory=/opt/audd-reddit
ExecStart=/opt/audd-reddit/redditBot
Restart=always
RestartSec=10

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

If you don’t want to use the above unit, any other process supervisor works too. The bot reloads config.json when the file changes.


Related

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