我在 discord.py 中搜索货币代码有什么问题

时间:2021-04-06 18:13:52

标签: discord.py

我需要帮助修复我的代码

@client.command
@commands.cooldown(5, 20, commands.BucketType.user)
async def search(ctx):
    await open_account(ctx.author)
    user = ctx.author
    users = await get_bank_data()
    peen = users[str(user.id)]["wallet"]
    tu = random.randint(19, 157)
    rand = random.choice(hs)
    embed54=discord.Embed(title='Search in...', description=f'{rand}, {rand}, {rand}')
    embed54.add_footer('Do !beg for money as well!')

    await ctx.send(embed=embed54)

    await client.wait_for(rand)
    def check(m):
        return m.author == ctx.author and m.channnel == ctx.channel
        embed53=discord.Embed(title='You Found...', description=f'${tu} From {ctx.message}')
        await ctx.send(embed=embed53)
        fa = tu
        peen += fa
  

为什么不起作用 SyntaxError: 'await' 在异步函数之外 我需要帮助 请它每次都会出错,所以请帮助我 也是啊boi

2 个答案:

答案 0 :(得分:1)

由于我不知道 hs 是什么意思或者你是如何定义它的,所以我一次省略了整个内容并用一个简单的数字代替它,只是为了我的测试。

事先澄清:您在创建嵌入、check 函数以及使用 await 函数的渲染方式和一些拼写错误时出错。

这个方法很适合我:

@client.command()
@commands.cooldown(5, 20, commands.BucketType.user)
async def search(ctx):
    def check(m):
        return m.author == ctx.author and m.channel == ctx.message.channel # New check

    await open_account(ctx.author)
    user = ctx.author
    users = await get_bank_data()

    hs = # DEFINE WHAT IT IS! <<<

    peen = users[str(user.id)]["wallet"]
    tu = random.randint(19, 157)
    rand = random.choice(hs)
    embed54 = discord.Embed(title='Search in...', description=f'{rand}, {rand}, {rand}')
    embed54.set_footer(text='Do !beg for money as well!') # SET footer

    await ctx.send(embed=embed54)

    response = await client.wait_for('message', check=check) # Wait for MESSAGE, add check
    if response.content == rand: # I assume you want to get the author say one of the numbers
        embed53 = discord.Embed(title='You Found...', description=f'${tu} From {ctx.message.content}')
        await ctx.send(embed=embed53)
        fa = tu
        peen += fa
    else: # If the number is not in the embed
        await ctx.send("You did not respond correctly.")

我们做了什么?

  • 重新构建您的支票,因为它是 m.channelNOT m.channnel,这里只是一个小错误
  • 添加了一个“真正的”页脚,因为 embed54.add_footer 不存在,它是 embed54.set_footer(text='', icon_url='')
  • wait_for 来自消息作者的 'message' 并且如果 rand(您必须定义,因为我不知道 hs 是什么)不在他的回答他不会得到你定义的任何东西。 (if/else 语句)
  • ctx.message 替换 ctx.message.content 否则它会给出一个 longer "text" 而不仅仅是 search

! 由于您的代码中有很多错误,我建议您再次仔细查看docs

答案 1 :(得分:0)

我确实这样做了,而且效果很好:

@commands.cooldown(5, 20, commands.BucketType.user) 异步定义搜索(ctx): 定义检查(m): return m.author == ctx.author and m.channel == ctx.message.channel # 新检查

await open_account(ctx.author)
user = ctx.author
users = await get_bank_data()

peen = users[str(user.id)]["wallet"]
tu = random.randint(19, 157)
rand = random.choice(hs)
rand1 = random.choice(hs)
rand2 = random.choice(hs)
embed54 = discord.Embed(title='Search in...', description=f'{rand}, {rand1}, {rand2}')
embed54.set_footer(text='Do !beg for money as well!') # SET footer

await ctx.send(embed=embed54)

response = await client.wait_for('message', check=check) # Wait for MESSAGE, import check
if response.content == rand: # I assume you want to get the author say one of the numbers
    embed53 = discord.Embed(title='You Found...', description=f'${tu} From {ctx.message.content}')
    await ctx.send(embed=embed53)
    earnings = tu
    users[str(user.id)]["wallet"] += earnings
    with open("mainbank.json","w") as f:
      json.dump(users,f)
      return True
else:
  response = await client.wait_for('message', check=check)
  if response.content == rand1: # I assume you want to get the author say one of the numbers
      embed53 = discord.Embed(title='You Found...', description=f'${tu} From {ctx.message.content}')
      await ctx.send(embed=embed53)
      earnings = tu
      users[str(user.id)]["wallet"] += earnings
      with open("mainbank.json","w") as f:
        json.dump(users,f)
        return True
  else:
      response = await client.wait_for('message', check=check) # Wait for MESSAGE, import check
      if response.content == rand1: # I assume you want to get the author say one of the numbers
          embed53 = discord.Embed(title='You Found...', description=f'${tu} From {ctx.message.content}')
          await ctx.send(embed=embed53)
          earnings = tu
          users[str(user.id)]["wallet"] += earnings
      
      
          with open("mainbank.json","w") as f:
            json.dump(users,f)
            return True