如何解决" IndentationError"?

时间:2017-12-28 09:01:15

标签: python image bots discord discord.py

我正在制作一个Discord机器人并尝试让它发送图像。

这些都是我的进口商品:

 import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
import random
import time
import ftplib

然后我有了这段代码:

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

然后我有了这个,当我启动run.py时,它会在应用程序中显示一些打印件:

@bot.event
async def on_ready():
    print("I'm running on: " + bot.user.name)
    print ("With the ID: " + (bot.user.id))
    print("The commands are \n" + "#ping \n #pong \n #info (@user) \n #embedtest \n #hello \n #hug \n #kill \n ")

这是我的图片代码,所以这段代码应该从我的run.py程序所在的文件夹中弹出一张图片。

@bot.command(pass_context=True)
async def image(ctx):
myImage = open('UDST.png', 'rb') 
await bot.send_file(myImage)

但是当我运行它时,它给了我这个我不理解的错误。

C:\Users\david>"C:\Users\david\Desktop\UDST Bot\Udst bot.py"
  File "C:\Users\david\Desktop\UDST Bot\Udst bot.py", line 105
    await bot.send_file(myImage)
                               ^
IndentationError: unindent does not match any outer indentation level

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

为了回应wright所评论的问题,可能是因为你没有在你的功能之后缩进(你得到缩进错误)。

这个

@bot.command(pass_context=True)
async def image(ctx):
myImage = open('UDST.png', 'rb') 
await bot.send_file(myImage)

应该是这个

@bot.command(pass_context=True)
async def image(ctx):
    myImage = open('UDST.png', 'rb') 
    await bot.send_file(myImage)

如果这不起作用,您可以尝试一些发送discord.py docs上的图像的示例。其中一个示例显示您只需传递文件名

即可发送图像
await client.send_file(channel, 'your_image_file_here.png')

希望能解决你的问题!