Coding Errors/Discord Bot

I have a ton of old discord servers that went inactive, so I’m in the process of cleaning them up so I can start over. I’m creating a discord bot that will mass ban/kick everyone and delete all the channels, so I don’t have to do it manually, but I keep running into SyntaxError: ‘await’ outside function whenever I try to run my code. What would you guys suggest I do because I’m very new to this?

Repl link:

code snippet

python doesn’t allow toplevel async, you have to have it in a function


Adding on to what @bigminiboss said, await is only available within functions that are asynchronous. You will need to refactor your code into functions and declare the function as asynchronous by doing something like such:

import asyncio

async def someFunction():
    await doSomething()

you don’t need asyncio package to have async functions, just to run them top level. Also, how is this really adding, that is basically what I said

1 Like

Sorry, I’m don’t really use async functionality in my projects.

When it comes to code-related questions, I like to provide a visual example to show how it can be done. “You have to have it in a function” doesn’t seem like enough of an explanation IMO. Considering the OP didn’t know this in the first place, they probably won’t know how to do it without googling.

1 Like

I always like to check the documentation:

Also, client.ban and client.kick are outdated methods.
Use ctx.guild.ban() and ctx.guild.kick() instead.

Also, don’t forget that Discord use Intents now:

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True #As you are only messing with members, there is no need for others intents

bot = commands.Bot(command_prefix='!', intents=intents)

#write your code here but remember to use the ctx method'put-your-bot-token-here')

1 Like

do not! put your bot token in a secret named token. then import os and use['token'])

ok thank you then :smiley:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.