Build a Discord bot that downloads videos
A common Discord bot feature: someone posts a TikTok, Reel or YouTube link and the bot replies with the video. With CrawlFetch the bot doesn't run yt-dlp or ffmpeg — it resolves the link and sends the file.
How it fits together
The bot exposes a /download slash command. It defers the reply, resolves the URL, then either attaches the file (if it fits Discord's upload limit) or replies with the download link.
The bot code
import { Client, GatewayIntentBits, AttachmentBuilder } from "discord.js";const client = new Client({ intents: [GatewayIntentBits.Guilds] });const LIMIT_MB = 10; // default upload limit for most serversclient.on("interactionCreate", async (i) => {if (!i.isChatInputCommand() || i.commandName !== "download") return;await i.deferReply();const r = await fetch("https://api.crawlfetch.com/v1/resolve?" + new URLSearchParams({ url: i.options.getString("url"), res: "720p" }), {headers: { Authorization: "Bearer " + process.env.CRAWLFETCH_KEY },});if (!r.ok) return i.editReply("Couldn't fetch that video (" + r.status + ").");const v = await r.json();const fmt = v.formats.find((f) => f.url === v.downloadUrl);if (fmt && fmt.sizeMb <= LIMIT_MB) {const file = Buffer.from(await (await fetch(v.downloadUrl)).arrayBuffer());return i.editReply({ content: v.title, files: [new AttachmentBuilder(file, { name: v.id + ".mp4" })] });}return i.editReply(v.title + "\n" + v.downloadUrl);});client.login(process.env.DISCORD_TOKEN);
Notes for Discord bot
- Request
res: "720p"so short clips stay under upload limits. - Rate-limit per user so one member can't drain your quota.
FAQ
Can the bot play music in voice channels?
It can fetch the audio file via audioUrl, but playing audio in voice is a separate feature of your bot library.
Related
Last updated 2026-10-25