【问题标题】:How to stop the execution if "!stop" found in the discord channel?如果在不和谐频道中找到“!停止”,如何停止执行?
【发布时间】:2022-01-04 02:03:41
【问题描述】:

如果机器人从频道收到“!停止”命令,如何停止脚本的执行? 我只是在学习创建这样的机器人,所以我当前的代码是:

bot = commands.Bot(command_prefix='!')
@bot.command(name='stop')
async def here(ctx):
        while True:
            print(2)
            time.sleep(15)

bot.run(TOKEN)

所以我有 3 个 selenium 驱动程序在不同的机器上工作,我想通过不和谐通道建立通信(每台机器都有自己的机器人)。目前,首先打开特定 URL 的机器将向频道发送停止消息并关闭与聊天的连接,而其他两台机器应在消息后立即停止。

所以,想法

page_opened = True
while page_opened = True:
     do something and listen
     if there is a message stop:
         page_opened = false

我应该如何处理不和谐机器人的这个想法?有什么提示吗?

【问题讨论】:

  • stackoverflow.com/questions/64473974/… 说你应该返回一些东西(虽然我不确定它是否是一个函数)。您总是可以尝试:while running: 并在开始之前将 running 设置为 true,但一旦调用 !stop 就设置为 false。
  • @LarrytheLlama 感谢您的评论。但我也没有完全理解所提供的解决方案......

标签: python discord discord.py


【解决方案1】:

只要您想停止执行任何 Python 程序,您只需使用exit()sys.exit() 命令即可。例如:

import sys

bot = commands.Bot(command_prefix='!')

@bot.command(name='stop')
async def here(ctx):
    sys.exit()  # Stops execution of the program

bot.run(TOKEN)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 2013-10-13
    • 2020-12-02
    • 2018-02-09
    • 2021-10-05
    • 2022-11-10
    • 1970-01-01
    相关资源
    最近更新 更多