[英]Custom help command cog in discord py v2
所以,在较早的 discord py 版本中,我有一个单独的 cog for help 命令和下面的命令
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 中的其他 cog 一样调用这个 cog
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]}')
它适用于除帮助命令 cog 之外的所有 cog。 有人可以告诉我如何纠正这个错误
试着放
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
在on_ready
加载齿轮
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.