H
H
hoojpop2021-06-13 16:20:23
Python
hoojpop, 2021-06-13 16:20:23

How to properly run a Discord bot in a stream?

I am working with PyQt5 . You need to run the Discord bot along with the rest of the thread.

def BOT_RUN():
    print("FUNC: BOT_RUN")
    bot.run(config.BOT_TOKEN)


class WorkThread(QtCore.QThread):
    threadSignal = QtCore.pyqtSignal(int)

    def __init__(self):
        super().__init__()

    def run(self):
        i = ''
        self.msleep(300)
        self.threadSignal.connect(BOT_RUN)
        self.threadSignal.connect(CHECK_CSVS)
        self.threadSignal.connect(SERVER_MONITORING)
        self.threadSignal.emit(i)
        self.threadSignal.disconnect(CHECK_CSVS)


self.threadSignal.connect(BOT_RUN)- fires and starts the bot, but the application closes. As if he launched a bot and that's enough for him.

For debugging, I wrote in each function print("FUNC: функция")to understand if other functions are launched.
It turns out that it does not start:

FUNC: BOT_RUN
PS C:\Users\...\Desktop\LOGGER>


How to continue further actions of the code after starting the bot?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hoojpop, 2021-06-14
@hoojpop

def START():
    loop = asyncio.new_event_loop()
    try:
        loop.run_until_complete(bot.start(config.BOT_TOKEN))
    except KeyboardInterrupt:
        loop.run_until_complete(bot.close())
    finally:
        loop.close()

class WorkThread(QtCore.QThread):
    threadSignal = QtCore.pyqtSignal(int)

    def __init__(self):
        super().__init__()

    def run(self):
        i = ''
        self.msleep(300)

        self.threadSignal.connect(START)
        self.threadSignal.emit(i)

V
Vindicar, 2021-06-13
@Vindicar

I don't know how threadSignal.connect() works, but I do know that bot.run() won't return until the bot has exited.
Those. from the BOT_RUN function, the flow of control will not return for a very long time.
Perhaps it's worth rescheduling which thread does what? Allocate a separate thread for bot.run()?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question