Ошибка команды Python - PullRequest
       8

Ошибка команды Python

0 голосов
/ 03 июля 2018

Здравствуйте, я пытаюсь добавить {0.subcommand_passed} в await bot.send_message, но я получаю сообщение об ошибке, так как добавить, чтобы поймать неправильную команду, переданную в ответ.

Так что если члены набирают ?helol вместо ?hello, бот должен ответить helol is a wrong command

@bot.event
async def on_command_error(error, ctx):
    if isinstance(error, commands.CommandNotFound):
        await bot.send_message(ctx.message.channel, "**Wrong command ** " + ctx.invoked_with)

и в подкоманде, если члены набирают ?cool girl вместо ?cool boy, бот должен ответить girl is a wrong sub command

@bot.group(pass_context=True)
async def cool(ctx):
    if ctx.invoked_subcommand is None:
        if ctx.subcommand_passed:
            await bot.say("**Wrong sub command: **{}".format(ctx.subcommand_passed))
        else:
            await bot.say("**Subcommand required**. {0.author.mention}".format(ctx.message))

@cool.command(pass_context=True)
async def boy(ctx):
    msg = "Hello ...".format(ctx.message)
    await bot.say(msg)

1 Ответ

0 голосов
/ 03 июля 2018

Используйте Context.subcommand_passed, чтобы получить текст попытки подкоманды.

@bot.group(pass_context=True)
async def cool(ctx):
    if ctx.invoked_subcommand is None:
        if ctx.subcommand_passed:
            await bot.say("**Wrong sub command: **{}".format(ctx.subcommand_passed))
        else:
            await bot.say("Subcommand required")

@cool.command(pass_context=True, name="bot")
async def _bot(ctx):
    msg = "Hello ...".format(ctx.message)
    await bot.say(msg)

Нераспознанная команда верхнего уровня вызовет CommandNotFound, что вам придется обработать в on_command_error. Я полагаю, что это все еще должно заполнить ctx.command предпринятой командой, но я не могу проверить в данный момент. Вы также можете попробовать ctx.invoked_with

@bot.event
async def on_command_error(error, ctx):
    if isinstance(error, commands.CommandNotFound):
        await bot.send_message(ctx.message.channel, "**Wrong command ** " + ctx.command)
...