pythondiscorddiscord.py

Why am I getting the error "command is not found"?


I was following this tutorial on how to create a discord music bot like Rythm using discord.py. I had troubleshooted my code and fixed a few errors to do with coroutine.

I have called the r function and now get this error upon running the code:

main.py:15: RuntimeWarning: coroutine 'r' was never awaited r()

When I run the code everything boots up successfully until I try to use a command. If I put '@join' in the chat it should have joined the voice chat or said "You're not in a voice channel!". Instead I get this error:

2022-09-02 20:07:08 ERROR discord.ext.commands.bot Ignoring exception in command None discord.ext.commands.errors.CommandNotFound: Command "join" is not found

I have tried swapping out @commands.command for @client.command (whilst also defining client in music.py) but more of the same.

I have two files, main.py and music.py.

MAIN.PY

import discord
from discord.ext import commands
import music

cogs = [music]

client = commands.Bot(command_prefix='@', intents=discord.Intents.all())


async def r():
    for i in range(len(cogs)):
        await cogs[i].setup(client)

r()

client.run(
    "my token")

MUSIC.PY

import discord
from discord.ext import commands
import youtube_dl


class music(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command()
    async def join(self, ctx):
        if ctx.author.voice is None:
            await ctx.send("You're not in a voice channel!")
        voice_channel = ctx.author.voice.channel
        if ctx.voice_client is None:
            await voice_channel.connect()
        else:
            await ctx.voice_client.move_to(voice_channel)

    @commands.command()
    async def disconnect(self, ctx):
        await ctx.voice_client.disconnect()

    @commands.command()
    async def play(self, ctx, url):
        ctx.voice_client.stop()
        FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 - 
        reconnect_delay_max 5', 'options': '-vn'}
        YDL_OPTIONS = {'format': "bestaudio"}
        vc = ctx.voice_client

        with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
            info = ydl.extract_info(url, download=False)
            url2 = info['formats'][0]['url']
            source = await discord.FFmpegOpusAudio.from_probe(
                url2, **FFMPEG_OPTIONS)
            vc.play(source)

    @commands.command()
    async def pause(self, ctx):
        await ctx.voice_client.pause()
        await ctx.send("Paused")

    @commands.command()
    async def resume(self, ctx):
        await ctx.voice_client.resume()
        await ctx.send("Resumed")


async def setup(client):
    await client.add_cog(music(client))

Solution

  • When you define async function like async def r() then you would have to run it await r(). Problem is that you can't run await outside functions. You would have to run it in some async function - but it make the same problem with running this async function.

    You may try to run it in event on_ready() which is async function.

    @client.event
    async def on_ready():
        for item in cogs:
            await item.setup(client)