discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.utility' raised an error: IndentationError: expected an indented block utility.py line 11

discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.utility' raised an error: IndentationError: expected an indented block utility.py line 11

我正在尝试为我的 discord 机器人制作一个名为“Utility”的 cog,但是我的 ping 命令有问题。

我的机器人的其余部分,包括齿轮加载系统,工作正常。

齿轮的代码是;

import discord
from discord.ext import commands


class Utility(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def ping(self, ctx):
        author = ctx.message.author

        embed = discord.Embed(
            colour = discord.Colour.green()
        )

        embed.add_field(name='Pong!', value=(f'{round(bot.latency * 1000)}ms'), inline=False)

        await ctx.send(embed=embed)

def setup(bot):
    bot.add_cog(Utility(bot))

此外,如果需要,ping 命令本身在 bot 的任何其他部分都不起作用。

错误是;

Traceback (most recent call last):
  File "C:\Users\My Microsoft User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\ext\commands\bot.py", line 596, in _load_from_module_spec
    spec.loader.exec_module(lib)
  File "<frozen importlib._bootstrap_external>", line 779, in exec_module
  File "<frozen importlib._bootstrap_external>", line 916, in get_code
  File "<frozen importlib._bootstrap_external>", line 846, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\My Microsoft User\Desktop\Discord Bot Coding\DTOG Bot\cogs\utility.py", line 11
    author = ctx.message.author
    ^
IndentationError: expected an indented block

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\My Microsoft User\Desktop\Discord Bot Coding\DTOG Bot\bot.py", line 61, in <module>
    bot.load_extension(f'cogs.{filename[:-3]}')
  File "C:\Users\My Microsoft User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\ext\commands\bot.py", line 653, in load_extension
    self._load_from_module_spec(spec, name)
  File "C:\Users\My Microsoft User\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\ext\commands\bot.py", line 599, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.utility' raised an error: IndentationError: expected an indented block (utility.py, line 11)

编辑 2020 年 10 月 10 日

因为人们在这里遇到问题是我的齿轮装载机;

import discord
import random
import asyncio
import time
import youtube_dl
import functools
import itertools
import math
import sys
import traceback
import pycountry
import os
from functools import partial
from async_timeout import timeout
from discord.ext import commands
from random import choice

bot = commands.Bot(command_prefix = 'dtog!')
bot.remove_command('help')

@bot.event
async def on_ready():
    print('The bot is online!')
    ...
    bot.loop.create_task(status_task())

async def status_task():
    while True:
        await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="DTOG code"))
        await asyncio.sleep(5)
        await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="dtog!help!"))
        await asyncio.sleep(5)

@bot.command()
async def load(ctx, extension):
    id = str(ctx.author.id)
    if id == '721029142602056328':
        bot.load_extension(f'cogs.{extension}')
    else:
        await ctx.send("You can't do this!")

        embed.add_field(name='Pong!', value=(f'{round(self.bot.latency * 1000)}ms'), inline=False)

@bot.command()
async def unload(ctx, extension):
    id = str(ctx.author.id)
    if id == 'MY DISCORD ID':
        bot.unload_extension(f'cogs.{extension}')
    else:
        await ctx.send("You can't do this!")

@bot.command(pass_context=True)
async def shutdown(ctx):
    id = str(ctx.author.id)
    if id == 'MY DISCORD ID':
        await ctx.send('Shutting down...')
        await ctx.bot.logout()
    else:
        await ctx.send("You can't do this!")

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        bot.load_extension(f'cogs.{filename[:-3]}')

bot.run('MY TOKEN')

我找到了解决问题的方法。

    @commands.command()
    async def ping(self, ctx):
    author = ctx.message.author

    embed = discord.Embed(
        colour = discord.Colour.dark_blue()
    )

    embed.add_field(name='Pong!', value=(f'{round(bot.latency * 1000)}ms'), inline=False)

    await ctx.send(embed=embed)