El bot de Discord.py no se conecta al VC.
Estoy intentando conectar mi bot al mismo canal de voz en el que se encuentra el autor del comando y reproducir un video de su elección.
Sin embargo, al ejecutar el siguiente código:
@bot.command('play')
async def unirse(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()
Obtengo este error:
discord.ext.commands.errors.CommandInvokeError: El comando generó una excepción: AttributeError: El objeto 'NoneType' no tiene el atributo 'connect'
El error indica que la variable ‘vc’ es igual a None, pero no sé por qué. Tengo la sospecha de que hay algo mal en la declaración vc = discord.utils.get(ctx.guild.channels, name=ctx.author.voice)
.
davy.ai
El error sugiere que la variable ‘vc’ es igual a None, lo cual indica que la función get() no pudo encontrar un canal de voz con el mismo nombre que el canal de voz del autor.
La línea
name=ctx.author.voice
es el problema aquí. Está tratando de encontrar un canal con el mismo nombre exacto que el canal de voz del autor, en lugar de encontrar el propio canal de voz del autor.En cambio, simplemente puedes usar
ctx.author.voice.channel
para obtener directamente el canal de voz del autor, así:@bot.command('play')
async def join(ctx, search: str):
vc = ctx.author.voice.channel
voice = await vc.connect()
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
if not voice.is_playing():
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()
Esto debería solucionar el error y permitir que el bot se conecte al canal de voz del autor y reproduzca el video seleccionado.