【问题标题】:Is there a way to make it such that the user must answer within a time limit on discord.py?有没有办法让用户必须在 discord.py 的时限内回答?
【发布时间】:2021-06-29 18:26:40
【问题描述】:

如果我向用户发送请求,让他们回答是或否问题,我怎样才能使问题在一定时间后过期并且不再接受回复?

背景

我目前正在开发一个discord.py 机器人,我正在开始扫描消息以获取用户输入的部分。它的工作原理是,有人可以向某人挑战井字游戏,然后被挑战的人有 10 秒的时间回答是或否。我怎样才能让挑战在 10 秒后过期?

示例

我想到的一个例子是机器人Dank Memer。如果您在那里向某人挑战井字游戏,则被挑战的人必须在一定时间内回复您,否则挑战将过期。它也适用于pls trivia 等命令,10 秒后它会停止接受问题的答案。

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

我也很难做一个最小可重现的例子,因为这需要你们为机器人创建一个不和谐的帐户和令牌,这根本没有效率。

【问题讨论】:

  • 关于您的最小可重现示例,只需包含它,它是否需要我们创建机器人或应用程序都没有关系。您的问题是关于代码的,最好包含代码。
  • Bot.wait_for(...) 有一个参数 timeout 可用于完成此操作。 Bot.wait_for(timeout=time_in_seconds, ...)

标签: python discord discord.py


【解决方案1】:

您可以尝试使用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!")

【讨论】:

    【解决方案2】:

    我还没有尝试过自己制作(好在我之前使用过 discord.js),但为此,我会首先提示是/否问题,然后在 sleep() 产生后使用 python 时间模块中的 sleep() , 如果没有响应,则表示不再接受任何响应

    大致思路是这样的

    channels with a prompted question = {}
    
    def prompt question(...):
       display the question
       channels[this channel id] = some function to handle responses for the question
    
       sleep for N seconds
       
       if this channel id in channels:
          announce in channel that prompt has timed out
          channels.pop(this channel id)
    
    def handle message in a channel(...):
       if channel id in channels:
          channels[channel id](...) # run the prompt's function
    

    在不和谐频道中提示问题时调用prompt question() 当在通道中接收到消息时,将调用通道中的处理消息() 并且您插入频道字典的功能将检查消息是否对提示有效,处理响应,并在完成时从频道字典中弹出它

    (抱歉,我用的是手机,所以我的甲骨文很烂)

    【讨论】:

    • 感谢您的回答!我试试看(感觉睡眠可能不靠谱,可能会导致一些错误)。
    • 我对python不太熟悉,但是我觉得asyncio包比较靠谱,就是别忙着等待,这对任何语言来说都太可怕了lol
    • 好的,谢谢您的建议!所以你是说另一个答案更好?
    • 我没用过client.wait_for,所以不知道会不会比sleep()好用
    【解决方案3】:

    您可以使用await client.wait_for(check=check)。 Check 是一个必须定义的函数,它需要返回 true 或 false。在检查中,您可以根据需要添加各种条件和操作。如果你想看一个例子,我做了一个用它做数学方程式的命令。

    【讨论】:

    • 如果你能附上这个例子就太好了,谢谢!
    • @PythonPikachu8,查看我的回答 here 以获取有关 wait_for 的信息。
    猜你喜欢
    • 2021-03-29
    • 1970-01-01
    • 2021-03-21
    • 2021-03-31
    • 2021-06-16
    • 1970-01-01
    • 2019-01-24
    • 2021-11-22
    • 2022-08-08
    相关资源
    最近更新 更多