简体   繁体   中英

IndentationError: unexpected unindent

Started coding not too long ago, around a month or so. I'm currently coding a bot for Discord and everything was working fine until I got this error message when trying to run the bot after adding in new commands to it:

Traceback (most recent call last):
  File "C:\Users\Jeriel\Desktop\JerryBot\run.py", line 162, in main
    from musicbot import MusicBot
  File "C:\Users\Jeriel\Desktop\JerryBot\musicbot\__init__.py", line 1, in <modu
le>
    from .bot import MusicBot
  File "C:\Users\Jeriel\Desktop\JerryBot\music\bot.py", line 2094
    if __name__ == "__main__":
                             ^
IndentationError: unexpected unindent

It started after I added this in. I checked every line before these and I couldn't find a single unindent anywhere:

    async def kick(message,*args):
        """Kicks the specified user from the server"""
        if len(message.mentions) < 1:
            return False

        if message.channel.is_private:
            msg = await client.send_message(message.channel,'Users cannot be kicked/banned from private channels.')
            asyncio.ensure_future(message_timeout(msg, 40))
            return

        if not message.channel.permissions_for(message.server.get_member(client.user.id)).kick_members:
            msg = await client.send_message(message.channel, message.author.mention + ', I do not have permission to kick users.')
            asyncio.ensure_future(message_timeout(msg, 40))
            return

        members = []

        if not message.channel.is_private and message.channel.permissions_for(message.author).kick_members:
            for member in message.mentions:
                if member != message.author:
                    try:
                        await client.kick(member)
                        members.append(member.name)
                    except:
                        pass
                else:
                    msg = await client.send_message(message.channel, message.author.mention + ', You should not kick yourself from a channel, use the leave button instead.')
                    asyncio.ensure_future(message_timeout(msg, 40))
        else:
            msg = await client.send_message(message.channel, message.author.mention + ', I do not have permission to kick users, or this is a private message channel.')
            asyncio.ensure_future(message_timeout(msg, 40))

        msg = await client.send_message(message.channel,'Successfully kicked user(s): `{}`'.format('`, `'.join(members)))
        asyncio.ensure_future(message_timeout(msg, 60))

    @register('ban','@<mention users>',owner=True)

    async def ban(message,*args):
        """Bans the specified user from the server"""
        if len(message.mentions) < 1:
            return False

        if message.channel.is_private:
            msg = await client.send_message(message.channel,'Users cannot be kicked/banned from private channels.')
            asyncio.ensure_future(message_timeout(msg, 40))
            return

        if not message.channel.permissions_for(message.server.get_member(client.user.id)).ban_members:
            msg = await client.send_message(message.channel, message.author.mention + ', I do not have permission to ban users.')
            asyncio.ensure_future(message_timeout(msg, 40))
            return

        members = []

        if message.channel.permissions_for(message.author).ban_members:
            for member in message.mentions:
                if member != message.author:
                    try:
                        await client.ban(member)
                        members.append(member.name)
                    except:
                        pass
                else:
                    msg = await client.send_message(message.channel, message.author.mention + ', You should not ban yourself from a channel, use the leave button instead.')
                    asyncio.ensure_future(message_timeout(msg, 40))
        else:
            msg = await client.send_message(message.channel, message.author.mention + ', I do not have permission to ban users, or this is a private message channel.')
            asyncio.ensure_future(message_timeout(msg, 40))

        msg = await client.send_message(message.channel,'Successfully banned user(s): `{}`'.format('`, `'.join(members)))
        asyncio.ensure_future(message_timeout(msg, 30))

    @register('bans',alias='bannedusers')
    @register('bannedusers')


if __name__ == "__main__":
    bot = JerryBot()
    bot.run("---")

Your problem's here:

    @register('bans',alias='bannedusers')
    @register('bannedusers')


if __name__ == "__main__":
    bot = JerryBot()
    bot.run("---")

Decorator syntax requires a function definition under the @ line, which must be at the same indentation level as the @ . You can't have your if statement there, in other words. You need a function definition. The indentation is just the first issue it caught; if you indent your if statement, you get a different error.

I can't tell whether you included the @ lines in error here or if you omitted a function definition you meant to place there. Either write the function or remove the @ lines, as suits your purpose.

Is it possible that you mixed tabs and spaces? This is a common error for beginners with python. Technically, you can use either but cannot mix them. My solution for this (I use vim as an IDE) is to set the tab key to actually be 4 spaces in my .vimrc.

You could try re-indeting in vim by typing gg=G and pressing enter in command mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM