Discord bot 无法连接到语音通道 (python)

问题描述

嗨 我想在 python 中制作一个不和谐的机器人,它可以连接到语音通道。我的代码

@client.command()
async def join(ctx):
  channel = ctx.author.voice
  if str(channel) == 'None':
    await ctx.channel.send(':x: You have to be in a voice channel to use this command. :x:')
  else:
    channel = ctx.author.voice.channel
    await channel.connect()

显示错误

Ignoring exception in command join:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py",line 85,in wrapped
    ret = await coro(*args,**kwargs)
  File "main.py",line 25,in join
    await channel.connect()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/abc.py",line 1150,in connect
    voice = cls(client,self)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/voice_client.py",line 199,in __init__
    raise RuntimeError("pynacl library needed in order to use voice")
RuntimeError: pynacl library needed in order to use voice

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

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py",line 902,in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py",line 864,in invoke
    await injected(*ctx.args,**ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/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: RuntimeError: pynacl library needed in order to use voice

如果有人有想法,非常感谢

解决方法

您需要安装 PyNaCl 库,您可以使用 pip 获取该库:https://pypi.org/project/PyNaCl/