我一直在考虑在我的不和谐机器人中制作一个21点游戏,但我遇到了一个障碍。
显然,我有一个用命令.blackjack
调用的游戏,它在生成随机值和发送消息方面工作得很好。但是,我不知道怎么做,所以玩家能够在发牌的消息发出后说点击或站立,例如。
@client.command()
async def blackjack(ctx):
# (insert all random number gens, etc. here)
await ctx.send(f"{dface1}{dsuit1} ({dvalue1}), {dface2}{dsuit2} ({dvalue2})")
await ctx.send(f"(Dealer Total: {dtotal})")
await ctx.send(f"{pface1}{psuit1} ({pvalue1}), {pface2}{psuit2} ({pvalue2})")
await ctx.send(f"(Total: {ptotal})")
这次又是什么?我该如何运行我的下一部分代码,即玩家是否击球或站立,发牌人是否击球或站立等。
发布于 2020-12-15 23:41:15
discord.py
具有内置子命令支持,下面是一个示例:
@commands.group(invoke_without_subcommand=True)
async def your_command_name(ctx):
# Do something if there's not a subcommand invoked
@your_command_name.command()
async def subcommand_name(ctx, *args):
# Do something
# To invoke
# {prefix}your_command_name subcommand_name some arguments here
或者,您可以简单地等待消息
@client.command()
async def blackjack(ctx):
# ...
def check(message):
"""Checks if the message author is the same as the one that invoked the
command, and if the user chose a valid option"""
return message.author == ctx.author and message.content.lower() in ['stand', 'hit']
await ctx.send('Would you like to hit or stand?')
message = await client.wait_for('message', check=check)
await ctx.send(f"You chose to `{message.content}`")
# To invoke
# {prefix}blackjack
# Would you like to hit or stand?
# stand
# You chose to `stand`
发布于 2020-12-15 23:43:27
我真的不知道怎么玩二十一点,所以我恐怕不能给你一个完整的回答你的问题。然而,我会说你如何才能实现你想要的。在我看来,有两种方法可以做到这一点。
方法1
等待用户对机器人的消息做出反应
为此,您必须使用:
reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
例如,假设您正在等待或️来自用户(这可以分别表示点击和站立)。代码将如下所示:
@client.command()
async def start(ctx):
def check(reaction, user):
return (user == ctx.author) and (str(reaction.emoji) == '' or str(reaction.emoji) == '️')
async def sendMessage(msg):
message = await ctx.send(msg)
await message.add_reaction('')
await message.add_reaction('️')
try:
reaction, user = await client.wait_for('reaction_add', timeout = 60.0, check = check)
except:
await message.clear_reactions()
await ctx.send('No reaction received.')
else:
await message.clear_reactions()
return reaction
return 0
reaction = str(await sendMessage('This is my message'))
这是一个用于检查用户是否使用或️进行反应的简单代码。你将不得不添加更多的条件和循环来获得你想要的东西。
方法2
等待用户发送消息
为此,您必须使用:
msg = await client.wait_for('message', check = check, timeout = 60.0)
然后,您必须检查msg
是否等于hit或stand,或者是某种缩写形式,如h或s。此外,请确保编写一个在client.wait_for()
函数(check = check
)中调用的check(author)
函数,以检查作者是否与运行命令的作者相同。
我希望你在读完这个答案后能想出你想要的代码。
https://stackoverflow.com/questions/65308715
复制相似问题