在 linux 上使用 discord.py 我得到错误 'Bot' object has no attribute 'join_voice_channel'

Using discord.py on linux I get the error 'Bot' object has no attribute 'join_voice_channel'

有问题的代码:

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()

我收到错误:

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'

我 运行 python3 Ubuntu 19 在 google 虚拟机上。所以我找遍了,只能找到这个 thread 谈论这个问题,但那里的解决方案没有用。这在 windows 环境中对我有用。这让我相信它是 opus 没有被加载所以我重新安装它并验证它被成功调用。我还创建了一个新的虚拟机实例,但也无济于事。我现在有点迷路了。

discord.py 的较新版本,称为 rewrite 分支,不再具有 client.join_voice_channel()。这已更改为 VoiceChannel.connect。请参阅文档 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()
# ...

由于新版本有很多变化,您可能需要花一些时间来迁移您的代码。如果您想安装旧版本的 discord.py,称为 async 分支,那么您可以通过 运行 在您的 linux 机器上执行以下命令。

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