简体   繁体   中英

Discord.py: Changing Nickname if user has specific role

I want to make my bot change someone's Nickname, if this user Has a specific role, so pretty much a Role Prefix system. So i tried it like this, but it keeps giving me the error "Missing Permissions". Here's my Code:

@client.event
@has_role("Admin")
async def on_member_update(nick, member):
    await member.edit(nick="Admin | ")

The bot simply doesn't have permissions to edit the member, it's too low on the hierarchy or the member it's the owner of the server. Also note that the has_role decorator only works on commands, it will not work on events

To make it work you should either use an if-statement or create your own decorator

  • if-statement
@client.event
async def on_member_update(before, after):
    role = discord.utils.get(before.guild.roles, name="Admin")
    if after in role.members:
        # Change the nick
  • custom decorator
from functools import wraps

def has_admin_role(coro):
    @wraps(coro)
    async def wrapper(before, after):
        role = discord.utils.get(before.guild.roles, name="Admin")
        if after in role.members:
            await coro(before, after)
    return wrapper


@client.event
@has_admin_role # Without calling it, it's hardcoded
async def on_member_update(before, after):
    # Edit the nick

Also note that the has_admin_role decorator will only work on the on_member_update event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM