简体   繁体   English

如何在 clearall 命令中使用 wait_for? (discord.py)

[英]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.我正在制作一个清除该频道中所有消息的 clearall 命令,我希望它有一个是和否选项,但我对 wait_for 不是很熟悉。 Can anyone incorporate that into the clearall command code?任何人都可以将其合并到 clearall 命令代码中吗?

@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.为了使用wait_for ,您必须通过 3 个 arguments。 First argument is event .第一个参数是event According to API References:根据 API 参考资料:

  • event – The event name, similar to the event reference, but without the on_ prefix, to wait for. event – 要等待的事件名称,类似于事件引用,但没有 on_ 前缀。

Second argument is check function.第二个参数是check function。 Again according to API References:再次根据 API 参考文献:

  • check – A predicate to check what to wait for. check – 检查等待什么的谓词。 The arguments must meet the parameters of the event being waited for. arguments 必须满足等待事件的参数。

And the last argument is timeout .最后一个参数是timeout

  • timeout – The number of seconds to wait before timing out and raising asyncio.TimeoutError . timeout – 在超时和引发asyncio.TimeoutError之前等待的秒数。

So, to wait for a yes or no respond, you can do:因此,要等待yesno响应,您可以执行以下操作:

@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.使用此代码,如果您键入yes ,它将清除所有消息。 If you type something else than yes , it won't do anything.如果您输入的不是yes ,它不会做任何事情。

If you want it to do something if input is not yes , for example canceling, you can do it too.如果你想让它在输入不是yes时做某事,例如取消,你也可以这样做。

@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.因此,有了这个,如果您输入的不是yes ,它将取消删除过程。

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. timeout=5.0用于限制命令等待下一条消息的时间——在本例中为 5 秒。 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 .在这个例子中,如果用户在 5 秒内没有响应,那么机器人将打印出Input not received in time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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