0
votes

I am learning how to use discord.py and I want to make bot feature that give role when we join particular voice channel and removes the same role when user leaves the channel

@client.event
async def on_voice_state_update():
1
hi, is there a command to grant statusIronMan

1 Answers

3
votes

on_voice_state_update gives you after and before arguments, here's how to check when a member joined and left a voice channel

async def on_voice_state_update(member, before, after):
    if before.channel is None and after.channel is not None:
        # member joined a voice channel, add the roles here
    elif before.channel is not None and after.channel is None:
        # member left a voice channel, remove the roles here

To add roles, first you need to get a discord.Role object, then you can do member.add_roles(role)

role = member.guild.get_role(role_id)

await member.add_roles(role)

To remove a role is the same, but .remove_roles

await member.remove_roles(role)

EDIT:

async def on_voice_state_update(member, before, after):
    channel = before.channel or after.channel

    if channel.id == some_id:
        if before.channel is None and after.channel is not None:
            # member joined a voice channel, add the roles here
        elif before.channel is not None and after.channel  is None:
            # member left a voice channel, remove the roles here

Reference: