【问题标题】:Preventing bot from disconnecting users from normal voice channels?防止机器人断开用户与正常语音通道的连接?
【发布时间】:2019-12-15 22:52:34
【问题描述】:

我正在尝试让我的 discord 机器人断开转移到 AFK 频道的用户的连接。一切都很好,除了它试图断开刚刚进入谈话频道的用户,而不仅仅是当你转移到 AFK 频道时。我必须设置权限以不允许机器人移动或断开与这些频道的连接,因此它会不断拉起 Missing 权限。希望它忽略那些其他语音通道。

我不确定如何排除语音通道,所以我尝试了

if discord.VoiceChannel.id == id:
   return

无济于事。我尝试将机器人设置为不通过不和谐看到这些频道,但它仍然可以并且仍然试图断开人们的连接。

@client.event
async def on_voice_state_update(member = discord.Member, before = discord.VoiceState.channel, after = discord.VoiceState.afk):
    await member.move_to(channel = None, reason = None)

我猜这是基本的,但不确定如何忽略其他渠道。 我以为API说before = discord.VoiceState.channel指的是成员最近的语音频道,如果他们不在一个频道中,则没有,然后当他们进入AFK频道时,after = discord.VoiceState.afk会断开连接。我解释错了吗?我显然错过了一些东西

【问题讨论】:

    标签: python discord python-3.7 discord.py discord.py-rewrite


    【解决方案1】:

    APIon_voice_state_update 会给你三样东西:

    • 改变语音状态的member
    • VoiceState之前该成员做了某事。
    • VoiceState该成员做了某事之后。

    “做了某事”的意思是:

    • 会员加入语音频道。
    • 会员离开语音频道。
    • 该成员自己静音或耳聋。
    • 成员被其他人耳聋或静音。

    (也就是 API 所说的字面意思)

    您正在寻找的是VoiceState发生更改之后。在 API 中,它声明 VoiceState 有一个名为 afk 的属性,用于检查成员是否在 afk 通道中。

    您的代码将如下所示:

    @client.event
    async def on_voice_state_update(member, before, after):
        # If the user moved to the afk channel.
        if after.afk:
            # Do something about the user in afk channel.
    
        ### Use the codes below if you want to check if the user moved to a channel of the ID:
    
    
        if after.channel is None:
            # The user just simply left the channel. 
            # (Aka he did not switch to another voice channel.)
        elif after.channel.id == ID_OF_CHANNEL_HERE:
            # Do something about the user that just joined the channel with the respective ID
    

    【讨论】:

    • 似乎工作,虽然,当用户移动到 AFK 频道时,它会断开它们,然后说 if after.channel.id == 521373272475041817: AttributeError: 'NoneType' object has no attribute 'id' 编辑:我在我希望机器人忽略的语音频道下放一个返回
    • 如果用户只是离开频道,@BrandonWeeks after.channel 将为空。发生错误的原因是,即使用户只是离开了一个语音通道而没有加入另一个,也会调用此命令。 (并且您将用户踢出调用它的语音通道),我将相应地更新我的答案。
    【解决方案2】:

    阅读文档怎么样? Here it is 我的想法是你可以这样做

    if discord.VoiceClient.channel.id == id:
        return
    

    但我觉得这是不对的。

    【讨论】:

    • 你试过在discord.py官方discord服务器上问这个吗?与堆栈溢出相比,您将获得更好更快的答案。
    • 是的,我去过那里。您给我的参考是旧的 async dpy,我使用新的重写版本 - dpy 版本 1.2.3
    • 我以为我发的是新的,我以为他们更新了,但很抱歉浪费了您的时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2021-11-19
    • 2021-07-01
    • 2017-11-20
    • 2021-03-18
    • 2020-12-18
    相关资源
    最近更新 更多