简体   繁体   中英

How can I use the wait_for in my clearall command? (discord.py)

I'm making a clearall command that clears all of the messages in that channel, and I want it to have a yes and no option, but I'm not very familiar with wait_for. Can anyone incorporate that into the clearall command code?

@client.command
@commands.has_guild_permissions(administrator=True)
async def clearall(ctx):
    await ctx.channel.purge(limit=99999999)
    await ctx.send("All messages cleared.")

In order to use wait_for , you have to pass 3 arguments. First argument is event . According to API References:

  • event – The event name, similar to the event reference, but without the on_ prefix, to wait for.

Second argument is check function. Again according to API References:

  • check – A predicate to check what to wait for. The arguments must meet the parameters of the event being waited for.

And the last argument is timeout .

  • timeout – The number of seconds to wait before timing out and raising asyncio.TimeoutError .

So, to wait for a yes or no respond, you can do:

@client.command()
@commands.has_guild_permissions(administrator=True)
async def clearall(ctx):
    await ctx.send('Do you want to remove messages?')
    try:
        await client.wait_for('message', check=lambda m: m.content.lower()=='yes' and m.author==ctx.author, timeout=60.0)
    except asyncio.TimeoutError:
        await ctx.send('Timeout error')
    await ctx.channel.purge(limit=99999999)
    await ctx.send("All messages cleared.")

With this code, if you type yes , it'll purge all the messages. If you type something else than yes , it won't do anything.

If you want it to do something if input is not yes , for example canceling, you can do it too.

@client.command()
@commands.has_guild_permissions(administrator=True)
async def clearall(ctx):
    await ctx.send('Do you want to remove messages?(yes/no)')
    try:
        respond = await client.wait_for('message', timeout=60.0)
    except asyncio.TimeoutError:
        await ctx.send('Timeout error')
    if respond.content.lower() == 'yes' and respond.author==ctx.author:
        await ctx.send('done')
        await ctx.send("All messages cleared.")
    else:
        await ctx.send('canceled')

So, with this, if you type something else than yes , it will cancel the removing process.

Reference

Try this out:

@client.command()
async def clearall(ctx):
    await ctx.send('Would you like to clear all messages in this channel?')
    def check(m):
        return m.channel == ctx.channel and m.author == ctx.author
    try:
        message = await client.wait_for('message', timeout=5.0, check=check)
    except asyncio.TimeoutError:
        await ctx.send('Input not received in time')
    else:
        if 'yes' in message.content.lower():
            await ctx.channel.purge(limit=99999999)
        else:
            return

The timeout=5.0 acts to put a restraint on how long the command will wait for the next message - in this case, 5 seconds. You can modify it to your own time value. In this example, if the user doesn't respond in 5 seconds, then the bot will print out Input not received in time .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM