首页 > 解决方案 > 使用 python IndentationError 编码的 Discord bot:unindent 不匹配任何外部缩进级别

问题描述

File "main.py", line 16
await message.channel.send ('Sample text')
                                          ^

IndentationError: unindent 不匹配任何外部缩进级别

我试图制作一个不和谐的机器人,这又来了!

完整代码

   import discord
import os 
#discord client
client = discord.Client()
#event
@client.event
async def on_ready():
  print('Logged in as {0.user}'.format(client))
#event register
@client.event
async def on_message(message):
  if message.author == client.user:
    return
    if message.content.startswith('$vrise'):
   await message.channel.send ('Text')
  
  client.run(os.getenv('TOKEN'))

标签: python

解决方案


现在更新了您发布的代码,感谢您更新问题!

您的代码已取消缩进。将其更改为:

import discord
import os

# discord client
client = discord.Client()

# event
@client.event
async def on_ready():
    print('Logged in as {0.user}'.format(client))

# event register
@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('$vrise'): # This was one indent to much
        await message.channel.send('Text')

    client.run(os.getenv('TOKEN'))

请注意 if 语句以及它们的缩进如何与其范围相匹配。


推荐阅读