【问题标题】:How do I put discord.py help command in an embed?如何将 discord.py 帮助命令放入嵌入中?
【发布时间】:2021-01-13 11:45:05
【问题描述】:

所以,我目前有一个使用 discord.py 运行的 discord 机器人,如您所知,discord.py 带有自己的帮助命令(所以我不必自己制作)。它非常有用,我将我的命令分成了 cogs/categories。它确实有助于简化,因为现在我不必编写自己的帮助命令。

问题是,当我运行帮助命令时,它出现在一个巨大的代码块中,如下所示: 我听到一些用户抱怨说这在视觉上并不吸引人,当我添加更多命令时,它会填满屏幕。是否有简单的方法(无需编写我自己的帮助命令)将所有这些移动到嵌入中?也许复制此帮助命令的输出,并将其移动到嵌入中?如果没有,没关系,我会编写自己的帮助命令,但我只是想寻找一种简单的方法来做到这一点,而不会弄脏我的代码。与往常一样,提前感谢您。

如果需要,这里是我的代码示例:

import discord
from discord.ext import commands, tasks

TOKEN = "INSERT TOKEN HERE"
client = commands.Bot(command_prefix="wurf ", case_insensitive=True)

#Utility Category
class Utility(commands.Cog):
    def __init__(self, client):
        self.client = client
        
    @commands.command(
        help="Shows the ping/latency of the bot in miliseconds.",
        brief="Shows ping."
    )
    async def ping(self, ctx):
        if round(client.latency * 1000) <= 50:
            embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0x44ff44)
        elif round(client.latency * 1000) <= 100:
            embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0xffd000)
        elif round(client.latency * 1000) <= 200:
            embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0xff6600)
        else:
            embed=discord.Embed(title="PING", description=f":ping_pong: Pingpingpingpingping! The ping is **{round(client.latency *1000)}** milliseconds!", color=0x990000)
        await ctx.send(embed=embed)
client.add_cog(Utility(client))
client.run(TOKEN)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    你必须用Bot.help_command覆盖默认的帮助命令

    这是一个简单的嵌入实现,我继承自MinimalHelpCommand

    class MyHelpCommand(commands.MinimalHelpCommand):
        async def send_pages(self):
            destination = self.get_destination()
            e = discord.Embed(color=discord.Color.blurple(), description='')
            for page in self.paginator.pages:
                e.description += page
            await destination.send(embed=e)
    
    client.help_command = MyHelpCommand()
    

    【讨论】:

      【解决方案2】:

      正如我之前的 2 个答案所提到的,您必须将默认帮助命令设置为无。这是在您创建机器人时从一开始就完成的。
      至于嵌入,您将不得不自己做一些工作。这是我的机器人的一个示例,但请注意,这不一定是最佳做法 - 我不知道,但它对我有用。

      # When you create your bot, add this in the arguments
      bot = commands.Bot(prefix='.', help_command=None)
      bot.remove_command('help')
      
      # My sample help command:
      @bot.command()
      async def help(ctx, args=None):
          help_embed = discord.Embed(title="My Bot's Help!")
          command_names_list = [x.name for x in bot.commands]
      
          # If there are no arguments, just list the commands:
          if not args:
              help_embed.add_field(
                  name="List of supported commands:",
                  value="\n".join([str(i+1)+". "+x.name for i,x in enumerate(bot.commands)]),
                  inline=False
              )
              help_embed.add_field(
                  name="Details",
                  value="Type `.help <command name>` for more details about each command.",
                  inline=False
              )
      
          # If the argument is a command, get the help text from that command:
          elif args in command_names_list:
              help_embed.add_field(
                  name=args,
                  value=bot.get_command(args).help
              )
      
          # If someone is just trolling:
          else:
              help_embed.add_field(
                  name="Nope.",
                  value="Don't think I got that command, boss!"
              )
      
          await ctx.send(embed=help_embed)
      

      你可以从GitHub repository看到完整的代码。

      【讨论】:

        【解决方案3】:

        一个简单的方法是使用“PrettyHelp”python 模块:

        1. 您可以使用 pip 使用pip install -U discord-pretty-help 安装它
        2. 然后导入from pretty_help import DefaultMenu, PrettyHelp
        3. 然后你设置菜单按钮并替换默认的帮助菜单:
        menu = DefaultMenu('◀️', '▶️', '❌') # You can copy-paste any icons you want.
        bot.help_command = PrettyHelp(navigation=menu, color=discord.Colour.green()) 
        # The color can be whatever you want, including normal color codes, 
        # I just like the discord green personally.
        

        我相信这必须在主 bot 文件而不是 cog 中完成,它最终看起来像这样:screenshot

        【讨论】:

          猜你喜欢
          • 2021-02-28
          • 1970-01-01
          • 2021-06-04
          • 2021-04-21
          • 2020-11-06
          • 2021-09-16
          • 2021-12-10
          • 2020-09-19
          • 2021-08-20
          相关资源
          最近更新 更多