首页 > 解决方案 > 我在 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' outside async function 我需要帮助,请它每次都会出错,所以也帮助我,是的 boi

标签: discord.py

解决方案


Since I don't know what hs means or how you defined it I omitted the whole thing once and replaced it with a simple number just for my test.

To clarify beforehand: You had errors in creating an embed, in your check function and in your way of rendering with await functions and some typos.

This method works fine for me:

@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.")

What did we do?

  • Re-built your check as it is m.channel and NOT m.channnel, just a little typo here
  • Added a "real" footer, as embed54.add_footer does not exist, it is embed54.set_footer(text='', icon_url='')
  • wait_for a 'message' from the author of the message and if rand, which you have to define as I do not know what hs is, is not in his answer he will not get whatever you defined. (if/else statements)
  • Replaced ctx.message with ctx.message.content as otherwise it will give out a longer "text" than just search

! Since there were many mistakes in your code I would recommend to have a closer look at the docs again !


推荐阅读