discord.py 中的有条件角色赋予
Conditional role-giving in discord.py
我是 Python 的新手。我正在为 Discord 服务器编写一个迷你游戏。我想发出一个命令,根据他们的角色改变成员的角色,并发送消息“你不能杀死 {mention},他是不朽的。”,如果参数提到没有角色之一的玩家: “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
我是 Python 的新手。我正在为 Discord 服务器编写一个迷你游戏。我想发出一个命令,根据他们的角色改变成员的角色,并发送消息“你不能杀死 {mention},他是不朽的。”,如果参数提到没有角色之一的玩家: “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