How to Automate TikTok Downloads With a Discord Bot

By The Tiklocker Team on 2026-04-08 18:58:37

Tired of leaving Discord to download TikTok videos? You can build a simple Discord bot that automatically downloads any TikTok link posted in your server. Post a link, get the video. No watermark, no leaving the chat.

🤖 What We're Building

A Discord bot that: 1. Watches for TikTok links in chat messages 2. Downloads the video via the Tiklocker API 3. Posts the watermark-free video directly in the channel

🛠️ Prerequisites

📝 The Bot Code

import discord
import requests
import re
import os

DISCORD_TOKEN = "your_discord_bot_token"
TIKLOCKER_API_KEY = "your_tiklocker_api_key"
API_URL = "https://tiklocker.com/api/batch-download"

# Regex to match TikTok URLs
TIKTOK_PATTERN = re.compile(
    r'https?://(?:www\.|vm\.)?tiktok\.com/[^\s]+'
)

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print(f"Bot is online as {client.user}")

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    # Find TikTok URLs in the message
    urls = TIKTOK_PATTERN.findall(message.content)
    if not urls:
        return

    for url in urls:
        await message.channel.send(f"⏳ Downloading video...")

        try:
            headers = {
                "Authorization": f"Bearer {TIKLOCKER_API_KEY}",
                "Content-Type": "application/json"
            }
            response = requests.post(
                API_URL,
                json={"urls": [url]},
                headers=headers
            )

            if response.status_code == 200:
                filename = "tiktok_video.mp4"
                with open(filename, "wb") as f:
                    f.write(response.content)

                await message.channel.send(
                    file=discord.File(filename)
                )
                os.remove(filename)
            else:
                await message.channel.send(
                    "❌ Couldn't download that video. "
                    "Make sure the link is valid and public."
                )
        except Exception as e:
            await message.channel.send(f"❌ Error: {str(e)}")

client.run(DISCORD_TOKEN)

🔧 Setup Instructions

  1. Create a Discord Application:

    • Go to discord.com/developers/applications
    • Create a new application, then create a bot under the "Bot" tab
    • Enable "Message Content Intent" under Privileged Gateway Intents
    • Copy the bot token
  2. Invite the Bot to Your Server:

    • Go to the OAuth2 URL Generator
    • Select "bot" scope and "Send Messages" + "Attach Files" permissions
    • Use the generated URL to invite the bot
  3. Run the Bot:

    python tiktok_bot.py
    
  4. Test It: Post any TikTok link in your server and the bot will respond with the downloaded video.

🔒 Security Tips

  • Never commit your tokens to version control. Use environment variables instead:
    DISCORD_TOKEN = os.environ["DISCORD_TOKEN"]
    TIKLOCKER_API_KEY = os.environ["TIKLOCKER_API_KEY"]
    
  • Limit to specific channels if you don't want the bot active everywhere
  • Add rate limiting to prevent abuse of your API credits

🚀 Advanced Features to Add

  • Support for other platforms: Extend the regex to match Instagram, YouTube, Twitter, and Reddit URLs
  • Queue system: Handle multiple downloads simultaneously without blocking
  • Credit tracking: Display remaining API credits with a /credits command
  • Auto-delete: Remove the original link message after posting the video for a cleaner chat

🤔 FAQ

How many credits does this use? Each video download uses 1 API credit, regardless of whether it's triggered by the bot or manually.

Can the bot handle multiple links in one message? Yes. The code above loops through all TikTok URLs found in a single message and downloads each one.

What's the file size limit for Discord? Discord allows files up to 8MB for free servers (25MB with Nitro boosts). Most TikTok videos are well under this limit.

Get your API key at Tiklocker.com/api and start building.

Enjoying the content?

Get updates on new features, blog posts, and exclusive offers directly in your inbox.