【问题标题】:Discord.py Commands within Commands?命令中的 Discord.py 命令?
【发布时间】:2021-03-26 06:07:34
【问题描述】:

我一直在考虑在我的不和谐机器人中制作二十一点游戏,但遇到了障碍。

我显然有使用命令.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})")

现在呢?我该怎么做才能运行我的下一部分代码,即玩家是否击球或站立,庄家是否击球和站立等。

【问题讨论】:

  • 检查one第76行
  • 你的意思是子命令?

标签: python discord discord.py


【解决方案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'))

这是一个简单的代码,用于检查用户是否对?或?️做出反应。你必须添加更多的条件和循环才能得到你想要的。

方法二

等待用户发送消息

为此,您必须使用:

msg = await client.wait_for('message', check = check, timeout = 60.0)

然后您必须检查 msg 是否等于 hit 或 stand 或诸如 h 或 s 之类的简短形式。还要确保编写一个在client.wait_for() 函数(check = check) 内部调用的check(author) 函数,以检查作者是否与运行该命令的作者相同。

我希望您在阅读此答案后能够想出您正在寻找的代码。

【讨论】:

    【解决方案2】:

    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`
    

    【讨论】:

      猜你喜欢
      • 2021-05-23
      • 2021-08-28
      • 2021-01-22
      • 2021-03-30
      • 2021-05-17
      • 2021-01-15
      • 2022-12-04
      • 2023-02-25
      • 2020-07-15
      相关资源
      最近更新 更多