0

I am relatively new to discord.py, While testin my !coinflip command ive come across alot of errors, Most of them i have fixed. But one keeps coming back.

@bot.event
async def on_message(ctx):
   if ctx.content.startswith('!coinflip'):
      embed = discord.Embed(title='Choose heads or tails:', color=0x00ff00)
      embed.set_footer(text='React with either  Tails or ️ Heads to make your choice\n. If you win 10  will be added.')
      msg = await ctx.channel.send(embed=embed)
      await msg.add_reaction('️')

      def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in ['', '️']
        reaction, user = await bot.wait_for('reaction_add', check=check)
        if str(reaction.emoji) == '️':
          choice = 'heads'
        else:
          choice = 'tails'

        flip = random.choice(['heads', 'tails'])
        if choice == flip:
          userid = ctx.author.id
          if os.path.isfile(f"{userid}.txt"):
            file = open(f"{userid}.txt")
            coins = int(file.read())
            newcoins = coins + 10
            file = open(f"{userid}.txt", 'r')

            file.close()
            file = open(f"{userid}.txt", "w")
            file.write(str(newcoins))
            file.close()
            await ctx.channel.send('You win! The coin landed on ' + flip + '.')
        else:
          await ctx.channel.send('You lose! The coin landed on ' + flip + '.')

This is the error shown when the code is run:

  File "c:\Users\rhan\OneDrive\Bureaublad\bscly bot\discordbot.py", line 41
    reaction, user = await bot.wait_for('reaction_add', check=check)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: 'await' outside async function

I have tried looking at other posts, Which surprisingly had about the same problem, But most of the answers either didnt work or were very vague.

  • 1
    There seems to be something very wrong with your indentation. Why the nested `check` function that will `return` at its first line, leaving the rest of the code to never be executed, for example? – Thierry Lathuille Feb 07 '23 at 15:53

1 Answers1

1

It doesn't seem like your check function is an async function. It says on the discord.py docs that you may only use await in async functions. https://discordpy.readthedocs.io/en/async/faq.html#where-can-i-use-await

Parkers
  • 26
  • 3
  • Hi, thanks for answering. After correcting my code i have found a new problem, All commands except !coinflip dont work. Have any explanation for that? – Ronghan Yury Feb 07 '23 at 16:20
  • 1
    @Ronghan Yury - your `on_message()` function overrides the behavior for ALL other commands, so you need to add a call to `bot.process_commands(message)` to handle everything you aren't handling in your function. See the answer to this post: https://stackoverflow.com/questions/49331096/why-does-on-message-stop-commands-from-working – nigh_anxiety Feb 07 '23 at 17:10