为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

紫衣仙女 2022-06-28 16:55:49
对编码非常陌生,所以请耐心等待。我想知道在处理不和谐机器人时是否可以对嵌套命令和响应进行排序。例如,您将使用命令查看您的选项,然后机器人将等待对其消息的响应并做出相应的回复。我在描述我的意思时遇到了一点麻烦,所以这里有一个例子:你问机器人一些事情机器人给你选项你从这些选项中选择机器人回应你的答案或者你要求机器人用你做的事情说下一个 机器人让你说些什么 你说些什么 机器人在它的回复中使用你所说的我已经尝试将 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)我原以为机器人会在回复后考虑到该消息,但是机器人只是从头开始。
查看完整描述

1 回答

?
UYOU

TA贡献1878条经验 获得超4个赞

当输入第一个命令时,使用某种外部状态(例如,全局变量)来跟踪这个事实。您对on_message第一个命令的响应与对第二个命令的响应相同,因此它需要查看该外部状态并决定相应地执行什么操作。一个快速的、即兴的、未经测试的(因为我没有设置 discord.py)示例:


in_progress = False


async 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.")

查看完整回答
反对 回复 2022-06-28
  • 1 回答
  • 0 关注
  • 82 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号