Want to describe my task firstly. My aim is to create Discord bot that sends a particular message exported from .txt in chat with a time interval of N seconds therefore I'm currently trying to use libraries such as discord, pyqt6 and asyncio.
I've tried to use time library but the problem is that time.sleep() delays all processes so the GUI becomes unusable and I cannot implement other constructions due to my lack of experience (I swear I tried my best). Hence I tried using asyncio and I found it suitable because it will not delay GUI.
My code includes a class with different methods and finally executes in main() but I cannot comprehend how am I supposed to execute method async def func() whereas every other functions does not demand async execution. Here is my code. I will be super grateful for every advise or connected literature because I've been browsing the internet for a couple days now and I still found barely anything to comprehend. Thank you.
class DiscordBot(QtWidgets.QMainWindow, widget.Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.start_button.clicked.connect(self.printer)
def __getitem__(self, item):
file = open('phrases.txt')
a = file.read().split('\n')
for item in range(len(a)):
yield a[item]
file.close()
async def printer(self):
self.listWidget.clear()
i = 0
a = DiscordBot.__getitem__(self, item=i)
for i in a:
self.listWidget.addItem(DiscordBot.__getitem__(self, item=i))
await asyncio.sleep(5)
def main():
app = QtWidgets.QApplication(sys.argv)
window = DiscordBot()
window.show()
app.exec()
asyncio.run(window.printer())
if __name__ == '__main__':
main()