所以,在早期的discord py版本中,我有一个单独的cog用于帮助命令和follow命令
import discord
from discord.ext import commands
class HelpCommand(commands.HelpCommand):
colour = 0x0dd2ff
@commands.Cog.listener()
async def on_ready(self):
print("Cog loaded - HelpCommand.py")
def footer(self):
return f"{self.clean_prefix}{self.invoked_with} [command] for more information"
def get_command_signature(self, command):
return f"```{self.clean_prefix}{command.qualified_name} {command.signature}```"
async def send_cog_help(self, cog):
embed = discord.Embed(title=f"**{cog.qualified_name}** commands", colour=self.colour)
if cog.description:
embed.description = cog.description
filtered = await self.filter_commands(cog.get_commands(),sort=True)
for command in filtered:
embed.add_field(name=command.qualified_name, value=command.short_doc or "No description")
embed.set_footer(text=self.footer())
await self.get_destination().send(embed=embed)
async def send_command_help(self, command):
embed = discord.Embed(title=command.qualified_name, colour=self.colour)
if command.help:
embed.description = command.help
embed.add_field(name="Signature", value=self.get_command_signature(command))
embed.set_footer(text=self.footer())
await self.get_destination().send(embed=embed)
async def send_bot_help(self, mapping):
embed = discord.Embed(title="Bot Commands", colour=self.colour)
description = self.context.bot.description
if description:
embed.description = description
for cog, commands in mapping.items():
if not cog:
continue
filtered = await self.filter_commands(commands, sort=True)
if filtered:
value = "\t".join(f"`{i.name}`"for i in commands)
embed.add_field(name=cog.qualified_name, value=value)
embed.set_footer(text=self.footer())
await self.get_destination().send(embed=embed)
async def setup(bot):
bot.help_command = HelpCommand()
我曾经像 main.py 中的其他齿轮一样使用
来调用这个齿轮for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
将库更新为discord py v2后,我将cogs调用命令更改为
async def load():
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
适用于除帮助命令齿轮之外的所有齿轮。有人可以告诉我如何纠正这个错误吗
尝试放置
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
在
on_ready
加载齿轮