自定义帮助命令删除特定命令,如所有者或管理员命令
Custom Help Command removing specific commands like owner or admin commands
所以我按照几个教程制作了一个不和谐的机器人,我制作了一个自定义帮助命令,所以我不希望帮助命令显示一些命令,例如加载或卸载 cogs 命令,一些所有者或管理员命令。但是我不完全能够弄清楚该怎么做我检查了一些类似的帖子但是在其中创建了一个带有所有者命令的齿轮但是我的命令分布在几个齿轮上。我对 discord.py 有点陌生,所以请指导我。谢谢你!!提前
所以这是我的自定义帮助命令代码:-
import random
from typing import Optional
from discord import Embed
from discord.utils import get
from discord.ext.menus import MenuPages, ListPageSource
from discord.ext.commands import Cog
from discord.ext.commands import command
colors = [0xFFE4E1, 0x00FF7F, 0xD8BFD8, 0xDC143C, 0xFF4500, 0xDEB887, 0xADFF2F, 0x800000, 0x4682B4, 0x006400, 0x808080, 0xA0522D, 0xF08080, 0xC71585, 0xFFB6C1, 0x00CED1]
def syntax(command):
cmd_and_aliases = "|".join([str(command), *command.aliases])
params = []
for key, value in command.params.items():
if key not in ("self", "ctx"):
params.append(f"[{key}]" if "NoneType" in str(value) else f"<{key}>")
params = " ".join(params)
return f"`{cmd_and_aliases} {params}`"
class HelpMenu(ListPageSource):
def __init__(self, ctx, data):
self.ctx = ctx
super().__init__(data, per_page=5)
async def write_page(self, menu, fields=[]):
offset = (menu.current_page*self.per_page) + 1
len_data = len(self.entries)
embed = Embed(title="Help",
description="Welcome to the help dialog!",
colour= random.choice(colors))
embed.set_thumbnail(url=self.ctx.guild.me.avatar_url)
embed.set_footer(text=f"{offset:,} - {min(len_data, offset+self.per_page-1):,} of {len_data:,} commands.")
for name, value in fields:
embed.add_field(name=name, value=value, inline=False)
return embed
async def format_page(self, menu, entries):
fields = []
for entry in entries:
fields.append((entry.brief or "No description", f"-{syntax(entry)}"))
return await self.write_page(menu, fields)
class Help(Cog):
def __init__(self, bot):
self.bot = bot
self.bot.remove_command("help")
async def cmd_help(self, ctx, command):
embed = Embed(title=f"Help with `{command}`",
description=f"-{syntax(command)}",
colour=random.choice(colors))
embed.add_field(name="Command description", value=command.help)
await ctx.send(embed=embed)
@command(name="help", brief="Help", help="Gives the info of the commands.")
async def show_help(self, ctx, cmd: Optional[str]):
"""Shows this message."""
if cmd is None:
x = list(self.bot.commands)
menu = MenuPages(source=HelpMenu(ctx, x))
await menu.start(ctx)
else:
if (command := get(self.bot.commands, name=cmd)):
await self.cmd_help(ctx, command)
else:
await ctx.send("That command does not exist.")
@Cog.listener()
async def on_ready(self):
print("help ready")
def setup(bot):
bot.add_cog(Help(bot))
您可以在 @command
中使用 hidden
关键字参数,而不显示在您的帮助命令中包含 hidden=True
的命令:)
另外,你应该继承 commands.HelpCommand
instead. Here's a good tutorial on how to.
所以我按照几个教程制作了一个不和谐的机器人,我制作了一个自定义帮助命令,所以我不希望帮助命令显示一些命令,例如加载或卸载 cogs 命令,一些所有者或管理员命令。但是我不完全能够弄清楚该怎么做我检查了一些类似的帖子但是在其中创建了一个带有所有者命令的齿轮但是我的命令分布在几个齿轮上。我对 discord.py 有点陌生,所以请指导我。谢谢你!!提前
所以这是我的自定义帮助命令代码:-
import random
from typing import Optional
from discord import Embed
from discord.utils import get
from discord.ext.menus import MenuPages, ListPageSource
from discord.ext.commands import Cog
from discord.ext.commands import command
colors = [0xFFE4E1, 0x00FF7F, 0xD8BFD8, 0xDC143C, 0xFF4500, 0xDEB887, 0xADFF2F, 0x800000, 0x4682B4, 0x006400, 0x808080, 0xA0522D, 0xF08080, 0xC71585, 0xFFB6C1, 0x00CED1]
def syntax(command):
cmd_and_aliases = "|".join([str(command), *command.aliases])
params = []
for key, value in command.params.items():
if key not in ("self", "ctx"):
params.append(f"[{key}]" if "NoneType" in str(value) else f"<{key}>")
params = " ".join(params)
return f"`{cmd_and_aliases} {params}`"
class HelpMenu(ListPageSource):
def __init__(self, ctx, data):
self.ctx = ctx
super().__init__(data, per_page=5)
async def write_page(self, menu, fields=[]):
offset = (menu.current_page*self.per_page) + 1
len_data = len(self.entries)
embed = Embed(title="Help",
description="Welcome to the help dialog!",
colour= random.choice(colors))
embed.set_thumbnail(url=self.ctx.guild.me.avatar_url)
embed.set_footer(text=f"{offset:,} - {min(len_data, offset+self.per_page-1):,} of {len_data:,} commands.")
for name, value in fields:
embed.add_field(name=name, value=value, inline=False)
return embed
async def format_page(self, menu, entries):
fields = []
for entry in entries:
fields.append((entry.brief or "No description", f"-{syntax(entry)}"))
return await self.write_page(menu, fields)
class Help(Cog):
def __init__(self, bot):
self.bot = bot
self.bot.remove_command("help")
async def cmd_help(self, ctx, command):
embed = Embed(title=f"Help with `{command}`",
description=f"-{syntax(command)}",
colour=random.choice(colors))
embed.add_field(name="Command description", value=command.help)
await ctx.send(embed=embed)
@command(name="help", brief="Help", help="Gives the info of the commands.")
async def show_help(self, ctx, cmd: Optional[str]):
"""Shows this message."""
if cmd is None:
x = list(self.bot.commands)
menu = MenuPages(source=HelpMenu(ctx, x))
await menu.start(ctx)
else:
if (command := get(self.bot.commands, name=cmd)):
await self.cmd_help(ctx, command)
else:
await ctx.send("That command does not exist.")
@Cog.listener()
async def on_ready(self):
print("help ready")
def setup(bot):
bot.add_cog(Help(bot))
您可以在 @command
中使用 hidden
关键字参数,而不显示在您的帮助命令中包含 hidden=True
的命令:)
另外,你应该继承 commands.HelpCommand
instead. Here's a good tutorial on how to.