Reputation: 11
I am trying to connect my bot to the same voice channel the author of the command is and play a video of their choice.
However, when running the following code:
async def join(ctx, search: str):
vc = discord.utils.get(ctx.guild.channels, name=ctx.author.voice)
voice = await vc.connect()
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
if not voice.is_playing():
await vc.connect()
author = ctx.message.author
query_string = urllib.parse.urlencode({'search_query': search})
html_content = urllib.request.urlopen('http://www.youtube.com/results?' + query_string)
search_content = html_content.read().decode()
search_results = re.findall(r'\/watch\?v=\w+', search_content)
vid_url = f'https://www.youtube.com{search_results[0]}'
voice.play(FFmpegPCMAudio(vid_url, **FFMPEG_OPTIONS))
voice.is_playing()
I get this error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'connect'
The error is saying that the variable 'vc' is equal to None, but I don't know why. I do have a suspicion that there is something wrong with the vc = discord.utils.get(ctx.guild.channels, name=ctx.author.voice)
statement
Upvotes: 0
Views: 94
Reputation: 7750
Indeed, the problem is with the line
vc = discord.utils.get(ctx.guild.channels, name=ctx.author.voice)
Specifically, name=ctx.author.voice
filters on the channel.name
equaling the author's VoiceStatus, which are completely different types and will never match, so you of course get None
.
Instead, you probably want to match on the the ctx.author.voice.channel
, and specifically the channel id
, rather than name
, to avoid potential collisions.
Altogether, you want:
vc = discord.utils.get(ctx.guild.channels, id=ctx.author.voice.channel.id)
I'd also throw in an if statement to check that the user/author is in fact in a voice channel, otherwise ctx.author.voice.channel.id
itself will raise an AttributeError, since channel
will be None
if they're not in a VoiceChannel when they send the command.
Upvotes: 1