猿问

是否可以为不和谐的机器人嵌套命令?

对编码非常陌生,所以请耐心等待。我想知道在处理不和谐机器人时是否可以对嵌套命令和响应进行排序。例如,您将使用命令查看您的选项,然后机器人将等待对其消息的响应并做出相应的回复。我在描述我的意思时遇到了一点麻烦,所以这里有一个例子:你问机器人一些事情机器人给你选项你从这些选项中选择机器人回应你的答案或者你要求机器人用你做的事情说下一个 机器人让你说些什么 你说些什么 机器人在它的回复中使用你所说的


我已经尝试将 on_message 命令嵌套到已经存在的 if 语句中,但这显然没有奏效。我还尝试添加另一个 if 语句,包含整个 message.content 内容,希望机器人在考虑到响应后会考虑该消息。


async def on_message(message):

    if message.author == client.user:

        return

    if message.content.startswith("!ml"):

        message.content = message.content.lower().replace(' ', '')

        if message.content in command1:

            response = "Hello! To start type !ml menu. You will be given your options. Don't forget to type !ml before " \

                       "everything you tell me, so I know it's me your talking to! Thanks : ) "

        elif message.content in command2:

            response = "test"

            if message.content in top:


            await message.channel.send(response)

我原以为机器人会在回复后考虑到该消息,但是机器人只是从头开始。


紫衣仙女
浏览 83回答 1
1回答

UYOU

当输入第一个命令时,使用某种外部状态(例如,全局变量)来跟踪这个事实。您对on_message第一个命令的响应与对第二个命令的响应相同,因此它需要查看该外部状态并决定相应地执行什么操作。一个快速的、即兴的、未经测试的(因为我没有设置 discord.py)示例:in_progress = Falseasync def on_message(message):    if message.author == client.user:        return    elif "start" in message.content and not in_progress:        in_progress = True        await message.channel.send("You said `start`. Waiting for you to say `stop`.")    elif "stop" in message.content and in_progress:        in_progress = False        await message.channel.send("You said `stop`. Waiting for you to `start` again.")
随时随地看视频慕课网APP

相关分类

Python
我要回答