如何取消待定的wait_for

时间:2019-08-27 10:50:56

标签: python discord discord.py coroutine

假设有与此类似的命令:

<dependencies>
    <dependency>
        <groupId>com.lmax</groupId>
        <artifactId>disruptor</artifactId>
        <version>3.3.6</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.cloudera.hadoop</groupId>
        <artifactId>hadoop</artifactId>
        <version>2.6.0-mr1-cdh5.7.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.phoenix</groupId>
        <artifactId>phoenix-spark</artifactId>
        <version>4.14.1-HBase-1.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.phoenix</groupId>
        <artifactId>phoenix-client</artifactId>
        <version>4.14.0-cdh5.14.2</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.phoenix</groupId>
                <artifactId>phoenix-spark</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.11</artifactId>
        <version>2.3.1</version>
        <exclusions>
            <exclusion>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-sql_2.11</artifactId>
        <version>2.3.1<version>
        <exclusions>
            <exclusion>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-yarn_2.11</artifactId>
        <version>2.3.1<version>
        <exclusions>
            <exclusion>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>{main class...}</mainClass>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"></transformer>
                        </transformers>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

如果用户在实际对消息作出反应之前多次发送相同的命令,是否可以取消该@bot.command() async def test(ctx): def check(r, u): return u == ctx.message.author and r.message.channel == ctx.message.channel and str(r.emoji) == '✅' await ctx.send("React to this with ✅") try: reaction, user = await bot.wait_for('reaction_add', timeout=300.0, check=check) except asyncio.TimeoutError: await ctx.send('Timeout') else: await ctx.send('Cool, thanks!') ?因此,该漫游器停止等待先前发送的消息的反应,而仅等待最后一条。

1 个答案:

答案 0 :(得分:4)

这样对您有用吗?

pending_tasks = dict()

async def test(ctx):
    def check(r, u):
        return u == ctx.message.author and r.message.channel == ctx.message.channel and str(r.emoji) == '✅'

    await ctx.send("React to this with ✅")
    try:
        if ctx.message.author in pending_tasks:
            pending_tasks[ctx.message.author].close()
        pending_tasks[ctx.message.author] = bot.wait_for('reaction_add', timeout=300.0, check=check)
        reaction, user = await pending_tasks[ctx.message.author]
    except asyncio.TimeoutError:
        await ctx.send('Timeout')
    else:
        await ctx.send('Cool, thanks!')

您将所有待处理的请求存储在dict中,在创建另一个请求之前,请检查您是否已为此用户执行现有任务,如果要取消并创建一个新任务,则请