discord.py中的条件角色赋予



我是Python的新手。我正在为Discord服务器编写一个迷你游戏。我想制作一个命令,根据成员的角色更改成员的角色,并发送消息";你不能杀他,他是不朽的&";,如果论点提到一个没有角色之一的玩家:;3条生命"2寿命";,1寿命";,或";死了";。我试着使用一个列表;不在";操作员:

elif role.name not in ['3 lives', '2 lives', '1 life', 'Dead']:
await ctx.send("You can't kill {}, he/she is immortal".format(victim.mention))
break

但即使受害者没有这些角色,该函数也会被调用。这是我的代码:

# $kill command
@client.command(name = 'kill')
@commands.has_any_role('3 lives', '2 lives', '1 life')
@commands.cooldown(1, 57600, commands.BucketType.user)
async def kill(ctx, victim: discord.Member):
for role in victim.roles:
#if you try to kill yourself 
if victim == ctx.message.author: 
await ctx.reply("You can't kill yourself.")
kill.reset_cooldown(ctx)
break
#if you try to kill dead
elif role.name == 'Dead': 
await ctx.reply("You can't kill {}, this user is already dead.".format(victim.mention))
kill.reset_cooldown(ctx)
break

else:
#makes people have 2 lives
if role.name == '3 lives': 
newrole = discord.utils.get(victim.guild.roles, name='2 lives')
await victim.add_roles(newrole)                    
await victim.remove_roles(role)
await ctx.reply("You attacked {}, now this user has 2 lives.".format(victim.mention))
break
#makes people have 1 life
elif role.name == '2 lives': 
newrole = discord.utils.get(victim.guild.roles, name='1 life')
await victim.add_roles(newrole)
await victim.remove_roles(role)
await ctx.reply("You attacked {}, now this user has 1 life".format(victim.mention))
break
#makes people dead
elif role.name == '1 life': 
newrole = discord.utils.get(victim.guild.roles, name='Dead')
await victim.add_roles(newrole)
await victim.remove_roles(role)
await ctx.reply("You attacked {}, now this user is dead.".format(victim.mention))
break

更新1:应该通知不可能杀死没有角色的成员的条件"3寿命"2寿命"1寿命";或";死了"总是正确的,因为它检查了是否存在不属于可杀列表的其他角色,包括@everyone。

修复了它,它肯定有效。

check1 = discord.utils.get(victim.roles, name = '3 lives')
check2 = discord.utils.get(victim.roles, name = '2 lives')
check3 = discord.utils.get(victim.roles, name = '1 lives')
check4 = discord.utils.get(victim.roles, name = 'Dead')
check_all = [check1, check2, check3, check4]
#if you try to kill immortal
elif not any(check_all):
await ctx.reply("You can't kill {}, this user is immortal.".format(victim.mention))
attack.reset_cooldown(ctx)
break

最新更新