music bot in discord code example

Example 1: python discord music bot

There were some changes to youtube_dl and FFmpeg, so this should work.

import discord

import youtube_dl

from discord.ext import commands

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}   

def endSong(guild, path):
    os.remove(path)                                   

@cat.command(pass_context=True)
async def play(ctx, url):
    if not ctx.message.author.voice:
        await ctx.send('you are not connected to a voice channel')
        return

    else:
        channel = ctx.message.author.voice.channel

    voice_client = await channel.connect()

    guild = ctx.message.guild

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        file = ydl.extract_info(url, download=True)
        path = str(file['title']) + "-" + str(file['id'] + ".mp3")

    voice_client.play(discord.FFmpegPCMAudio(path), after=lambda x: endSong(guild, path))
    voice_client.source = discord.PCMVolumeTransformer(voice_client.source, 1)

    await ctx.send(f'**Music: **{url}')
Optional, useful function
If you want you can make your bot leave the voice channel after the song/music stop playing. Add this at the end of your code.

while voice_client.is_playing():
        await asyncio.sleep(1)
    else:
        await voice_client.disconnect()
        print("Disconnected")
If something wouldn't work
If you had any problem with understanding the code or something wouldn't work, comment under my answer and I'll try my best to help you. Btw if you are thinking about hosting your bot on Heroku I can help you with that, because there are some things you need to do to make your music bot run there.

Example 2: discord.js music

//type npm install discord-misic-system --save
//type npm install discord.js --save
//type node .
//see your music bot is ready :] enjoy I LOVE MY INDIA


const MusicBot = require("discord-music-system"); // Require the module
 
const bot = new MusicBot({ // Create the bot
    token: ("type you bot token here"), // You can find the token at https://discord.com/developers/applications/
    ytApiKey: ("Your Youtube api here"), // Video to explain how to get it: https://www.youtube.com/watch?v=VqML5F8hcRQ
    prefix: 'in$', // Example: /
    game: 'sanikava `in$`' // Example: /help
});
 
bot.run(); // Run the bot

Example 3: how make a music bot

go here https://github.com/WeebDev/Commando/tree/master/commands/music