'How can I call stop a while loop while it is running and not finished yet?

Triggering Ctrl+C with a text from Discord itself?

How can I use close() with a text from Discord to stop the while loop even if it is not finished yet?

How can I stop client.run(TOKEN) with a text from Discord?



Solution 1:[1]

You can use the SIGINT signal, which is the signal when you Control-C. See here for more information. You can then get the bot to send the signal to itself:

import os
import signal

def do_control_c_interrupt():
    os.kill(os.getpid(), signal.SIGINT)

Then, you can do_control_c_interrupt() inside of a command if you need to stop the bot.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eric Jin