繁体   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?

我在我的 discord 机器人中创建了一个自动删除消息功能,当我从审查列表命令中删除单词时搁浅了。 因为我要从列表中删除的单词在列表中,所以 msg 被机器人删除了。 我想通过在我的 on message delete 消息事件中添加一个额外的 if 来规避这个问题,该事件检查用户是否具有 Admin 角色或 Mod 角色。 我想要的结果是,如果他们确实有这个角色,它将返回并且 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)```

您获得角色的方式主要有两种, discord.utils.get ,还有guild.get_role 无论如何,这里都是两个例子

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:

这通过 id 获取角色并检查成员是否具有角色

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:

这通过名称获取角色并检查成员是否具有角色

或者它也可以在一行中完成(看起来很糟糕)

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

这利用了 utils.get 如果未找到该事物则返回 None 的事实。 因此,它正在搜索成员的角色以查找名为 Admin/Mod 的角色,如果找不到,则该成员没有该角色

不推荐的一些更短的方式

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

这会将成员拥有的每个角色的 id 角色与 mod 和 admin 角色的角色 id 进行比较

暂无
暂无

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

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