E
E
EcoTry2021-06-12 19:34:21
Python
EcoTry, 2021-06-12 19:34:21

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


All installed by ffprobe, ffplay, ffmpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-06-13
@EcoTry

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

Let me guess, channel.connect() was throwing a 'NoneType' object has no attribute 'connect'?
You should never swallow exceptions in this way - all indiscriminately and without reaction.
channel.connect() throws exactly three possible exceptions, and they need to be handled properly!
asyncio.TimeoutError - the server hiccupped, we can't do anything, just ask the user to try again later.
ClientException - we are already connected to the voice chat, you need to either switch or ask to wait until we finish with the first request.
OpusNotLoaded - the opus library has not been loaded, it is needed to work with voice.
Once again, except: pass is a very, very good way to shoot yourself in the foot.
And the funny thing is that in the documentation for connect ()it is written that it returns voice_channel ready for work. Then it seems like it was not necessary to touch ctx.guild.voice_client, connect () will return the desired object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question