Question:
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?
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:
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.
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
bot.run('put-your-bot-token-here')