melonyeeps

Make a discord bot when you do /downchannel <ytchannelurl>

Kodari AI
KodariJan 18 at 11:15 pm 842 (v2)

I’ll create a Discord bot with a slash command to download YouTube channel content.

YouTubeChannelDownloader
A Discord bot that downloads videos from YouTube channels using slash commands. Users can provide a YouTube channel URL and the bot will download the channel's content. Built with discord.py and yt-dlp for reliable video downloading. Features command validation, download progress tracking, and error handling for failed downloads.

[Generation stopped]

Kodari can make mistakes. Review important code before using.
Files
.gitignore
bot.py
README.md
requirements.txt
Code
4 93
bot.py
import discord
from discord import app_commands
from discord.ext import commands
import os
from dotenv import load_dotenv
import yt_dlp
import asyncio

load_dotenv()

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    try:
        synced = await bot.tree.sync()
        print(f'Bot is ready! Logged in as {bot.user}')
        print(f'Synced {len(synced)} command(s)')
    except Exception as e:
        print(f'Failed to sync commands: {e}')

@bot.tree.command(name="downchannel", description="Download videos from a YouTube channel")
@app_commands.describe(ytchannelurl="The URL of the YouTube channel to download")
async def downchannel(interaction: discord.Interaction, ytchannelurl: str):
    await interaction.response.defer(thinking=True)
    
    if not ytchannelurl.startswith(('https://www.youtube.com/', 'https://youtube.com/', 'https://m.youtube.com/')):
        await interaction.followup.send('Invalid YouTube URL! Please provide a valid YouTube channel URL.')
        return
    
    try:
        await interaction.followup.send(f'Starting download from channel: {ytchannelurl}\nThis may take a while...')
        
        ydl_opts = {
            'format': 'best',
            'outtmpl': 'downloads/%(channel)s/%(title)s.%(ext)s',
            'ignoreerrors': True,
            'no_warnings': False,
            'extract_flat': False,
            'quiet': False,
        }
        
        loop = asyncio.get_event_loop()
        
        def download():
            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                info = ydl.extract_info(ytchannelurl, download=True)
                return info
        
        info = await loop.run_in_executor(None, download)
        
        if info:
            channel_name = info.get('channel', 'Unknown')
            video_count = len(info.get('entries', []))
            await interaction.followup.send(f'Successfully downloaded {video_count} videos from {channel_name}!')
        else:
            await interaction.followup.send('Download completed but no information was retrieved.')
            
    except Exception as e:
        await interaction.followup.send(f'An error occurred: {str(e)}')
        print(f'Error in downchannel command: {e}')

bot.run(os.getenv('DISCORD_TOKEN'))