python - (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
解决方案
您可以使用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
推荐阅读
- typescript - “引用一个值,但在此处用作类型”仅在导出接口时出错
- c# - 如何在不使用 Visual Studio 的情况下将测试方法与 Azure 测试计划测试用例相关联?
- angular - 如何在离子页面上触发 ngOnInit
- android - 我如何在多段线中显示路径与谷歌地图颤动的多个坐标
- r - 有没有办法在切换标签时防止闪亮“记住”旧图像?
- python - 检查输入时出错:预期 input_39 有 2 个维度,但得到了形状为 (100、50、780) 的数组
- html - 在 Power BI 中将 HTML 列转换为纯文本
- camunda - BPM 项目范围和边界
- linux - 需要在 shell 脚本中一次对两个 Oracle DB 执行一个 sql 查询,并将数据导出到单独的 csv 文件
- python - ctypes 等效于“字符名称 [MAX_DEVICE_NAME_LENGTH]”