How can I ignore the ctx argument from discord.py?

Viewed 157

Here is the full script:

from discord.ext.commands import Bot
import tracemalloc

tracemalloc.start()
client = Bot(command_prefix='$')
TOKEN = ''

@client.event
async def on_ready():
    print(f'Bot connected as {client.user}')
    
@client.command(pass_context=True)
async def yt(ctx, url):
    author = ctx.message.author
    voice_channel = author.voice_channel
    vc = await client.join_voice_channel(voice_channel)
    player = await vc.create_ytdl_player(url)
    player.start()

@client.event
async def on_message(message):
    if message.content == 'play':
        await yt("youtubeurl")
client.run(TOKEN)

When I run this script, it works fine. When I type play in the chat, I get this error:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\user\bot.py", line 26, in on_message
    await yt("youtubeurl")
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 374, in __call__
    return await self.callback(*args, **kwargs)
TypeError: yt() missing 1 required positional argument: 'url'

How do I fix this? So far all I have tried is added the argument * between ctx and url, which didn't fix it

1 Answers

It is not possible to "ignore" the context argument, why are you invoking the command in the on_message event?

You can get the context with Bot.get_context

@client.event
async def on_message(message):
    ctx = await client.get_context(message)
    if message.content == 'play':
        yt(ctx, "url")

I'm guessing your commands aren't working cause you didn't add process_commands at the end of the on_message event

@client.event
async def on_message(message):
    # ... PS: Don't add the if statements for the commands, it's useless
    await client.process_commands(message)

Every command should be working from now on.

Reference:

Related