我正在制作一个不和机器人,这样我就可以为联盟5v5制作锦标赛。这是我为组建团队而制定的命令:
rows, cols = 5, 2
teamArr = [["Empty" for x in range(rows)] for y in range(cols)]
teamNum = ""
playerName = ""
lane = ""
@bot.command()
async def team(context, name, num, pos): #member: discord.Member
teamEmbed = discord.Embed(title="Teams", color=0x6a0dad)
playerName = str(name)
teamNum = str(num)
lane = str(pos)
#team1
if (teamNum == 1):
if (lane == 'Top'):
teamArr[0][0] = str(playerName)
elif (lane == 'Jungle'):
teamArr[0][1] = str(playerName)
elif (lane == 'Mid'):
teamArr[0][2] = str(playerName)
elif (lane == 'Adc'):
teamArr[0][3] = str(playerName)
elif (lane == 'Support'):
teamArr[0][4] = str(playerName)
#team2
elif (teamNum == 2):
if (lane == 'Top'):
teamArr[1][0] = str(playerName)
elif (lane == 'Jungle'):
teamArr[1][1] = str(playerName)
elif (lane == 'Mid'):
teamArr[1][2] = str(playerName)
elif (lane == 'Adc'):
teamArr[1][3] = str(playerName)
elif (lane == 'Support'):
teamArr[1][4] = str(playerName)
teamResult = str(teamArr[0][0]) + "n" + str(teamArr[0][1]) + "n" + str(teamArr[0][2]) + "n" + str(teamArr[0][3]) + "n" + str(teamArr[0][4])
teamEmbed.add_field(name="Team 1", value=teamResult, inline=True)
teamResult2 = str(teamArr[1][0]) + "n" + str(teamArr[1][1]) + "n" + str(teamArr[1][2]) + "n" + str(teamArr[1][3]) + "n" + str(teamArr[1][4])
teamEmbed.add_field(name="Team 2", value=teamResult2, inline=True)
await context.send(embed=teamEmbed)
我制作了teamArr,所有元素都是";空的"我正在设置teamArr[0][0]=playerName,以便我将字符串";空的";以玩家的名字命名。然而,每当我把它打印出来时,它都会保持为";空的";并且似乎不起作用。我在这里可能做错了什么?
您正在设置teamNum = str(num)
,但随后比较teamNum == 1
和teamNum == 2
。因此,这两种比较都不正确,代码会跳到最后。您可以通过执行int(teamNum) == 1
或teamNum == '1'
之类的操作来解决此问题。
旁注:
- 您应该使用转换器来省去转换自己的麻烦:
async def team(ctx, name: str, num: int, lane: str)
。也不需要多次str()
所有内容 - 通过执行
'n'.join(teamArr[0])
,可以更容易地打印出状态 - 为了获得更好的风格,您可以使用
teamArr[teamNum-1]
,而不是编写两次