Home > Net >  Can't play audio in voice channels, AttributeError: 'VoiceChannel' object has no attr
Can't play audio in voice channels, AttributeError: 'VoiceChannel' object has no attr

Time:11-15

vc = bot.get_channel(ctx.author.voice.channel.id)
await vc.connect()
await vc.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source="assets/a.mp3"))

This code was working like 3 months ago, and now it doesn't because voice channel object (vc) has no attribute play. Any reason why it stopped working and how to fix it?

Ignoring exception in command play:
Traceback (most recent call last):
  File "C:\Users\Poco\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\Poco\Desktop\py\elevate\main.py", line 38, in play
    await vc.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source="assets/ass.mp3"))
AttributeError: 'VoiceChannel' object has no attribute 'play'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Poco\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Poco\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Poco\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'VoiceChannel' object has no attribute 'play'

I tried to use different vcs, servers, permissions but nothing has solved the problem

CodePudding user response:

A AttributeError typically means you have the wrong type of object. Like the error says, a VoiceChannel has no attribute play. If you look the API docs for voice channel you can indeed see there is no play() method there.

However, if you do a quick search through the docs, you will find the method on VoiceClient. So you need a instance of VoiceClient to use the play method. How do you get a instance of VoiceClient? The docs state clearly how to do this:

You do not create these, you typically get them from e.g. VoiceChannel.connect().

If you look at the docs for VoiceChannel.connect() you see it states clearly:

Returns A voice client that is fully connected to the voice server.

So all you need to do is actually save the return value. So replace this:

await vc.connect()

with this:

connection = await vc.connect()

Now you have a instance of the voice channel object. Now, instead of calling play() on the channel, which has no method named play(), call it on the VoiceClient, which does. Our instance of the VoiceClient is named "conenction", so we can do this:

await connection.play(discord.FFmpegPCMAudio(executable="ffmpeg.exe", source="assets/a.mp3"))

CodePudding user response:

voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected:
    await voice.move_to(channel)
else:
    voice = await channel.connect()
  • Related