无法让我的 Discord 机器人连接到语音通道
Can't get my Discord bot to connect to voice channel
我是第一次玩机器人,但似乎无法让它加入语音频道。我知道 $join 命令正在运行,因为我的机器人会用正确的频道名称说“正在连接到 {channel}”,但它仍然无法连接。如果我不在语音频道中,机器人会正确地说“错误”。
我已经尝试了所有可以在网上找到的解决方案。每次机器人都会识别出正确的频道,但它根本不会加入频道。这感觉像是一个如此简单的问题,所以我不确定如何使用与其他人类似的代码,但仍然无法正常工作。下面是我用于命令的代码。我省略了所有前缀和机器人设置代码,因为我发送的所有消息都正常工作。
@bot.command()
async def join(ctx):
if (ctx.author.voice):
channel = ctx.message.author.voice.channel
await ctx.send(f"Connecting to {channel}")
await channel.connect()
else: await ctx.send("Error")
asyncio.TimeoutError – Could not connect to the voice channel in time.
discord.ClientException – You are already connected to a voice channel.
discord.OpusNotLoaded – The opus library has not been loaded.
这些是机器人可以针对 VoiceChannel.connect()
引发的以下错误
如果您没有在应该收到错误时收到错误,这表明全局命令处理程序制作不当 on_command_error
。
您要么暂时删除它以在控制台上获取实际错误,要么在未处理错误的情况下通过输出错误来修复它。一种常见的做法是:
import sys
import traceback
@bot.event
async def on_command_error(ctx, error):
if x: # let x, y, z be isinstance check for error
# do this
elif y:
# do that
elif z:
# idk do something other than this and that
else:
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
在收到加入 VC 的实际错误后,检查这些:
asyncio.TimeoutError
:当您的机器人有
互联网连接不佳或您的 ping 高得离谱,足够高
不建立联系。
discord.ClientException
:可能需要检查机器人在启动时是否不在 VC 中,以及您是否启用了相关的公会意图和语音意图。
discord.OpusNotLoaded
:您的设备没有加载 libopus
,您可能需要根据您的使用情况在线搜索。如果您使用的是 ubuntu-based linux 发行版,您可以使用 sudo apt install libopus0
然后尝试查看它是否解决了您的问题。我不确定 windows
如果你看完上面的东西后还是想不通,让我知道你面临的EXACT ERROR
,其中包括回溯信息(将打印到您的控制台上的整个错误文本)
我是第一次玩机器人,但似乎无法让它加入语音频道。我知道 $join 命令正在运行,因为我的机器人会用正确的频道名称说“正在连接到 {channel}”,但它仍然无法连接。如果我不在语音频道中,机器人会正确地说“错误”。
我已经尝试了所有可以在网上找到的解决方案。每次机器人都会识别出正确的频道,但它根本不会加入频道。这感觉像是一个如此简单的问题,所以我不确定如何使用与其他人类似的代码,但仍然无法正常工作。下面是我用于命令的代码。我省略了所有前缀和机器人设置代码,因为我发送的所有消息都正常工作。
@bot.command()
async def join(ctx):
if (ctx.author.voice):
channel = ctx.message.author.voice.channel
await ctx.send(f"Connecting to {channel}")
await channel.connect()
else: await ctx.send("Error")
asyncio.TimeoutError – Could not connect to the voice channel in time.
discord.ClientException – You are already connected to a voice channel.
discord.OpusNotLoaded – The opus library has not been loaded.
这些是机器人可以针对 VoiceChannel.connect()
如果您没有在应该收到错误时收到错误,这表明全局命令处理程序制作不当 on_command_error
。
您要么暂时删除它以在控制台上获取实际错误,要么在未处理错误的情况下通过输出错误来修复它。一种常见的做法是:
import sys
import traceback
@bot.event
async def on_command_error(ctx, error):
if x: # let x, y, z be isinstance check for error
# do this
elif y:
# do that
elif z:
# idk do something other than this and that
else:
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
在收到加入 VC 的实际错误后,检查这些:
asyncio.TimeoutError
:当您的机器人有 互联网连接不佳或您的 ping 高得离谱,足够高 不建立联系。discord.ClientException
:可能需要检查机器人在启动时是否不在 VC 中,以及您是否启用了相关的公会意图和语音意图。discord.OpusNotLoaded
:您的设备没有加载libopus
,您可能需要根据您的使用情况在线搜索。如果您使用的是 ubuntu-based linux 发行版,您可以使用sudo apt install libopus0
然后尝试查看它是否解决了您的问题。我不确定 windows
如果你看完上面的东西后还是想不通,让我知道你面临的EXACT ERROR
,其中包括回溯信息(将打印到您的控制台上的整个错误文本)