I want it to search for the video and play it, how can i change the following code to achieve that? Every time I use the ytsearch function in ytdl, I notice that it only searches for the first word of the title and download it, however, it causes error later on and do nothing.
JavaScript
x
24
24
1
@commands.command()
2
async def play(self, ctx, url):
3
if ctx.author.voice is None:
4
await ctx.send("You are not in a voice channel!")
5
voice_channel = ctx.author.voice.channel
6
if ctx.voice_client is None:
7
await voice_channel.connect()
8
else:
9
await ctx.voice_client.move_to(voice_channel)
10
11
ctx.voice_client.stop()
12
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
13
YDL_OPTIONS = {'format':"bestaudio", 'default_search':"ytsearch"}
14
vc = ctx.voice_client
15
16
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
17
info = ydl.extract_info(url, download=False)
18
if 'entries' in info:
19
url2 = info["entries"][0]["formats"][0]
20
elif 'formats' in info:
21
url2 = info["formats"][0]['url']
22
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
23
vc.play(source)
24
And this is the error message:
JavaScript
1
31
31
1
Ignoring exception in command play:
2
Traceback (most recent call last):
3
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
4
ret = await coro(*args, **kwargs)
5
File "/home/runner/HandmadeLivelyLines/music.py", line 44, in play
6
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
7
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/player.py", line 387, in from_probe
8
return cls(source, bitrate=bitrate, codec=codec, **kwargs)
9
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/player.py", line 324, in __init__
10
super().__init__(source, executable=executable, args=args, **subprocess_kwargs)
11
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/player.py", line 138, in __init__
12
self._process = self._spawn_process(args, **kwargs)
13
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/player.py", line 144, in _spawn_process
14
process = subprocess.Popen(args, creationflags=CREATE_NO_WINDOW, **subprocess_kwargs)
15
File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
16
self._execute_child(args, executable, preexec_fn, close_fds,
17
File "/usr/lib/python3.8/subprocess.py", line 1639, in _execute_child
18
self.pid = _posixsubprocess.fork_exec(
19
TypeError: expected str, bytes or os.PathLike object, not dict
20
21
The above exception was the direct cause of the following exception:
22
23
Traceback (most recent call last):
24
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
25
await ctx.command.invoke(ctx)
26
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
27
await injected(*ctx.args, **ctx.kwargs)
28
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
29
raise CommandInvokeError(exc) from exc
30
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: expected str, bytes or os.PathLike object, not dict
31
Thanks.
Advertisement
Answer
To resolve the issue of the URL:
JavaScript
1
2
1
url2 = info["entries"][0]["formats"][0]['url']
2
And the issue of only one word being taken into the search query:
JavaScript
1
2
1
async def play(self, ctx, *, url):
2