首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将commands.Bot()中的内置帮助命令模拟为bot.tree?

如何将commands.Bot()中的内置帮助命令模拟为bot.tree?
EN

Stack Overflow用户
提问于 2022-10-05 21:53:12
回答 2查看 235关注 0票数 -1

我想要做的是触发commands.Bot()包含的bot命令帮助(例如,/help返回所有已注册的斜杠命令,/help <command>返回该斜杠命令的描述)。

我的代码的一部分

代码语言:javascript
运行
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-06 00:06:06

做了一些实验,找到了一个可行的解决方案:

代码语言:javascript
运行
复制
@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)
票数 -2
EN

Stack Overflow用户

发布于 2022-10-05 22:12:13

您的意思是同步,它是任何app_command工作所必需的步骤,包括斜杠命令。

最简单的方法是创建一个前缀命令(也称为常规命令),执行await bot.tree.copy_global_to(guild=guild),其中会社是包含要将其复制到的行会的对象。这确保您指定的公会上有一个命令树的副本。如果每个公会树没有问题,也可以使用await bot.tree.sync()进行全局复制。

您的斜杠命令没有正确执行,但即使正确,如果您不将命令树与不和谐同步,则将不会在bot向上时显示斜杠命令。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73966680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档