【问题标题】:discord.py-rewrite wait_for() canceldiscord.py-rewrite wait_for() 取消
【发布时间】:2020-05-05 01:45:10
【问题描述】:

我正在制作一个带有重写的不和谐机器人,当命令运行时,事件必须完成,但是如果我想执行另一个命令,我不能因为前一个命令还没有完成,它会发送其他消息,如何我可以阻止它吗?

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

    if message.content.startswith("!order"):
        channel = message.author
        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)

【问题讨论】:

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


    【解决方案1】:

    如果检查到某个单词,您可以在检查中引发异常。在这里,如果机器人看到消息CANCEL,它将取消命令:

    @client.event
    async def on_message(message):
        def check(m):
            if m.channel == message.channel and m.content == "CANCEL":
                raise ValueError("Cancelled command")
            return m.channel == message.channel and m.author != client.user
    
        if message.content.startswith("!order"):
            channel = message.author
            await channel.send("in game name")
            in_game_name = await client.wait_for('message', check=check)
    

    【讨论】:

    • 您可以将timeout 添加到wait_for。如果超时,wait_for 将返回 None
    猜你喜欢
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    相关资源
    最近更新 更多