Using discord.py on linux I get the error 'Bot

2020-03-31 22:13发布

问题:

Code in question:

bot = commands.Bot(command_prefix='!')
@bot.command(pass_context=True)
    async def yt(ctx, url):
    author = ctx.message.author
    voice_channel = author.voice.channel
    vc = await bot.join_voice_channel(voice_channel)

    player = await vc.create_ytdl_player(url, before_options="-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5")
    player.start()

Error I receive:

Traceback (most recent call last):
    File "/home/wilkins30/.local/lib/python3.7/site-packages/discord/ext/commands/core.py", line 79, in wrapped
        ret = await coro(*args, **kwargs)
    File "OverBot.py", line 286, in one
        vc = await bot.join_voice_channel(voice_channel)
AttributeError: 'Bot' object has no attribute 'join_voice_channel'

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

Traceback (most recent call last):
    File "/home/wilkins30/.local/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 863, in invoke
        await ctx.command.invoke(ctx)
    File "/home/wilkins30/.local/lib/python3.7/site-packages/discord/ext/commands/core.py", line 727, in invoke
        await injected(*ctx.args, **ctx.kwargs)
    File "/home/wilkins30/.local/lib/python3.7/site-packages/discord/ext/commands/core.py", line 88, in wrapped
        raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: 
AttributeError: 'Bot' object has no attribute 'join_voice_channel'

I am running python3 in Ubuntu 19 on a google virtual machine. So I have looked all over and could only find this thread talking about the issue but solutions there didn't work. This worked for me in a windows environment. which lead me to believe it was opus not being loaded so I reinstalled that and verified that it was being successfully called. I have also created a fresh instance of the virtual machine to no avail as well. I'm sort of lost at the moment.

回答1:

The newer versions of discord.py, referred to as the rewrite branch, no longer has client.join_voice_channel(). This has been changed to VoiceChannel.connect. See documentation here.

Before:

vc = await client.join_voice_channel(channel)
player = vc.create_ffmpeg_player('testing.mp3', after=lambda: print('done'))
player.start()

player.is_playing()
player.pause()
player.resume()
player.stop()
# ...

After:

vc = await channel.connect()
vc.play(discord.FFmpegPCMAudio('testing.mp3'), after=lambda e: print('done', e))
vc.is_playing()
vc.pause()
vc.resume()
vc.stop()
# ...

As there are many changes in the new version, you may have to spend some time to migrate your code. If you would like to install the older version of discord.py, referred to as the async branch, then you can do so by running the following command on your linux machine.

sudo python3 -m pip install discord.py==0.16.12 --force-reinstall