Ask coding questions

← Back to all posts
need helpp
CAUESPINDOLA (1)

Does anyone know what I'm doing wrong? it's a command in python I'm doing for discord please i need help!! cod: @client.command()
async def Youtube(ctx, *, search):
query_string = urllib.parse.urlencode({
'search_query': search
})
htm_content = urllib.request.urlopen(
'http://www.youtube.com/results?'+ query_string
)
search_results = re.findall('href=\"\?watch\?v=(.{11})', htm_content.read().decode())
await ctx.send(f'http://www.youtube.com/watchv=' + search_results[0])

Comments
hotnewtop
InvisibleOne (3264)

change the first line to async def Youtube(ctx, search): you don't need the * just enclose the search in quotes if it is more then one word. * will return everything as a tuple or list I think

Coder100 (19280)

also send repl as well thank you

CAUESPINDOLA (1)

@Coder100 Ignoring exception in command Youtube:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 100, in Youtube
await ctx.send(f'http://www.youtube.com/watchv=' + search_results[0])
IndexError: list index out of range

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: IndexError: list index out of range

Coder100 (19280)

that meant there were no search results, search_results was [] @CAUESPINDOLA