Home>
import discord
from discord.ext import commands
import youtube_dl
intents= discord.Intents().all()
bot= discord.Client()
client= discord.Client(intents=intents)
bot= commands.Bot(command_prefix='!', intents=intents)
YDL_OPTIONS= {'format': 'worstaudio/best',
               'noplaylist': 'True', 'simulate': 'True', 'preferredquality': '192', 'preferredcodec': 'mp3', 'key': 'FFmpegExtractAudio'}
class mod(commands.cog):
    def __init__(self, bot):
        self.bot= bot
    @commands.command()
    async def clear(self, ctx, amount: int): # define a function, i.e. team name.
        messages= await ctx.channel.purge(limit=amount + 1) # purge messages, +1 since ours will also be deleted
    @commands.command() # embed in notes about all commands
    async def cmds(self, ctx):
        cmdsembed= discord.Embed(title='` Bot Commands `')
        cmdsembed.add_field(name="` Smart commands `", value=">>> `!profil` -view profile\n`!avatar` -view avatar\n`!cmds` -all bot commands", inline=True)
        cmdsembed.add_field(name="` Music commands `", value=">>> `!join` -the bot will enter the voice\n`!leave` -the bot will leave the voice", inline=True)
        await ctx.send(embed=cmdsembed)
    @commands.command()
    async def profile(self, ctx):
        embed= discord.Embed(title=f"Profile -{ctx.message.author}") # main embed
        embed.add_field(name=">>> Status: ", value="`Not defined`", inline=True)
        embed.set_thumbnail(url=ctx.author.avatar_url) # ava right square
        embed.add_field(name=">>> Balance: ", value="`0`", inline=True)
        embed.add_field(name=">>> Voice online: ", value="`0 h /0 m`", inline=True)
        embed.add_field(name=">>> Beloved: ", value="`no`", inline=True)
        await ctx.send(embed=embed)
    @commands.command()
    async def avatar(self, ctx):
        avaembed= discord.Embed(title= f"Avatar is {ctx.message.author}")
        avaembed.set_thumbnail(url=ctx.author.avatar_url)
        avaembed.add_field(name=f">>> @{ctx.message.author} ,", value="below `Your` avatar.", inline=True)
        avaembed.set_image(url=ctx.author.avatar_url)
        await ctx.send(embed=avaembed)
youtube_dl.utils.bug_reports_message= lambda: ''
ytdl_format_options= {
    'format': 'bestaudio/best',
    'restrictfilenames': True,
    'noplaylist': True,
    'nocheckcertificate': True,
    'ignoreerrors': False,
    'logtostderr': False,
    'quiet': True,
    'no_warnings': True,
    'default_search': 'auto',
    'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}
ffmpeg_options= {
    'options': '-vn'
}
ytdl= youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(commands.Cog):
    def __init__(self, source, *, data, volume=0.5):
        super().__init__(source, volume)
        self.data= data
        self.title= data.get('title')
        self.url= ""
    @classmethod
    async def from_url(cls, url, *, loop=None, stream=False):
        loop= loop or asyncio.get_event_loop()
        data= await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
        if 'entries' in data:
            # take first item from a playlist
            data= data['entries'][0]
        filename= data['title'] if stream else ytdl.prepare_filename(data)
        return filename
    @bot.command(name= 'play')
    async def play(self,ctx, url):
        if not ctx.message.author.name== "Rohan Krishna":
            await ctx.send('NOT AUTHORISED!')
            return
        try:
            server= ctx.message.guild
            voice_channel= server.voice_clientasync with ctx.typing(): filename= await YTDLSource.from_url(url, loop=bot.loop)
                voice_channel.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source=filename))
            await ctx.send('**Now playing:** {}'.format(filename))
        except:
            await ctx.send("The bot is not connected to a voice channel.")
    @bot.command(name= 'join')
    async def join(self,ctx):
        if not ctx.message.author.voice:
            await ctx.send("{} is not connected to a voice channel".format(ctx.message.author.name))
            return
        else:
            channel= ctx.message.author.voice.channel
        await channel.connect()
    @bot.command(name='pause')
    async def pause(self,ctx):
        voice_client= ctx.message.guild.voice_client
        if voice_client.is_playing():
            await voice_client.pause()
        else:
            await ctx.send("The bot is not playing anything at the moment.")
    @bot.command(name='resume')
    async def resume(self,ctx):
        voice_client= ctx.message.guild.voice_client
        if voice_client.is_paused():
            await voice_client.resume()
        else:
            await ctx.send("The bot was not playing anything before this. Use play_song command")
    @bot.command(name='leave')
    async def leave(self,ctx):
        voice_client= ctx.message.guild.voice_client
        if voice_client.is_connected():
            await voice_client.disconnect()
        else:
            await ctx.send("The bot is not connected to a voice channel.")
    @bot.command(name='stop')
    async def stop(self,ctx):
        voice_client= ctx.message.guild.voice_client
        if voice_client.is_playing():
            await voice_client.stop()
        else:
            await ctx.send("The bot is not playing anything at the moment.")
def setup(bot):
    bot.add_cog(mod(bot=bot))

Such a problemdiscord.ext.commands.errors.CommandNotFound: Command "join" is not found. The team does not see from play. I think the problem is in two classes and tabs. Help a newbie please.

  • Answer # 1
    class Music(commands.Cog):
    def __init__(self, bot):
        self.bot= bot
    @commands.command(name= 'play')
    async def play(self,ctx, url):
        if not ctx.message.author.name== "Rohan Krishna":
            await ctx.send('NOT AUTHORISED!')
            return
        try:
            server= ctx.message.guild
            voice_channel= server.voice_client
            async with ctx.typing():
                filename= await YTDLSource.from_url(url, loop=bot.loop)
                voice_channel.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source=filename))
            await ctx.send('**Now playing:** {}'.format(filename))
        except:
            await ctx.send("The bot is not connected to a voice channel.")
        @commands.command(name= 'join')
        async def join(self,ctx):
            if not ctx.message.author.voice:
                await ctx.send("{} is not connected to a voice channel".format(ctx.message.author.name))
                return
            else:
                channel= ctx.message.author.voice.channel
           await channel.connect()
        @commands.command(name='pause')
        async def pause(self,ctx):
        voice_client= ctx.message.guild.voice_client
        if voice_client.is_playing():
            await voice_client.pause()
        else:
            await ctx.send("The bot is not playing anything at the moment.")
        @commands.command(name='resume')
        async def resume(self,ctx):
        voice_client= ctx.message.guild.voice_client
        if voice_client.is_paused():
            await voice_client.resume()
        else:
            await ctx.send("The bot was not playing anything before this. Use play_song command")
        @commands.command(name='leave')
        async def leave(self,ctx):
        voice_client= ctx.message.guild.voice_client
        if voice_client.is_connected():
            await voice_client.disconnect()
        else:
            await ctx.send("The bot is not connected to a voice channel.")
        @commands.command(name='stop')
        async def stop(self,ctx):
        voice_client= ctx.message.guild.voice_client
        if voice_client.is_playing():
            await voice_client.stop()
        else:
            await ctx.send("The bot is not playing anything at the moment.")
    def setup(bot):
        bot.add_cog(Music(bot=bot))
    

    This needs to be inserted after the YTDLSource class. It doesn't load commands since YTDLSource is not a cog class.

  • Answer # 2
    class Music(commands.Cog):
    def __init__(self, bot):
        self.bot= bot
    @commands.command(name= 'play')
    async def play(self,ctx, url):
        if not ctx.message.author.name== "Rohan Krishna":
            await ctx.send('NOT AUTHORISED!')
            return
        try:
            server= ctx.message.guild
            voice_channel= server.voice_client
            async with ctx.typing():
                filename= await YTDLSource.from_url(url, loop=bot.loop)
                voice_channel.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source=filename))
            await ctx.send('**Now playing:** {}'.format(filename))
        except:
            await ctx.send("The bot is not connected to a voice channel.")
        @commands.command(name= 'join')
        async def join(self,ctx):
            if not ctx.message.author.voice:
                await ctx.send("{} is not connected to a voice channel".format(ctx.message.author.name))
                return
            else:
                channel= ctx.message.author.voice.channel
           await channel.connect()
        @commands.command(name='pause')
        async def pause(self,ctx):
        voice_client= ctx.message.guild.voice_client
        if voice_client.is_playing():
            await voice_client.pause()
        else:
            await ctx.send("The bot is not playing anything at the moment.")
        @commands.command(name='resume')
        async def resume(self,ctx):
        voice_client= ctx.message.guild.voice_client
        if voice_client.is_paused():
            await voice_client.resume()
        else:
            await ctx.send("The bot was not playing anything before this. Use play_song command")
        @commands.command(name='leave')
        async def leave(self,ctx):
        voice_client= ctx.message.guild.voice_client
        if voice_client.is_connected():
            await voice_client.disconnect()
        else:
            await ctx.send("The bot is not connected to a voice channel.")
        @commands.command(name='stop')
        async def stop(self,ctx):
        voice_client= ctx.message.guild.voice_client
        if voice_client.is_playing():
            await voice_client.stop()
        else:
            await ctx.send("The bot is not playing anything at the moment.")
    def setup(bot):
        bot.add_cog(Music(bot=bot))
    

    This needs to be inserted after the YTDLSource class. It doesn't load commands since YTDLSource is not a cog class.