多个 wait_for 消息 discord.py

时间:2021-05-31 08:41:21

标签: python discord.py python-asyncio

我想要做什么:这与a question I previously asked的情况类似,我正在制作多人游戏。我想同时获得多个用户输入,基本上是我发现的另一个问题 (multiple wait_for_messages wanted discord.py) 中的情况。

我的问题:我每次都需要向不同的人发送消息。基本上,一款游戏可能有 3 名玩家,而另一款游戏可能有 5 名玩家。最初的目的也是同时向用户的 dms 发送消息,并让他们从那里回答输入。我不确定如何在我的代码中定义我需要的 wait_for 数量。

代码尝试 1

    # inside a cog
    @commands.command()
    async def bagpie(self, ctx, *members: discord.Member):
        members = list(members)
        if ctx.author not in members:
            members.append(ctx.author)
        if len(members) > 3:
            return
        # making checks
        def check1(msg):
            return msg.author == members[0]
        def check2(msg):
            return msg.author == members[1]
        def check3(msg):
            return msg.author == members[2]      
        # waiting for multiple messages with the checks      
        ret = await asyncio.gather(
        await self.bot.wait_for("message", timeout=10, check=check1),
        await self.bot.wait_for("message", timeout=10, check=check2),
        await self.bot.wait_for("message", timeout=10, check=check3),
        return_exceptions = True
        )

        # Test for errors
        ret = [r if not isinstance(r, Exception) else None for r in ret]
        msg1, msg2, msg3 = ret # *ret would cause an error to occur
        # setup embed to send final product
        fin_string = ""
        count = 0
        msg_list = [msg1, msg2, msg3]
        for member in members:
            fin_string += f"{member} says: {msg_list[count]}"
            count += 1
        embed = discord.Embed(title="This is what you said!", description=fin_string)
        await ctx.send(embed=embed)

结果:如您所见,尽管提供了输入,但机器人没有响应,而是恢复到超时状态。

Result of Attempt 1 not working

代码尝试 2

# couldn't get this to work in a cog, so this one is not in a cog

# checks if it's a dm and if author is given member
def is_dm_check(member):
    def inner_check(msg):
        return msg.author.id==member.id and msg.guild is None
    return inner_check

# sends message to member and waits for the message
async def get_input(member):
    await member.send("Please provide an input:")
    return client.wait_for('message', check=is_dm_check(member), timeout=75)

@client.command()
async def play_game(ctx, members: commands.Greedy[discord.Member]=None):
    if not members:
        await ctx.send("No Players")
        return
    if ctx.author not in members:
        members.append(ctx.author)
    responses = asyncio.gather(*map(get_input, members))
    for member, response in zip(members, responses):
        await ctx.send(f"{member.mention}: {response.content}")

结果:消息应该是同时发送的,但它仍然抛出错误。

Code once again throws an error

0 个答案:

没有答案