我想要做的是触发commands.Bot()
包含的bot命令帮助(例如,/help
返回所有已注册的斜杠命令,/help <command>
返回该斜杠命令的描述)。
我的代码的一部分
class MyHelp(commands.HelpCommand):
async def send_bot_help(self, mapping):
if self.context.guild.id != master_guild:
return
names = [command.name for command in bot.tree.get_commands()] # iterating through the commands objects getting names
available_commands = "\n".join(names) # joining the list of names by a new line
embed = discord.Embed(title=f"Commands ({len(names)}):",description=available_commands)
embed.set_footer(text=f" {prefix}help <command> (e.g {prefix}help {random.choice(names)})")
await self.context.send(embed=embed)
async def send_command_help(self, command): # I am in the process of making this work for slash commands
if self.context.guild.id != master_guild:
return
if len(command.description) == 0:
desc = "No description provided"
else:
desc = command.description
e = discord.Embed(title=command.name,description=desc)
e.set_footer(text="Usage: "+command.usage)
await self.context.send(embed=e)
bot = commands.Bot(command_prefix=prefix, intents=discord.Intents.all(), help_command=MyHelp()) #help_command obviously not needed
@bot.event
async def on_ready():
try:
synced = await bot.tree.sync()
print(f"Synced {len(synced)} command{'s' if len(synced) > 1 else ''}.")
except Exception as e:
print(f"Error syncing commands: {e}")
@bot.tree.command(description = "Sends help")
@app_commands.describe(command = "Enter command")
async def help(ctx: discord.Interaction, command:str=None):
await ctx.channel.send_help(ctx.command)
发布于 2022-10-06 00:06:06
做了一些实验,找到了一个可行的解决方案:
@bot.tree.command(description = "Sends help")
@app_commands.describe(command = "The command to get help for")
async def help(interaction: discord.Interaction, command:str=None):
if command is not None:
for c in bot.tree.get_commands(): # Iterates through registered slash commands
if c.name == command: # Checks if command iterated is the command requested
command = c
break
else:
continue
try:
embed = discord.Embed(title=command.name,description=command.description)
await interaction.response.send_message(embed=embed)
except AttributeError: # if command is not found
e = discord.Embed(title="Error:",description=f"Command {command} not found", color=0xFF0000)
await interaction.response.send_message(embed=e)
return
# ran if command is None
names = [command.name for command in bot.tree.get_commands()] # iterates through all the names of registered slash commands and compiles them into a list
available_commands = "\n".join(names) # joins the list of names with a new
embed = discord.Embed(title=f"Commands ({len(names)}):",description=available_commands)
embed.set_footer(text=f" /help <command> (e.g /help {random.choice(names)})")
await interaction.response.send_message(embed=embed)
发布于 2022-10-05 22:12:13
您的意思是同步,它是任何app_command工作所必需的步骤,包括斜杠命令。
最简单的方法是创建一个前缀命令(也称为常规命令),执行await bot.tree.copy_global_to(guild=guild)
,其中会社是包含要将其复制到的行会的对象。这确保您指定的公会上有一个命令树的副本。如果每个公会树没有问题,也可以使用await bot.tree.sync()
进行全局复制。
您的斜杠命令没有正确执行,但即使正确,如果您不将命令树与不和谐同步,则将不会在bot向上时显示斜杠命令。
https://stackoverflow.com/questions/73966680
复制相似问题