我正在尝试制作一个名为";"效用";对于我的不和机器人,然而我的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命令本身在机器人的任何其他部分都不起作用
错误为;
Traceback (most recent call last):
File "C:UsersMy Microsoft UserAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagesdiscordextcommandsbot.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:UsersMy Microsoft UserDesktopDiscord Bot CodingDTOG Botcogsutility.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:UsersMy Microsoft UserDesktopDiscord Bot CodingDTOG Botbot.py", line 61, in <module>
bot.load_extension(f'cogs.{filename[:-3]}')
File "C:UsersMy Microsoft UserAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagesdiscordextcommandsbot.py", line 653, in load_extension
self._load_from_module_spec(spec, name)
File "C:UsersMy Microsoft UserAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagesdiscordextcommandsbot.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)