【问题标题】:missing arguments message in discord不和谐中缺少参数消息
【发布时间】:2020-06-02 04:09:09
【问题描述】:

所以我用不同的帮助菜单制作了一个帮助命令。就像 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)')

【问题讨论】:

    标签: discord.py-rewrite


    【解决方案1】:

    您可以像这样提供默认 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)")
    

    参考资料:

    【讨论】:

    • 很高兴听到这个消息!如果您也不介意accepting the answer 对您有帮助,因为它可以帮助其他人在遇到相同问题时找到解决方案。编码愉快,机器人好运:^)
    • 哦,是的,谢谢(-:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多