首页 > 解决方案 > Bot 无法连接到语音通道 - discord.py 重写

问题描述

我正在使用 discord.py 重写制作一个不和谐的机器人,但我最近遇到了一个问题。

我已发出加入用户语音频道的命令。问题是,当我在本地 PC 上运行它时,我的命令运行良好,但现在我试图在树莓派上运行它,它在连接到语音通道时失败。

我已经尝试安装所有依赖项,但我无法让它工作。命令代码:

@bot.command()
async def join(ctx):
    channel = ctx.message.author.voice.channel
    voice = get(bot.voice_clients, guild=ctx.guild)

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()

    await ctx.send("I joined the channel!")

没有提出任何例外。

标签: pythondiscorddiscord.pydiscord.py-rewrite

解决方案


您正在使用错误的方式连接到语音通道。尝试使用此代码。

它识别用户的位置并在该语音通道中连接。

   @bot.command(name='join', invoke_without_subcommand=True)
    async def join(ctx):
       destination = ctx.author.voice.channel
       if ctx.voice_state.voice:
         await ctx.voice_state.voice.move_to(destination)
         return
    
       ctx.voice_state.voice = await destination.connect()
       await ctx.send(f"Joined {ctx.author.voice.channel} Voice Channel")

推荐阅读