Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用<;=>;=&书信电报;及>;在json-python中存储的变量discord.py上_Python_Json_Discord.py - Fatal编程技术网

如何使用<;=>;=&书信电报;及>;在json-python中存储的变量discord.py上

如何使用<;=>;=&书信电报;及>;在json-python中存储的变量discord.py上,python,json,discord.py,Python,Json,Discord.py,我有一些关于discord机器人的代码,它主要检查存储在json中的地精hp,如果它没有死,它会重放战斗序列。如果它是死的(json变量小于0),它应该停止序列。但事实并非如此。我检查了json,变量为-9,代码仍在循环。代码如下: @client.command(name='fight_01') @commands.cooldown(1, 30, commands.BucketType.user) async def fight_goblin(ctx): users = await g

我有一些关于discord机器人的代码,它主要检查存储在json中的地精hp,如果它没有死,它会重放战斗序列。如果它是死的(json变量小于0),它应该停止序列。但事实并非如此。我检查了json,变量为-9,代码仍在循环。代码如下:

@client.command(name='fight_01')
@commands.cooldown(1, 30, commands.BucketType.user)
async def fight_goblin(ctx):
    users = await get_bank_data()
    user = ctx.author
    rng1 = random.randint(1, 3)
    hp = users[str(user.id)]['hp']
    users[str(user.id)]['goblin_hp_json'] = 5
    with open("mainbank.json", 'w') as f:
      json.dump(users, f)
    rng2 = random.randint(1, 3)
    if rng1 == int('2'):
        await ctx.channel.send("You searched all around and couldn't find a goblin, try again next time :(")

    elif hp <= 0:
      await ctx.channel.send("You can't go adventuring with no hp you idiot")
        
            
   


     #Define this as async then remove the set hp to 5.
     #THen call it using the loop that the stackoverflow guy gave
    else:

      while True: #an infinite loop
        goblin_hp = users[str(user.id)]['goblin_hp_json']
        if users[str(user.id)]['hp'] <= 0:
          time.sleep(1.5)
          await ctx.channel.send('You died, better luck next time')
                    #If your command stop here you can use return, else use break to get out of the loop, here I will use break
          break
             
                  
    
        if goblin_hp <= 0:
          content3 = 'You did ' + rng5 + ' damage so you killed the goblin, well done'
          await ctx.channel.send(content3)
          rng6 = random.randint(20, 100)
          users[str(user.id)]['bank'] += rng6
          await asyncio.sleep(0.8) #use await asyncio.sleep(0.8) here rather
          content4 = 'You gained ' + str(rng6) + ' coins for defeating the goblin'
                        
          await ctx.channel.send(content4)
          with open("mainbank.json", 'w') as f:
            json.dump(users, f)
                    #Same here, I will use break
          break
        else:
          await goblin_fight_sequence(ctx)
                   #(This is where I want it to return to the first else: statement)
                   #Here you can use continue to repeat your loop, or just delete this part (an you will return to the first if). To match with your current code, I will use continue
                   #continue



async def goblin_fight_sequence(ctx):
  await ctx.channel.send('You encountered a wild goblin!')
        
  await asyncio.sleep(1.5) #use await asyncio.sleep(1.5) here rather
  rng3 = random.randint(1, 10)
  if rng3 == int('3') or int('4') or int('7'):
            
    user = ctx.author
    users = await get_bank_data()
    await ctx.channel.send('The goblin got the first hit and you lost 1hp')      

                
    users[str(user.id)]['hp'] -= int('1')
    dmg = users[str(user.id)]['max_damage'] #You forgot an ] here so I replaced it
    rng4 = random.randint(0, dmg)
    rng5 = str(rng4)
    await asyncio.sleep(1.5) #use await asyncio.sleep(1.5) here rather
    content1 = 'You did ' + rng5 + ' damage'
    users[str(user.id)]['goblin_hp_json'] -= int(rng4)
    with open("mainbank.json", 'w') as f:
      json.dump(users, f)
    users = await get_bank_data()
    await ctx.channel.send(content1)

@client.command(name='fight_01')
@冷却时间(1,30,commands.BucketType.user)
异步def战斗精灵(ctx):
用户=等待获取银行数据()
user=ctx.author
rng1=random.randint(1,3)
hp=users[str(user.id)]['hp']
用户[str(user.id)]['goblin\u hp\u json']=5
将open(“mainbank.json”,“w”)作为f:
json.dump(用户,f)
rng2=random.randint(1,3)
如果rng1==int('2'):
等待ctx.channel.send(“您四处搜索,但找不到地精,请下次再试:()

elif hp我通过添加users=wait get_bank_data()解决了这个问题这基本上打开了JSON的读取。我在while true下面添加了它。老实说,我不完全确定这是如何修复的,但它确实修复了它。

你的问题不是很清楚,你能在你的代码中添加
goblin\u fight\u sequence
函数吗请,因为我没有看到任何迹象表明
goblin\u hp
是修改的iedI已经添加了代码@BaptisteTo老实说,我没有足够的信息来做出一个好的回答…你必须输入输出(带有打印),如果有,则进行回溯,或其他有助于解决方案的事情…没有错误,代码只是从未停止循环,这意味着它总是转到“else”语句。这意味着if不起作用,我猜这很奇怪,因为您显式将用户设置为5,它应该是int。但是,在解析json时,有时可能会得到str而不是int。因此,您可能希望在某个时候将变量强制转换为int。这可以通过
打印(goblin\u hp,键入(goblin\u hp))
进行验证。否则,我真的不知道