2017-08-25 66 views
0

不久前開始編碼,大約一個月左右。我目前編碼不和諧殭屍,一切工作正常,直到我嘗試添加新命令之後運行機器人時,此錯誤消息:IndentationError:意外的未加註

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 

它開始我加入這之後我檢查每一行之前,我無法找到一個單一的任何地方:

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("---") 
+1

錯誤消息指向'if __name__ ==「__main __」:'。爲什麼該行相對於代碼的其餘部分是縮進的? – Blorgbeard

+0

您是否複製並粘貼了您的代碼,或將其輸入到自己的代碼中?如果您複製並粘貼它,可能是由於您在某些地方使用製表符縮進以及在其他位置使用縮進縮進的問題。這樣做總是搞笑的東西... – colelemonz

+0

@Blorgbeard我認爲OP希望使用'if __name__ =='__main __「:'塊作爲主要功能,如果是的話那麼它必須是非縮進 – colelemonz

回答

2

你的問題在這裏:

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


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

裝飾語法要求下@線函數的定義,它必須有相同的縮進水平爲@。換句話說,你不能在那裏陳述if陳述。你需要一個函數定義。縮進只是它發現的第一個問題;如果您縮進您的if聲明,則會得到不同的錯誤。

我不能告訴你是否在這裏包含了@行錯誤,或者你忽略了一個你想放在那裏的函數定義。要麼寫入函數,要麼刪除@行,以適合您的目的。

+0

謝謝爲了那個原因! 我這樣做了,它似乎修復了縮進問題,但是一旦我這樣做,它就出現了: 'File「C:\ Users \ Jeriel \ Desktop \ JerryBot \ musicbot \ bot.py」,line 2018 異步高清踢(消息,*參數): ^ TabError:在壓痕 C使用不一致的製表和空格:\用戶\ Jeriel \桌面\ JerryBot>' 上的任何想法如何解決這一問題? –

0

是否有可能您混合製表符和空格?這是python初學者的常見錯誤。從技術上講,你可以使用但不能混合它們。我的解決方案(我使用vim作爲IDE)是在我的.vimrc中設置tab鍵實際上是4個空格。

您可以嘗試在vim中重新鍵入gg=G並在命令模式下按回車鍵。