首页 > 解决方案 > (Discord.py) 我如何制作一个敢于 21 的游戏?我需要以数字的形式输入,比如“1 2 3”(最多 3 个数字)并轮流输入?

问题描述

我一直在开发一个不和谐的机器人,我试图在其中制作游戏“21 胆”(有关该信息的信息,请参见此链接 [https://www.wikihow.com/Play-21-Dares][1])

我如何轮流输入?我尝试使用

玩家 = 等待 msg.reaction.users().flatten()

但是我该如何让它轮流呢?

接下来,我需要检查输入,例如,第一个玩家最多只能说出 3 个数字,而第二个玩家必须在第一个玩家的第三个数字之后开始。基本思想是,

玩家 A:“1、2、3。” 玩家 B:“4、5。” 玩家 C:“6、7、8。” 玩家 D:“9。” 玩家 E:“10、11、12。” 玩家 A:“13、14、15。” 玩家 B:“16、17。” 玩家 C:“18。” 玩家 D:“19、20。” 玩家 E:“21。” 在这种情况下,玩家 E 被迫说“21”。玩家A:“1。” 玩家 B:“2、3、4。” 玩家 C:“5、6、7。” 玩家 D:“8、9、10。” 玩家 A:“11。” 玩家 B:“12、13、14。” 玩家 C:“15。” 玩家 D:“16、17、18。” 玩家 A:“19。” 玩家 B:“20。” 玩家 C:“21。” 在这种情况下,玩家 C 被迫说“21”。玩家A:“1。” 玩家 B:“2、3、4。” 玩家 C:“5、6、7。” 玩家 D:“8。” 玩家 E:“9。” 玩家 F:“10、11、12。” 玩家 G:“13、14。” 玩家 H:“15、16。” 玩家 A:“17、18、19。” 玩家 B:“20。” 玩家 C:“21。” 在这种情况下,玩家 C 被迫说“21。

到目前为止,这是我的代码。请帮助我,我已经尝试了 3 天来完成这项工作

    async def tod(self, ctx, user: discord.Member = None):
        '''21 truth or dare game'''
        the_author = ctx.author
        channel = ctx.channel
        if user is None:
            embed = discord.Embed(title="Truth or Dare game", color=discord.Colour.orange(),
                                  description=f"{the_author.mention} is inviting anyone to play truth or dare! \n\nType `accept` now to accept the challenge and begin a game with them.")
        elif user != the_author and not user.bot:
            embed = discord.Embed(title=" truth or dare", color=discord.Colour.orange(),
                                  description=f"{the_author.mention} is inviting anyone to play truth or dare! \n\nType `accept` now to accept the challenge and begin a game with them.")
        else:
            embed = discord.Embed(title="You can't invite yourself or a discord bot to a game!")
        
        msg = await channel.send(embed=embed)
        await msg.add_reaction("✅")
        

        import time
        time.sleep(10)
        players = await msg.reaction.users().flatten()
        check_list = []
        current_count = []
        def check(message):
            list = message.split(' ')
            for element in list:

                element = int(element)
                check_list.append(element)

            range_list=list(range(min(check_list), max(check_list)+1))
            if check_list == range_list:
                for element in current_count :check_list.append(element)
                check_list.clear()

        await self.bot.wait_for( 'message', check=check, timeout=60)```

I'm quite new to discord.py, so any small tips are also appreciated.

  [1]: https://www.wikihow.com/Play-21-Dares

标签: pythondiscorddiscord.py

解决方案


您可以使用on_message对发送的消息做出反应。从这个事件中,您会收到一个类型为 的对象Message。使用该属性author,您可以获得发送消息的成员。您现在可以比较成员和当前玩家的 id。使用您的content属性Message可以检查消息是否有效。你可以在这里自己阅读文档

要检查哪些玩家转了它,我建议您简单地创建一个计数器,每次收到有效消息时它都会增加。您将用户的 Member-ID 与 的 ID 进行比较player[counter]

编辑@Dhravya Shah 的评论: 我会这样检查(我没有测试代码,但概念是这样的):

client = discord.Client()
count = 0

@client.event
async def on_message(message):
    if players[count] == message.author:
        #Check if message is valid
        if messageIsValid: #placeholder variable
            #Save  the answer to an array or similar
            count += 1


推荐阅读