【问题标题】:How to add aliases to on_message command?如何为 on_message 命令添加别名?
【发布时间】:2021-08-03 07:07:40
【问题描述】:

所以我制作了这个帮助命令部分,如果用户键入 %help moderation,它会列出审核命令。但我想要别名或禁用区分大小写。


@client.event
async def on_message(message):
    if message.content.startswith('%help moderation'):
      await message.channel.send("%ban, %kick, %purge <limit>, %warn, %membercount, %botcount")

然后,这是我实际嵌入的帮助命令:

@client.command()
async def help(ctx):
    embed=discord.Embed(title="Commands", 
    description="Here are all the commands to use:", color=0x0618C1)
    embed.add_field(name="Moderation", value="Moderation Commands", inline=False)
    embed.add_field(name="Fun", value="General Fun Commands", inline=True)
    embed.add_field(name="Random", value="Commands that do random things", inline=True)
    await ctx.send(embed=embed)

【问题讨论】:

  • 您是否正在寻找向帮助命令添加额外选项(参数)/使其不区分大小写的方法?
  • 首先,我收到一个错误,提示 embed 未分类,是的,我正在寻找一种添加参数的方法。

标签: python command discord.py


【解决方案1】:

实际上,有一种更好的方法来完成所有这些工作。您可以使用:

@client.group(invoke_without_command = True) # the invoke_without_command = True states that %help will be executed without a subcommand like %help moderation.
async def help(ctx):
    # your code here 

而不是client.command() 创建一个所谓的命令组,它允许您使用带有扩展名的命令。为了放入一个子命令,在你的情况下“适度”,你需要使用:

@help.command()
async def moderation(ctx):
    # whatever should happen if someone types in "%help moderation".

这是 discord.py 中一个非常酷的功能,它可以防止您每次都使用 on_message 事件。

这就是你的代码最终的样子:

@client.group()
async def help(ctx):
    embed=discord.Embed(title="Commands", 
    description="Here are all the commands to use:", color=0x0618C1)
    embed.add_field(name="Moderation", value="Moderation Commands", inline=False)
    embed.add_field(name="Fun", value="General Fun Commands", inline=True)
    embed.add_field(name="Random", value="Commands that do random things", 
    inline=True)
    await ctx.send(embed=embed)

@help.command()
async def moderation(ctx):
    await ctx.send("%ban, %kick, %purge <limit>, %warn, %membercount, %botcount")

回到您的问题:您现在可以通过将“别名”作为参数放入@help.command() 中来为子命令添加别名,如下所示:

@help.command(aliases = ["first", "second", "third"])

【讨论】:

    【解决方案2】:

    您应该使用 discord.py 提供给您的命令框架,以便更轻松地使用它

    只需在@client.command() 中输入类似name="your command name" 的名称,然后输入类似aliases=['first','second'] 的数组

    所以你的代码会是这样的

    @client.command(name="your command name" , aliases=['first','second'])
    async def def_name(ctx):
         #command
    

    【讨论】:

      【解决方案3】:

      您应该决定是要使用on_message 事件还是命令扩展。使用on_messagewill conflict with command processing if you don't configure it correctly。 如果您没有充分的理由使用on_message,我建议您坚持使用命令扩展并阅读documentation 以定义参数。

      如果你想禁用区分大小写,你可以在初始化 Bot 对象时这样做,如果你想提供自定义帮助命令,最好禁用内置的。

      bot = commands.Bot(command_prefix='%',
                         case_insensitive=True, # Disable command case sensitivity
                         help_command=None) # Disable built in help command
      

      【讨论】:

        猜你喜欢
        • 2020-08-17
        • 1970-01-01
        • 2021-01-24
        • 1970-01-01
        • 1970-01-01
        • 2021-06-17
        • 2022-12-02
        • 1970-01-01
        • 2018-12-16
        相关资源
        最近更新 更多