【问题标题】:How to check what the user entered after a bot message using discord.py?如何使用 discord.py 检查用户在机器人消息后输入的内容?
【发布时间】:2021-10-06 21:30:24
【问题描述】:

我最近开始制作不和谐机器人。所以我有这个不和谐的机器人,它的功能是问用户一个你想问的问题。因此,机器人从问题列表中获取一个问题并发送一条消息,要求用户选择第一个或第二个选项。此外,我希望机器人能够回应用户的回应,比如“不错的选择”。但是,它不起作用并且给了我错误。这样做的正确方法是什么?

这是我的代码:

# Event registered when bot recieves a message
@Client.event
async def on_message(message):
    # Don't do anything if the message is from the bot
    if message.author == Client.user:
        return


    if message.content.startswith("$Hi"):
        await message.channel.send("Hello")


    # WOULD YOU RATHER GAME COMMANDS


    if message.content.startswith("$WYRG"):
        await message.channel.send(random.choice(ListOfWYRGQuestions) + "\n\n Please enter 1 or 2 for option 1 or option 2.")

        if message.content.startswith("1"):
            await message.channel.send("Good choice!")
        elif message.content.startswith("2"):
            await message.channel.send("Good Choice!")

什么是更准确和正确的方式来响应用户的响应?

【问题讨论】:

    标签: python python-3.x asynchronous discord discord.py


    【解决方案1】:

    您的代码的问题是is message.content.startswith("1")if message.content.startswith("$WYRG"): 的范围内这是自相矛盾的,因为消息需要同时以$WYRG12 开头(如该函数从每条消息的开始调用)

    要解决此问题,您应该将is message.content.startswith("1")is message.content.startswith("2") 移出范围,并且可能使用game_state 变量,因此这些命令只能在$WYRG 命令被首先调用时调用。这是我写的代码:

    game_state = False
    @bot.event
    async def on_message(message):
        global game_state
    
        # Don't do anything if the message is from the bot
        if message.author == bot.user:
            return
    
    
        if message.content.startswith("$Hi"):
            await message.channel.send("Hello")
    
    
        # WOULD YOU RATHER GAME COMMANDS
    
    
        if message.content.startswith("$WYRG"):
            await message.channel.send(random.choice(ListOfWYRGQuestions) + "\n\n Please enter 1 or 2 for option 1 or option 2.")
            game_state = True
        if message.content.startswith("1") and game_state:
            await message.channel.send("Good choice!")
            game_state = False
        elif message.content.startswith("2") and game_state:
            await message.channel.send("Good Choice!")
            game_state = False
    

    希望这是有道理的:)

    【讨论】:

      【解决方案2】:

      由于您似乎使用的是$WYRG和$Hi,我建议您查看d.py commands framework,至于如何获取下一条消息的答案,请查看wait_for客户端方法,它也可以设置x秒后超时,自动退出游戏

      【讨论】:

        猜你喜欢
        • 2021-09-28
        • 2017-01-17
        • 1970-01-01
        • 1970-01-01
        • 2021-02-13
        • 1970-01-01
        • 1970-01-01
        • 2021-10-14
        • 1970-01-01
        相关资源
        最近更新 更多