discord.py重写on_message不存在的命令

时间:2020-05-11 21:30:07

标签: python-3.x discord.py-rewrite

当我使用!order命令时,将发送Not existing command消息,该如何避免呢?

我的代码:

@client.event
async def on_message(message):
    channel = message.author
    def check(m):
        return m.channel == message.channel and m.author != client.user

    if message.content.startswith("!order"):
        await channel.send("in game name")
        in_game_name = await client.wait_for('message', check=check)

        await channel.send("in game ID")
        in_game_ID = await client.wait_for('message', check=check)

        await channel.send("cargo type")
        cargo_type = await client.wait_for('message', check=check)

        await channel.send("cargo limit")
        cargo_limit = await client.wait_for('message', check=check)

        await channel.send("storage")
        storage = await client.wait_for('message', check=check)

        await channel.send("priority")
        priority = await client.wait_for('message', check=check)

    await client.process_commands(message)


@client.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.CommandNotFound):
        await ctx.send("Not existing command!") 

1 个答案:

答案 0 :(得分:2)

您可以将process_commands移到else块中,以便仅在您的on_message不处理命令时运行它

@client.event
async def on_message(message):
    channel = message.author
    def check(m):
        return m.channel == message.channel and m.author != client.user

    if message.content.startswith("!order"):
        await channel.send("in game name")
        in_game_name = await client.wait_for('message', check=check)

        await channel.send("in game ID")
        in_game_ID = await client.wait_for('message', check=check)

        await channel.send("cargo type")
        cargo_type = await client.wait_for('message', check=check)

        await channel.send("cargo limit")
        cargo_limit = await client.wait_for('message', check=check)

        await channel.send("storage")
        storage = await client.wait_for('message', check=check)

        await channel.send("priority")
        priority = await client.wait_for('message', check=check)
    else:
        await client.process_commands(message)