简体   繁体   English

Discord.py:如果用户具有特定角色,则更改昵称

[英]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另请注意, has_role装饰器仅适用于命令,不适用于事件

To make it work you should either use an if-statement or create your own decorator要使其工作,您应该使用 if 语句或创建自己的装饰器

  • if-statement if 语句
@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.另请注意, has_admin_role装饰器仅适用于on_member_update事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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