如何为 on_message 命令添加别名?

时间:2021-05-12 22:32:30

标签: python command discord.py

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


@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)

3 个答案:

答案 0 :(得分:0)

您应该决定是要使用 on_message 事件还是命令扩展。使用 on_message will 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

答案 1 :(得分:0)

你应该使用 discord.py 提供给你的命令框架,这样使用起来更容易

只需在 WITH tab_start as (SELECT DISTINCT id, min(start) over (partition by id) as start FROM dt), tab_cancelled as (SELECT DISTINCT id, cancelled FROM dt WHERE cancelled IS NOT NULL), tab_rejoined as (SELECT DISTINCT dt.id, dt.start as rejoined FROM dt INNER JOIN tab_cancelled tc ON tc.id = dt.id AND dt.start > tc.cancelled) SELECT DISTINCT period_start, period_end, COUNT(DISTINCT dt1.id) as new, COUNT(DISTINCT dt2.id) as cancelled, COUNT(DISTINCT dt3.id) as rejoined FROM periods LEFT JOIN tab_start as dt1 ON dt1.start between period_start and period_end LEFT JOIN tab_cancelled as dt2 ON dt2.cancelled between period_start and period_end LEFT JOIN tab_rejoined as dt3 ON dt3.rejoined between period_start and period_end GROUP BY period_start, period_end 中放入一个类似 @client.command() 的名称,然后放入一个类似 name="your command name" 的数组

所以你的代码会像这样

aliases=['first','second']

答案 2 :(得分:0)

实际上,有一种更好的方法可以做到这一切。您可以使用:

@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"])