简体   繁体   English

我如何还可以检查成员是否具有特定角色是 if 语句? 仅当您具有特定角色时才需要 elif 执行?

[英]how can I also check if a member has a specific role is an if statement? need the elif to only execute if you have a specific role?

I am making a autodelete message feature in my discord bot and have ran aground when I got to the remove words from censor list command.我在我的 discord 机器人中创建了一个自动删除消息功能,当我从审查列表命令中删除单词时搁浅了。 because the word I want to remove from the list is on the list the msg get's deleted by the bot.因为我要从列表中删除的单词在列表中,所以 msg 被机器人删除了。 I want to circumvent this by adding an additional if inside my on message delete message event that checks if the user has the Admin role or the Mod role.我想通过在我的 on message delete 消息事件中添加一个额外的 if 来规避这个问题,该事件检查用户是否具有 Admin 角色或 Mod 角色。 my desired outcome is that if they do have the role it will return and the msg delete will never execute我想要的结果是,如果他们确实有这个角色,它将返回并且 msg delete 将永远不会执行

       if any(word in message.content for word in censorlist):
        if message.author == client.user:
            
            return
        
        elif
            
            return

        else:

            await message.delete()
    await client.process_commands(message)```

There are mainly 2 ways you for you to get the role, discord.utils.get , and there is guild.get_role .您获得角色的方式主要有两种, discord.utils.get ,还有guild.get_role Anyways here are examples for both无论如何,这里都是两个例子

get_role (Needs id) get_role (需要 id)

# Somewhere above all the if statements
admin_role = message.guild.get_role(id_here)
mod_role = message.guild.get_role(id_here)
# And then in your elif
elif mod_role in message.author.roles or admin_role in message.author.roles:

This gets the roles via id and checks if the member has the roles这通过 id 获取角色并检查成员是否具有角色

utils.get (needs name) utils.get (需要名称)

# Somewhere above all the if statements
admin_role = discord.utils.get(message.guild.roles, name="Admin")
mod_role = 
# And then in your elif
elif mod_role in message.author.roles or admin_role in message.author.roles:

This gets the roles via name and checks if the member has the role这通过名称获取角色并检查成员是否具有角色

Or it can be done in one line too (looks bad)或者它也可以在一行中完成(看起来很糟糕)

elif discord.utils.get(message.author.roles, name="Mod") or discord.utils.get(messages.author.roles, name="Admin"):

This takes advantage of the fact that utils.get returns None if the thing is not found.这利用了 utils.get 如果未找到该事物则返回 None 的事实。 So it is searching the member's roles for a role named Admin/Mod, if it isn't found then member does not have the role因此,它正在搜索成员的角色以查找名为 Admin/Mod 的角色,如果找不到,则该成员没有该角色

Some even shorter way that is not recommended不推荐的一些更短的方式

elif any(role.id in [mod_role_id, admin_role_id] for role in message.author.roles):

This compares the id role of each role the member has to the role ids of the mod & admin roles这会将成员拥有的每个角色的 id 角色与 mod 和 admin 角色的角色 id 进行比较

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

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