Y
Y
Yossi_i2021-02-02 21:09:05
Python
Yossi_i, 2021-02-02 21:09:05

How to make the bot remove the role of a participant in a certain voice channel?

I wrote a bot in the discord, I want to make it shoot the role of the participant in a certain voice channel.
Wrote like this:

@client.event
async def on_voice_state_update(member, before, after):
    role1 = discord.utils.get(member.guild.roles, id = 804852310966140978)

if before.channel and not after.channel:
        if before.channel.id == 804901448931475506:
            await member.remove_roles(role1)


But, the fact is that when you switch to another voice channel, the role remains. Tell me what to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
ettychel, 2021-02-16
@ettychel

Your condition is written
IF before.channel exists AND after.channel does NOT exist.
It turns out that this condition will work only when the user does not go to another channel, but exits the voice channel altogether, so the role is not removed when switching to another channel.
It would be more correct to check only the presence of before.channel and compare its id with the desired one, and if it matches, then remove the role, and you don’t care where the user left.
Those.

@client.event
async def on_voice_state_update(member, before, after):
    role1 = discord.utils.get(member.guild.roles, id = 804852310966140978)

if before.channel:
        if before.channel.id == 804901448931475506:
            await member.remove_roles(role1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question