Discord py,如何使用角色metnion删除权限
我想通过使用角色提及从服务器中具有相应角色的用户中删除角色。 例如,“%remove”_role@TEAM_A'从具有“团队A”角色的人员的角色中删除“团队A”。 我在谷歌上努力搜索,但找不到答案或方法,所以我自己做了,但没有完成,所以我在这里写了一个问题Discord py,如何使用角色metnion删除权限,discord,discord.py,Discord,Discord.py,我想通过使用角色提及从服务器中具有相应角色的用户中删除角色。 例如,“%remove”_role@TEAM_A'从具有“团队A”角色的人员的角色中删除“团队A”。 我在谷歌上努力搜索,但找不到答案或方法,所以我自己做了,但没有完成,所以我在这里写了一个问题 第一名%rmrole团队名称 第二。检查您输入的团队名称是否在“角色列表”中 三,。从服务器上的用户中删除输入的团队名称的角色 这是我的密码 @bot.command() async def rmrole(ctx, team_name):
- 第一名%rmrole团队名称
- 第二。检查您输入的团队名称是否在“角色列表”中
- 三,。从服务器上的用户中删除输入的团队名称的角色
@bot.command()
async def rmrole(ctx, team_name):
key = 0
role = get(ctx.guild.roles, name=team_name)
team_list = []
print(role)
# typo check
role_list = ["TEAM_A", "TEAM_B", "TEAM_C", "TEAM_D"]
if role in role_list:
type_error = 1
else:
type_error = 0
# remove_role
empty = True
if type_error == 1:
for member in ctx.guild.members:
if role in member.roles:
await member.remove_roles(role)
empty = False
if empty:
await ctx.send("anyone has this role.")
else:
await ctx.send("check the typos."
我担心如果我这样写,用户会收到很多警报。
因此,在主体中,我在文本中输入角色的名称,并以扫描代码中角色的方式编写
已搜索并找到“#删除_角色”部分。
代码已执行,但角色未删除
我想知道哪里错了,我需要帮助来做我想要的。你的错误很简单:
role_list = ["TEAM_A", "TEAM_B", "TEAM_C", "TEAM_D"]
if role in role_list:
type_error = 1
else:
type_error = 0
此代码将始终失败并导致type_error=0
,因为role
是一个discord.role.role
类而不是字符串。这意味着将通过get
获得的角色与表示其名称的字符串进行比较总是会失败:因此,删除角色的代码的第二部分永远不会被访问。不然就行了
相反,您希望:
if role.name in role_list:
type_error = 1
else:
type_error = 0
或者更好的是,这是:
if role is None:
return await ctx.send("Role doesn't exist")
…因为我个人不太明白你的代码的意义:如果role=get(ctx.guild.roles,name=team\u name)
失败(即该角色不存在),role
将是None
,你可以轻松地检查它,而不是将其与硬编码列表进行比较
if role is None:
return await ctx.send("Role doesn't exist")