Basic threading
This commit is contained in:
parent
dcbb847282
commit
b714658a27
|
@ -20,9 +20,14 @@ sp = pUp(s)
|
|||
# script start
|
||||
import tkinter
|
||||
import tkinter.ttk
|
||||
import tkinter.messagebox
|
||||
import threading
|
||||
import logging
|
||||
import configparser
|
||||
import asyncio
|
||||
import discord
|
||||
import time
|
||||
from discord.ext import tasks
|
||||
distro = "Discord but fast"
|
||||
version = (0,0,0)
|
||||
versionString = ".".join(map(str,version))
|
||||
|
@ -30,6 +35,13 @@ pathLogins = p(sp,"logins")
|
|||
initialized = False
|
||||
connected = False
|
||||
|
||||
guiTasks = []
|
||||
guiCreationTasks = []
|
||||
guiLock = threading.Lock()
|
||||
|
||||
def addGuiTask(gui,func,args):
|
||||
guiTasks.append((gui,func,args))
|
||||
|
||||
def getTitle(text = False):
|
||||
title = ""
|
||||
if text == False:
|
||||
|
@ -149,14 +161,16 @@ class guiLoginChooser:
|
|||
|
||||
def confirm(self):
|
||||
if self.dropdownLoginChoice.get() == "":
|
||||
import tkinter.messagebox as messagebox
|
||||
messagebox.showinfo(title=getTitle("Info"), message='Please choose a login from the dropdown menu, or create one by pressing "New"!')
|
||||
tkinter.messagebox.showinfo(title=getTitle("Info"), message='Please choose a login from the dropdown menu, or create one by pressing "New"!')
|
||||
return
|
||||
self.login = self.dropdownLoginChoice.get()
|
||||
self.window.destroy()
|
||||
|
||||
class guiLoginProgress:
|
||||
def __init__(self):
|
||||
self.type = "loginProgress"
|
||||
self.closed = False
|
||||
|
||||
self.window = tkinter.Tk()
|
||||
self.title = "Logging in..."
|
||||
self.width = 320
|
||||
|
@ -205,6 +219,10 @@ class guiLoginProgress:
|
|||
self.resizeElements()
|
||||
|
||||
def onClose(self):
|
||||
self.closeGui()
|
||||
|
||||
def closeGui(self):
|
||||
self.closed = True
|
||||
self.window.destroy()
|
||||
|
||||
class guiConversationList:
|
||||
|
@ -461,6 +479,115 @@ class guiConversation:
|
|||
self.pendingMessages.append(messageText)
|
||||
self.messagebox.delete("1.0","end")
|
||||
|
||||
def discordClient(token):
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
|
||||
global client
|
||||
client = discord.Client()
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
print("on_ready")
|
||||
|
||||
global initialized
|
||||
if initialized == False:
|
||||
guiLock.acquire()
|
||||
for gui in openGuis:
|
||||
if gui.type == "loginProgress": addGuiTask(gui,gui.closeGui,())
|
||||
|
||||
addGuiTask("create",guiConversationList,())
|
||||
guiLock.release()
|
||||
guiLoop.start()
|
||||
conversationUpdate.start()
|
||||
initialized = True
|
||||
|
||||
global connected
|
||||
connected = True
|
||||
|
||||
guiLock.acquire()
|
||||
for gui in openGuis: addGuiTask(gui,gui.window.title,(getTitle(gui.title),))
|
||||
guiLock.release()
|
||||
|
||||
await client.change_presence(status=discord.Status.dnd, activity=discord.CustomActivity("working on discord but fast"))
|
||||
|
||||
@client.event
|
||||
async def on_resumed():
|
||||
print("on_resumed")
|
||||
|
||||
global connected
|
||||
connected = True
|
||||
|
||||
guiLock.acquire()
|
||||
for gui in openGuis: addGuiTask(gui,gui.window.title,(getTitle(gui.title),))
|
||||
guiLock.release()
|
||||
|
||||
@client.event
|
||||
async def on_disconnect():
|
||||
print("on_disconnect")
|
||||
|
||||
global connected
|
||||
connected = False
|
||||
|
||||
guiLock.acquire()
|
||||
for gui in openGuis: addGuiTask(gui,gui.window.title,(getTitle(gui.title),))
|
||||
guiLock.release()
|
||||
|
||||
@client.event
|
||||
async def on_message(message):
|
||||
guiLock.acquire()
|
||||
for gui in openGuis:
|
||||
if gui.type == "conversation":
|
||||
if message.channel != gui.channel: continue
|
||||
addGuiTask(gui,gui.addMessage,(message,))
|
||||
addGuiTask(gui,gui.fillMessageLog,())
|
||||
guiLock.release()
|
||||
|
||||
@tasks.loop(seconds=0.033)
|
||||
async def guiLoop():
|
||||
guiLock.acquire()
|
||||
length = len(openGuis)
|
||||
index = 0
|
||||
while index < length:
|
||||
gui = openGuis[index]
|
||||
if gui.type == "conversation":
|
||||
if gui.readied == False:
|
||||
try:
|
||||
messages = await gui.channel.history(limit=int(config["performance"]["historyFetch"])).flatten()
|
||||
for message in reversed(messages):
|
||||
addGuiTask(gui,gui.addMessage,(message,))
|
||||
except:
|
||||
addGuiTask(gui,gui.addMessage,(["","Fetching messages failed."],))
|
||||
|
||||
addGuiTask(gui,gui.fillMessageLog,())
|
||||
gui.readied = True
|
||||
|
||||
while len(gui.pendingMessages) > 0:
|
||||
message = ""
|
||||
try:
|
||||
message = gui.pendingMessages.pop(0)
|
||||
await gui.channel.send(message)
|
||||
except:
|
||||
print("error sending message")
|
||||
|
||||
index = index + 1
|
||||
guiLock.release()
|
||||
|
||||
@tasks.loop(seconds=1.0)
|
||||
async def conversationUpdate():
|
||||
channels = client.private_channels
|
||||
|
||||
for gui in openGuis:
|
||||
if gui.type != "conversationList": continue
|
||||
if gui.closed == True: continue
|
||||
if channels == gui.convs: continue
|
||||
guiLock.acquire()
|
||||
addGuiTask(gui,gui.updateConvs,(channels,))
|
||||
guiLock.release()
|
||||
|
||||
client.run(token,bot = False)
|
||||
|
||||
def main():
|
||||
global config
|
||||
config = configparser.ConfigParser()
|
||||
|
@ -477,120 +604,55 @@ def main():
|
|||
if login == "": return
|
||||
token = False
|
||||
|
||||
global iLoginProgress
|
||||
iLoginProgress = guiLoginProgress()
|
||||
iLoginProgress.addToLog("Fetching token...")
|
||||
global openGuis
|
||||
openGuis = []
|
||||
loginProgressGui = guiLoginProgress()
|
||||
openGuis.append(loginProgressGui)
|
||||
loginProgressGui.addToLog("Fetching token...")
|
||||
|
||||
with open(p(pathLogins,login,"token"),"r") as fileh:
|
||||
token = fileh.read()
|
||||
|
||||
iLoginProgress.addToLog("\nLoading discord.py...")
|
||||
global discord
|
||||
import discord
|
||||
global tasks
|
||||
from discord.ext import tasks
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
iLoginProgress.addToLog("\nConnecting...")
|
||||
global client
|
||||
client = discord.Client()
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
print("on_ready")
|
||||
global initialized
|
||||
if initialized == False:
|
||||
iLoginProgress.window.destroy()
|
||||
global openGuis
|
||||
openGuis = []
|
||||
openGuis.append(guiConversationList())
|
||||
guiLoop.start()
|
||||
conversationUpdate.start()
|
||||
initialized = True
|
||||
|
||||
global connected
|
||||
connected = True
|
||||
|
||||
for gui in openGuis:
|
||||
if gui.closed == True: continue
|
||||
gui.window.title(getTitle(gui.title))
|
||||
|
||||
await client.change_presence(status=discord.Status.dnd, activity=discord.CustomActivity("working on discord but fast"))
|
||||
|
||||
@client.event
|
||||
async def on_resumed():
|
||||
print("on_resumed")
|
||||
global connected
|
||||
connected = True
|
||||
|
||||
for gui in openGuis:
|
||||
if gui.closed == True: continue
|
||||
gui.window.title(getTitle(gui.title))
|
||||
|
||||
@client.event
|
||||
async def on_disconnect():
|
||||
print("on_disconnect")
|
||||
|
||||
global connected
|
||||
connected = False
|
||||
|
||||
for gui in openGuis:
|
||||
if gui.closed == True: continue
|
||||
gui.window.title(getTitle(gui.title))
|
||||
|
||||
@client.event
|
||||
async def on_message(message):
|
||||
for gui in openGuis:
|
||||
if gui.closed == True: continue
|
||||
if gui.type == "conversation":
|
||||
if message.channel != gui.channel: continue
|
||||
gui.addMessage(message)
|
||||
gui.fillMessageLog()
|
||||
|
||||
@tasks.loop(seconds=0.033)
|
||||
async def guiLoop():
|
||||
length = len(openGuis)
|
||||
loginProgressGui.addToLog("\nConnecting...")
|
||||
threading.Thread(target=discordClient,args=(token,)).start()
|
||||
while True:
|
||||
guiLock.acquire()
|
||||
index = 0
|
||||
while index < length:
|
||||
length = len(openGuis)
|
||||
while (index < length):
|
||||
gui = openGuis[index]
|
||||
if gui.closed == True:
|
||||
del openGuis[index]
|
||||
length = length - 1
|
||||
length -= 1
|
||||
continue
|
||||
|
||||
if gui.type == "conversation":
|
||||
if gui.readied == False:
|
||||
try:
|
||||
messages = await gui.channel.history(limit=int(config["performance"]["historyFetch"])).flatten()
|
||||
for message in reversed(messages):
|
||||
gui.addMessage(message)
|
||||
except:
|
||||
gui.addMessage(["","Fetching messages failed."])
|
||||
gui.fillMessageLog()
|
||||
gui.readied = True
|
||||
|
||||
while len(gui.pendingMessages) > 0:
|
||||
message = ""
|
||||
try:
|
||||
message = gui.pendingMessages.pop(0)
|
||||
await gui.channel.send(message)
|
||||
except:
|
||||
print("error sending message")
|
||||
index += 1
|
||||
|
||||
length = len(guiTasks)
|
||||
while length > 0:
|
||||
task = guiTasks[0]
|
||||
gui = task[0]
|
||||
func = task[1]
|
||||
args = task[2]
|
||||
|
||||
gui.window.update()
|
||||
index = index + 1
|
||||
|
||||
@tasks.loop(seconds=1.0)
|
||||
async def conversationUpdate():
|
||||
channels = client.private_channels
|
||||
del guiTasks[0]
|
||||
length -= 1
|
||||
|
||||
if gui == "create":
|
||||
openGuis.append(func(*args))
|
||||
continue
|
||||
|
||||
if gui != False:
|
||||
if not gui in openGuis: continue
|
||||
if gui.closed == True: continue
|
||||
func(*args)
|
||||
|
||||
for gui in openGuis:
|
||||
if gui.type != "conversationList": continue
|
||||
if gui.closed == True: continue
|
||||
if channels == gui.convs: continue
|
||||
gui.updateConvs(channels)
|
||||
|
||||
client.run(token,bot = False)
|
||||
gui.window.update()
|
||||
guiLock.release()
|
||||
|
||||
time.sleep(0.033)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Reference in New Issue