Discord.py 如何测试成员是否在角色字典中具有特定角色?
Discord.py how can I test if a member has certain role in a dictionary of roles?
AdminRoles = ["Moderation","Administration","Emperor"]
@client.command()
async def Commands(ctx):
member = ctx.author
if AdminRoles in member.roles:
ShowCommand = discord.Embed(
title = "Moderation Commands",
description = "All commands",
colour = discord.Colour.red()
)
await ctx.send(embed = ShowCommand)
else:
ShowCommand = discord.Embed(
title = "Member Commands",
description = "All commands",
colour = discord.Colour.red()
)
await ctx.send(embed = ShowCommand)
我确实修复了上面的代码,因为当我输入命令时,它一直显示正常的播放器命令,并且应该显示 Mod 命令。
您正在查看列表 AdminRoles
是否在 member.roles 内,整个 列表为:
if ["a","b","m"] in members.roles:
但是您希望 AdminRoles 中的一项在 members.role 中,因此您需要类似的东西:
test = [e for e in AdminRoles if e in members.roles]
if len(test) > 0:
doTheRightModeratorThing()
else:
doTheRightCommonerThing()
(最后检查 adminRoles 中是否至少有一个角色在 member.roles 中)
在您的代码中,您做到了 if AdminRoles in member.roles:
。这意味着如果成员拥有所有 AdminRoles
。所以你可以这样改变你的代码:
AdminRoles = ["Moderation","Administration","Emperor"]
@client.command()
async def Commands(ctx):
member = ctx.author
for role in member.roles:
if role.name in AdminRoles:
ShowCommand = discord.Embed(
title = "Moderation Commands",
description = "All commands",
colour = discord.Colour.red()
)
await ctx.send(embed = ShowCommand)
return
ShowCommand = discord.Embed(
title = "Member Commands",
description = "All commands",
colour = discord.Colour.red()
)
await ctx.send(embed = ShowCommand)
在此代码中,如果会员有任何 AdminRoles
,将发送审核命令。
AdminRoles = ["Moderation","Administration","Emperor"]
@client.command()
async def Commands(ctx):
member = ctx.author
if AdminRoles in member.roles:
ShowCommand = discord.Embed(
title = "Moderation Commands",
description = "All commands",
colour = discord.Colour.red()
)
await ctx.send(embed = ShowCommand)
else:
ShowCommand = discord.Embed(
title = "Member Commands",
description = "All commands",
colour = discord.Colour.red()
)
await ctx.send(embed = ShowCommand)
我确实修复了上面的代码,因为当我输入命令时,它一直显示正常的播放器命令,并且应该显示 Mod 命令。
您正在查看列表 AdminRoles
是否在 member.roles 内,整个 列表为:
if ["a","b","m"] in members.roles:
但是您希望 AdminRoles 中的一项在 members.role 中,因此您需要类似的东西:
test = [e for e in AdminRoles if e in members.roles]
if len(test) > 0:
doTheRightModeratorThing()
else:
doTheRightCommonerThing()
(最后检查 adminRoles 中是否至少有一个角色在 member.roles 中)
在您的代码中,您做到了 if AdminRoles in member.roles:
。这意味着如果成员拥有所有 AdminRoles
。所以你可以这样改变你的代码:
AdminRoles = ["Moderation","Administration","Emperor"]
@client.command()
async def Commands(ctx):
member = ctx.author
for role in member.roles:
if role.name in AdminRoles:
ShowCommand = discord.Embed(
title = "Moderation Commands",
description = "All commands",
colour = discord.Colour.red()
)
await ctx.send(embed = ShowCommand)
return
ShowCommand = discord.Embed(
title = "Member Commands",
description = "All commands",
colour = discord.Colour.red()
)
await ctx.send(embed = ShowCommand)
在此代码中,如果会员有任何 AdminRoles
,将发送审核命令。