W
W
wz2021-03-25 13:49:35
Python
wz, 2021-03-25 13:49:35

SyntaxError: await outside function?

Again, I have the event code.
Writes an error: SyntaxError: await outside function.

# Event
@bot.event
async def on_message(message):
    await bot.process_commands(message)
    if message.author.id == bot.user.id:
        return

    if message.author.id in bot.blacklisted_users:
        return

    if f"<@!{bot.user.id}>" in message.content:
        data = cogs._json.read_json('prefixes')
        if str(message.guild.id) in data:
            prefix = data[str(message.guild.id)]

if __name__ == '__main__':
    for file in os.listdir(cwd+"/cogs"):
        if file.endswith(".py") and not file.startswith("_"):
            bot.load_extension(f"cogs.{file[:-3]}")

            if not message.author.bot and bot.user in message.mentions:
                embed = discord.Embed(title = f'Welcome!', timestamp = message.created_at, colour = discord.Colour.from_rgb(199, 182, 219),  description = f'''
                Hi {message.author.display_name}, my name **Osidium**!
                • My prefix - `o+`
                • Write the command `o+help` to find out all my features.
                • Want to know a little about me? Write `o-about`.
                • Need help on the bot, or found a bug/error? Visit our server: [Join](https://discord.gg/tYr5xeSS79)''')
                await message.reply(embed=embed)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2021-03-25
@shurshur

Well, it’s written right like that - await is outside the function. Can not be so. This code should be wrapped in a separate function and then run in the event loop via run_until_complete.

async def main():
  ... код ...

if __name__ == '__main__':
  loop = asyncio.get_event_loop()
  loop.run_until_complete(main())
  loop.close()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question