[英]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.