矛盾中缺少参数消息

时间:2020-06-01 19:40:01

标签: discord.py-rewrite

因此,我正在使用不同的帮助菜单进行帮助命令。像mee6一样。但我想添加一条消息,因为没有给出任何参数。怎么做?这是我现在拥有的:

@bot.command(name='help')
async def help(ctx, *, content):
    if content == ('Moderation'):
        await ctx.send(moderationmenu)
    if content == ('fun'):
        await ctx.send(funmenu)
    if content == None:
        await ctx.send('please provide an argument (Moderation / fun)')

1 个答案:

答案 0 :(得分:0)

您可以提供默认的arg值,如下所示:

@bot.command(name="help")
async def help(ctx, *, content = None):
    if not content: # more pythonic way of checking a variable is None or not
        await ctx.send("Please provide an argument (moderation / fun)")
    elif content.lower() == "fun": # brackets not necessary
        await ctx.send(funmenu)
    elif content.lower() == "moderation": # makes it case insensitive
        await ctx.send(moderationmenu)
    else:
        await ctx.send("Sorry, I didn't recognise that category. Please choose (moderation / fun)")

快速编辑-如果您将菜单分类为单独的变量,则可以将它们映射为字典:

@bot.command(name="help")
async def help(ctx, *, content = None):
    menus = {"fun": funmenu, "moderation": moderationmenu}
    if not content:
        await ctx.send("Please provide an argument (moderation / fun)")
    else:
        try:
            await ctx.send(menus[content.lower()])
        except KeyError:
            await ctx.send("Sorry, I didn't recognise that category. Please choose (moderation / fun)")

参考: