Answer the question
In order to leave comments, you need to log in
Problem with playing music in discord.py?
@slash.slash(guild_ids=guild_ids, name="play", description="Включить музыку", options=[create_option(name="url", description="Ссылка на песню", option_type=3, required=True)])
async def play(ctx, url):
if not ctx.author.voice:
emb = discord.Embed(title='Ошибка', color=0xff0000)
emb.add_field(name='Ты не подключен к каналу', value='Подключись к любому каналу', inline=False)
emb.set_thumbnail(url='https://cdn.discordapp.com/avatars/843118472892645377/e3dbb8f2b78f00b072ada3399c652b79.webp?size=128')
await ctx.send(embed=emb)
return
else:
channel = ctx.author.voice.channel
try:
await channel.connect()
except:
pass
server = ctx.guild
voice_channel = server.voice_client
emb = discord.Embed(title='Загружаем песню', color=0x90EE90)
msg = await ctx.send(embed=emb)
player = await YTDLSource.from_url(url, loop=bot.loop, stream=True)
voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
emb.title = "Сейчас играет"
emb.add_field(name="Композиция:", value=player.title, inline=True)
emb.set_thumbnail(url='https://cdn.discordapp.com/avatars/843118472892645377/e3dbb8f2b78f00b072ada3399c652b79.webp?size=128')
await msg.edit(embed=emb)
return
Answer the question
In order to leave comments, you need to log in
Well, think with your head.
> 'NoneType' object has no attribute ' play '
> voice_channel. play (
So, voice_channel is None.
We look where it came from, we read the docs on Guild.voice_client :
"voice_client - Returns the VoiceProtocol associated with this guild, if any.
Type: Optional [VoiceProtocol]" I.e.
it may well be None , and this should be checked before use.
But the more interesting question is why it is None.
You are referring to channel = ctx.author.voice.channel above - and not only voice can be None, but also channel in it. channel is not present, and just below you have an amazing construction:
try:
await channel.connect()
except:
pass
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question