首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >命令中的Discord.py命令?

命令中的Discord.py命令?
EN

Stack Overflow用户
提问于 2020-12-15 23:21:00
回答 2查看 455关注 0票数 0

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

显然,我有一个用命令.blackjack调用的游戏,它在生成随机值和发送消息方面工作得很好。但是,我不知道怎么做,所以玩家能够在发牌的消息发出后说点击或站立,例如。

代码语言:javascript
运行
复制
@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})")

这次又是什么?我该如何运行我的下一部分代码,即玩家是否击球或站立,发牌人是否击球或站立等。

EN

回答 2

Stack Overflow用户

发布于 2020-12-15 23:41:15

discord.py具有内置子命令支持,下面是一个示例:

代码语言:javascript
运行
复制
@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

或者,您可以简单地等待消息

代码语言:javascript
运行
复制
@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`
票数 1
EN

Stack Overflow用户

发布于 2020-12-15 23:43:27

我真的不知道怎么玩二十一点,所以我恐怕不能给你一个完整的回答你的问题。然而,我会说你如何才能实现你想要的。在我看来,有两种方法可以做到这一点。

方法1

等待用户对机器人的消息做出反应

为此,您必须使用:

代码语言:javascript
运行
复制
reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)

例如,假设您正在等待或️来自用户(这可以分别表示点击和站立)。代码将如下所示:

代码语言:javascript
运行
复制
@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

等待用户发送消息

为此,您必须使用:

代码语言:javascript
运行
复制
msg = await client.wait_for('message', check = check, timeout = 60.0)

然后,您必须检查msg是否等于hit或stand,或者是某种缩写形式,如h或s。此外,请确保编写一个在client.wait_for()函数(check = check)中调用的check(author)函数,以检查作者是否与运行命令的作者相同。

我希望你在读完这个答案后能想出你想要的代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65308715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档