簡體   English   中英

如何檢查用戶是否在角色列表中具有特定角色? (discord.py)

[英]How to check if a user has a specific role in a list of roles? (discord.py)

當嘗試運行此代碼時,沒有任何反應。 順便說一句,沒有錯誤。 我得到了一個角色列表來讓它工作,但它沒有。

rolelist = [822428355554312212,
            822728446059741218,
            823814683973779488]

check = ''

if rolelist in user.roles:
    check += 'Bot Developer'

你可以做

if any(role.id in rolelist for role in user.roles):
   ...

嗯,它有什么作用?

每次迭代,它從用戶那里獲取一個角色,獲取 ID,並檢查 ID 是否在rolelist列表數組中。 如果角色 id 在數組中,它會停止循環並執行它應該在 if 語句中執行的 function。

祝你有美好的一天!

還有另一種方式。 你可以使用內置的。

鏈接到文檔: https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.has_any_role

文檔中的示例:

@bot.command()
@commands.has_any_role('Library Devs', 'Moderators', 492212595072434186)
async def cool(ctx):
    await ctx.send('You are cool indeed')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM