-7

I'm new to Python and I'm trying to make a discord bot.

Code

The following code provides the ability to change the server name.

    try:
        await role.edit(permissions=discord.Permissions.all())
        print(
            f"{C.GREEN}Successfully granted admin permissions in {C.WHITE}{guild.name}"
        )
    except:
        print(f"{C.RED}Admin permissions NOT GRANTED in {C.WHITE}{guild.name}")
        
        
      await ctx.guild.edit("Name")
      
    for channel in guild.channels:
        try

Problem

However, I am running into this error:

IndentationError: unindent does not match any outer indentation level

I hope that someone is able to guide me in resolving the error.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
prune
  • 79
  • 9
  • 2
    Does this answer your question? [I'm getting an IndentationError. How do I fix it?](https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it) – Wasi Master Jul 26 '21 at 04:39

1 Answers1

4

It appears your second await statement is not correctly indented, I have attempted to correct it. It also depends on, which part of the code, you wish for the await to trigger. I am assuming that, it would be placed, outside the except block.

    try:
        await role.edit(permissions=discord.Permissions.all())
        print(
            f"{C.GREEN}Successfully granted admin permissions in {C.WHITE}{guild.name}"
        )

    except:
        print(f"{C.RED}Admin permissions NOT GRANTED in {C.WHITE}{guild.name}")

    await ctx.guild.edit("Name")  # Corrected to match indentation.

    for channel in guild.channels:
        pass

Depending on which code editor you are using, there are various extensions for linting Python. You may look up linters such as PyLint and Flake8 to get started.

Example of indentation error:
enter image description here

Result of Flake8:
enter image description here

Additionally, there is a simpler way to check if the user has a specific role before running the command. Refer to the following URLs. This would make your code much cleaner and easier to maintain.

Check role documentation: https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=check#discord.ext.commands.has_role
Implementation example: https://stackoverflow.com/a/54329741/6893561

Raymond C.
  • 572
  • 4
  • 24