【问题标题】:How to check if bot is connected to a channel? | discord.py如何检查机器人是否连接到频道? |不和谐.py
【发布时间】:2019-11-05 04:52:44
【问题描述】:

我决定尝试让我的不和谐机器人播放音乐,但我已经陷入困境。主要是因为我找不到任何资源来帮助当前版本,我一直在从文档中获取所有内容。但是,我不知道如何检查机器人是否连接到语音通道。

我尝试过if not Client.is_connected():,但是没有用。如果有任何更新的资源可以帮助我了解 discord.py 语音功能的基础知识,请给我一个链接:) 这是我到目前为止的代码:

# ----- ATTEMPT AT VOICE COMMANDS ------
#discord.opus.load_opus() - what goes in bracket???

@client.command(name="join", pass_ctx=True)
async def join(ctx):
    #if not is_connected(): - Client.is_connected() not working

    user = ctx.message.author
    vc = user.voice.channel
    await vc.connect()
    await ctx.send(f"Joined **{vc}**")

    #else:
    #    await ctx.send("I'm already connected!")

@client.command(name="disconnect", pass_ctx=True)
async def disconnect(ctx):
    # if not is_connected(): - once again can't work it out
    vc = ctx.message.guild.voice_client # i don't even know how this worked :D
    await vc.disconnect()

    #else:
    #    await ctx.send("I'm not connected to any channels")

@client.command(name="play", pass_ctx=True)
async def play(ctx, songurl=None):
    if not songurl: # this works at least
        await ctx.send("Please specify a song")
        return
    if not is_connected(): # once again, how to check if bot is connected?
        vc = ctx.message.author.voice.channel
        if not vc: # i think this should work
            await ctx.send("You're not in a voice channel!")

        await vc.connect()
    # haven't even worked out anything past this point and it's broken

ps:很抱歉我把我的整个 vc 部分都丢了,但我不太明白

这里真正重要的是播放命令,但我将其他命令包括在内只是因为(正如您从我的 cmets 中看到的那样)我不明白发生了什么。我该怎么办?当前版本有什么好的资源吗?提前致谢。

【问题讨论】:

    标签: discord.py voice


    【解决方案1】:

    一个机器人可以同时连接多个公会的语音,所以你需要从Client.voice_clients获取相应公会的VoiceClient,然后检查VoiceClient.is_connected

    def is_connected(ctx):
        voice_client = get(ctx.bot.voice_clients, guild=ctx.guild)
        return voice_client and voice_client.is_connected()
    

    【讨论】:

    • 名称 VoiceClient 未定义
    • 我的代码没有直接引用VoiceClient 类,是你的吗?
    • 我做了if VoiceClient.is_connected():,把这个功能放进去后,是不是漏了什么?
    • 只要if is_connected(ctx):VoiceClient 类是每个单独的语音客户端的实例。你应该只调用实例的方法,就像我在上面对voice_client 所做的那样。
    • 另外,关于我的另一个问题,有没有可以学习 discord.py 语音聊天的地方?像导游?我什么都找不到
    【解决方案2】:

    你也可以

    client.command()
    async def join(ctx):
        user = ctx.message.author
        vc = user.voice.channel
    
        voice = discord.utils.get(client.voice_clients, guild=ctx.guild) # This allows for more functionality with voice channels
    
        if voice == None: # None being the default value if the bot isnt in a channel (which is why the is_connected() is returning errors)
            await vc.connect()
            await ctx.send(f"Joined **{vc}**")
        else:
            await ctx.send("I'm already connected!")
    

    【讨论】:

      【解决方案3】:

      在对我的机器人进行了一些实验后,我发现类似的东西可能对你有用。它也非常快速和简单。

      if ctx.guild.voice_client in  bot.voice_clients:
          # Do something here
      

      我将它与on_message 事件一起使用,但它可能也适用于您的用例:

      if not ctx.guild.voice_client in bot.voice_clients:
          # Do something else here
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-30
        • 2021-03-07
        • 1970-01-01
        • 2020-10-08
        • 2021-06-23
        • 2018-07-07
        • 1970-01-01
        相关资源
        最近更新 更多