how to make a bot that gives role when we join a particular voice channel and removes when left

﹥>﹥吖頭↗ 提交于 2021-01-07 02:49:31

问题


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:


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:

  • on_voice_state_update
  • Guild.get_role
  • Member.add_roles
  • Member.remove_roles


来源:https://stackoverflow.com/questions/65446794/how-to-make-a-bot-that-gives-role-when-we-join-a-particular-voice-channel-and-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!