Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法让用户必须在discord.py的时间限制内回答?_Python_Discord_Discord.py - Fatal编程技术网

Python 有没有办法让用户必须在discord.py的时间限制内回答?

Python 有没有办法让用户必须在discord.py的时间限制内回答?,python,discord,discord.py,Python,Discord,Discord.py,如果我向用户发送请求,要求他们回答“是”或“否”问题,我如何使问题在一定时间后过期,不再接受答复 背景 我目前正在开发一个discord.pybot,现在我开始扫描用户输入的消息。它的工作原理是,有人可以向某人挑战一个tic tac toe游戏,然后被挑战的人有10秒钟的时间回答是或否。我如何使挑战在10秒钟后过期 例子 我想到的一个例子是botDank Memer。如果你在那里挑战某人玩井字游戏,那么被挑战的人必须在时间限制内回复你,否则挑战将过期。它还可以使用诸如pls trivia之类的命

如果我向用户发送请求,要求他们回答“是”或“否”问题,我如何使问题在一定时间后过期,不再接受答复

背景 我目前正在开发一个
discord.py
bot,现在我开始扫描用户输入的消息。它的工作原理是,有人可以向某人挑战一个tic tac toe游戏,然后被挑战的人有10秒钟的时间回答是或否。我如何使挑战在10秒钟后过期

例子 我想到的一个例子是bot
Dank Memer
。如果你在那里挑战某人玩井字游戏,那么被挑战的人必须在时间限制内回复你,否则挑战将过期。它还可以使用诸如
pls trivia
之类的命令,10秒后,它停止接受问题的答案

我很确定人们已经问过这个问题,但我在谷歌上看了一遍又一遍,找不到解决办法。我想这可能是因为我的措辞。


我也很难做一个最小的可复制的例子,因为这需要你们为bot创建一个不协调的帐户和令牌,这一点都不高效。你可以使用
等待客户端。等待(check=check)
。Check是一个必须定义的函数,它需要返回true或false。在check中,如果需要,可以添加各种条件和操作。我做了一个命令,如果你想看一个例子,可以用它来做一个数学等式。

你可以使用
等待客户端。等待(check=check)
。Check是一个必须定义的函数,它需要返回true或false。在check中,如果需要,可以添加各种条件和操作。如果你想看一个例子的话,我做了一个命令,用它来做一个数学等式。

我还没有自己做过(幸好我以前使用了discord.js),但是对于这个问题,我会首先提示是/否问题,然后在sleep()产生响应后,使用python时间模块中的sleep(),如果没有响应,表示不再接受任何回复

总的想法是这样的

带有提示问题的频道={}
def提示问题(…):
显示问题
通道[此通道id]=处理问题回答的一些函数
睡眠N秒
如果此通道id位于通道中:
在通道中宣布提示已超时
channels.pop(此频道id)
通道(…)中的def句柄消息:
如果通道中的通道id为:
通道[通道id](…)#运行提示功能
在不协调频道中提示问题时调用prompt question() 在通道中接收消息时,将调用通道中的句柄消息() 插入到通道字典中的函数将检查消息对于提示是否有效,处理响应,并在完成后从通道字典中弹出消息


(很抱歉,我在手机上,所以我的格式很糟糕)

我还没有尝试自己制作(幸好我以前使用了discord.js),但是对于这个问题,我会首先提示是/否问题,然后使用python的时间模块中的sleep(),在sleep()产生后,如果没有响应,就说不会接受更多响应

总的想法是这样的

带有提示问题的频道={}
def提示问题(…):
显示问题
通道[此通道id]=处理问题回答的一些函数
睡眠N秒
如果此通道id位于通道中:
在通道中宣布提示已超时
channels.pop(此频道id)
通道(…)中的def句柄消息:
如果通道中的通道id为:
通道[通道id](…)#运行提示功能
在不协调频道中提示问题时调用prompt question() 在通道中接收消息时,将调用通道中的句柄消息() 插入到通道字典中的函数将检查消息对于提示是否有效,处理响应,并在完成后从通道字典中弹出消息


(对不起,我在手机上,所以我的格式很糟糕)

您可以尝试使用
机器人。等待()
并检查并超时

import asyncio

@bot.command()
async def tic_tac_toe(ctx):
    def check(m):
        # returns whether the author of the (input) message is the author of the
        # command and whether the channel of the message is the channel of the command
        return m.author == ctx.author and m.channel == ctx.channel

    # asking the question
    await ctx.send("Do you want to play a game of tic tac toe? Respond with yes or no within 10 seconds.")
    
    try:
        # storing the response in a variable, adding the check
        # and setting timeout to 10 seconds (float)
        response = await bot.wait_for('message', check=check, timeout=10.0)

        if response.content == 'yes':
            # whatever action
        else:
            # whatever action

    # now comes the real part
    except asyncio.TimeOutError:
        await ctx.send("Aw man! You did not respond within ten seconds... try again!")

您可以尝试使用
bot.wait_for()
进行检查并超时

import asyncio

@bot.command()
async def tic_tac_toe(ctx):
    def check(m):
        # returns whether the author of the (input) message is the author of the
        # command and whether the channel of the message is the channel of the command
        return m.author == ctx.author and m.channel == ctx.channel

    # asking the question
    await ctx.send("Do you want to play a game of tic tac toe? Respond with yes or no within 10 seconds.")
    
    try:
        # storing the response in a variable, adding the check
        # and setting timeout to 10 seconds (float)
        response = await bot.wait_for('message', check=check, timeout=10.0)

        if response.content == 'yes':
            # whatever action
        else:
            # whatever action

    # now comes the real part
    except asyncio.TimeOutError:
        await ctx.send("Aw man! You did not respond within ten seconds... try again!")

如果你把这个例子也包括进来,那就太好了,谢谢@PythonPikachu8,请查看我的答案,以获取关于等待的信息。如果您包含示例,那将非常好,谢谢@PythonPikachu8,查看我的答案,获取关于等待的信息。谢谢你的答案!我会尝试一下(我觉得睡眠可能不可靠,可能会导致一些错误)。我对python不太熟悉,但我认为asyncio包更可靠,只是不要忙着等待,这在任何语言中都很可怕。好的,谢谢你的建议!那么你是说另一个答案更好?我还没有用client.wait_,所以我不确定它是否比sleep更好好,谢谢你的建议!谢谢你的回答!我会尝试一下(我觉得睡眠可能不可靠,可能会导致一些错误)。我对python不太熟悉,但我认为asyncio包更可靠,只是不要忙着等待,这在任何语言中都很可怕。好的,谢谢你的建议!那么你是说另一个答案更好?我还没有用client.wait_,所以我不确定它是否比sleep更好好,谢谢你的建议!关于您的最小可复制示例,只需将其包括在内,它是否需要我们创建机器人或应用程序并不重要。您的问题是关于代码的,最好包含代码。
Bot.wait_for(…)
有一个参数
timeout
,可用于完成此操作<代码>机器人。等待(超时=以秒为单位的时间。。。