如何使用discord.py机器人在加入VC时赋予用户角色,并在离开VC时将其删除

时间:2019-01-07 06:37:51

标签: python python-3.x discord discord.py

我正在尝试创建一个discord机器人,该机器人在用户加入语音通道时赋予其角色,并在离开时取消该角色。我知道on_voice_state_update以及如何赋予用户角色,但我不知道如何让什么用户加入频道以赋予他们角色。

现在我的代码是对答案的略微修改的版本 How to use discord.py event handler on_voice_state_update to run only when a user joins a voice channel

@client.event
async def on_voice_state_update(before, after ):
    if before.voice.voice_channel is None and after.voice.voice_channel is not None:
        for channel in before.server.channels:
            if channel.name == 'general':
                await client.send_message(channel, "User joined")

elif before.voice.voice_channel is not None and after.voice.voice_channel is None: for channel in before.server.channels: if channel.name == 'general': await client.send_message(channel, "User left");

2 个答案:

答案 0 :(得分:0)

afterMember对象,因为它在语音状态更改后“现在”存在。那将是您传递给add_roles

的成员对象
@client.event
async def on_voice_state_update(before, after ):
    role = discord.utils.get(after.server.roles, name="YOUR ROLE NAME")
    if not before.voice.voice_channel and after.voice.voice_channel:
        await client.add_roles(after, role)
    elif before.voice.voice_channel and not after.voice.voice_channel:
        await client.remove_roles(after, role)

答案 1 :(得分:0)

晚了三个月,但是对于以后偶然发现它的人来说,这是rewite(1.0)分支版本。

_llmulo