-2

I found some other question about this subject but I didn't understand any of them.

I am trying to make a program which sends a message every x minutes but because of the time.sleep in the loop the program isn't responding anymore. I tried multi-threading but it doesn't seems to work. I also found root.after but I think that I can't use this inside a while loop.

Anyway here is my code:
(auto-bump means sending !d bump message in discord)

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from time import sleep
import re
import urllib.request
import json
import tkinter as tk
from tkinter import *
import webbrowser
from selenium.webdriver.common.by import By
from tkinter import simpledialog
import threading
from queue import Queue


browser = webdriver.Chrome()
browser.get('https://discord.com/')
driver = browser


# open file
file = open('settings.txt')
all_lines = file.readlines()


def login(emailInput, passwordInput):
    file = open('settings.txt')
    if emailInput == "":
        emailInput = (all_lines[1])
        emailInput = emailInput.replace("\n", "")
    else:
        pass
    if passwordInput == "":
        passwordInput = (all_lines[3])
        passwordInput = passwordInput.replace("\n", "")
    else:
        pass #  If email and password not in entrys => replace by settings.txt credentials else replace settings credentials by entrys
    print(f"Logging in ...({emailInput}:{passwordInput})")
    try: 
        browser.find_element_by_xpath('/html/body/div/div/div/div/header[1]/nav/ul[2]/li[4]/a').click()
        browser.find_element_by_name('email').send_keys(emailInput) # EMAIL
        browser.find_element_by_name('password').send_keys(passwordInput)# PASSWORD
        browser.find_element_by_xpath('/html/body/div/div[2]/div/div[2]/div/div/form/div/div/div[1]/div[3]/div[2]/div/input').send_keys(Keys.ENTER) # CONNNECT
    except:
        print("Error while logging in.\nPlease retry or login manually")


def automsg(cldown):
    if cldown == "":
        cldown = (all_lines[5])
        cldown = cldown.replace("\n", "")
        cldown = int(cldown)
    else:
        pass
    message = (all_lines[7])
    print(message)
    cldown = int(cldown)
    cldown = (cldown * 60)
    while True:
        try:
            chatentry = browser.find_element_by_css_selector(".slateTextArea-1Mkdgw")
            chatentry.send_keys(message)
            chatentry.send_keys(Keys.ENTER)
            print("Message sent successfully !")
        except:
            print("Message failed, trying again ...\n")
            sleep(5)
        print(f"Next message in {cldown} secondes")
        sleep(cldown)


def autobump(cldown):
    if cldown == "":
        cldown = (all_lines[5])
        cldown = cldown.replace("\n", "")
        cldown = int(cldown)
    else:
        pass
    print(cldown)
    cldown = int(cldown)
    cldown = (cldown * 60)
    while True:
        try:
            chatentry = browser.find_element_by_css_selector(".slateTextArea-1Mkdgw")
            chatentry.send_keys("!d bump")
            chatentry.send_keys(Keys.ENTER)
            print("Bumped successfully !")
        except:
            print("Bumped failed, trying again ...\n")
            sleep(5)
        print(f"Next bump in {cldown} secondes")
        sleep(cldown)

def help():
    webbrowser.open('https://pastebin.com/5Vx5qxxx', new=2)



# Looks settings


HEIGHT = 500
WIDTH = 500


root = tk.Tk()
root.title("Discord Assistant (by Sarlay#3151)")
root.minsize(width=WIDTH, height=HEIGHT)
root.maxsize(width=WIDTH, height=HEIGHT)
root.iconbitmap("icon.ico")


canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()


background_image = tk.PhotoImage(file=r"./bg.gif")
background_label = tk.Label(root, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)


SettingsFrame = tk.Frame(root, bg="white")
SettingsFrame.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.05)

SettingsFrame2 = tk.Frame(root, bg="#24b8b8")
SettingsFrame2.place(relx=0.1, rely=0.15, relwidth=0.8, relheight=0.05)


emailEntry = tk.Entry(SettingsFrame2, font=8)
emailEntry.place(relx=0.02, rely=0.15, relwidth=0.35, relheight=0.8)

emaillabel = tk.Label(SettingsFrame, font=40, text="email")
emaillabel.place(relx=0.15, rely=0.2)

passwordEntry = tk.Entry(SettingsFrame2, font=8)
passwordEntry.place(relx=0.4, rely=0.15, relwidth=0.25, relheight=0.8)

passwordlabel = tk.Label(SettingsFrame, font=40, text="password")
passwordlabel.place(relx=0.44, rely=0.2)

cooldownEntry = tk.Entry(SettingsFrame2, font=8)
cooldownEntry.place(relx=0.67, rely=0.15, relwidth=0.3, relheight=0.8)

cooldownlabel = tk.Label(SettingsFrame, font=8, text="cooldown(min)")
cooldownlabel.place(relx=0.69, rely=0.2)

LoginButton = tk.Button(root, text="Login", font=40, command=lambda: login(emailEntry.get(), passwordEntry.get()))
LoginButton.place(relx=0.46, rely=0.3)

AutoMsgButton = tk.Button(root, text="Auto-message", font=40, command=lambda: automsg(cooldownEntry.get()))
AutoMsgButton.place(relx=0.4, rely=0.4)


HelpButton = tk.Button(root, text="?", font=40, command=help)
HelpButton.place(relx=0.95, rely=0.03)


AutobumpButton = tk.Button(root, text="Auto-Bump", font=40, command=lambda: autobump(cooldownEntry.get()))
AutobumpButton.place(relx=0.42, rely=0.5)

root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Sarlay
  • 15
  • 4
  • 1
    Event handler function needs to finish quickly, since until it complete, no other GUI events can be processed. Both sleep and an infinite loop are a big no-no there. See here: https://stackoverflow.com/questions/25753632/tkinter-how-to-use-after-method on how you can have things happen periodically without blocking the event loop. – Dan Mašek Jun 14 '20 at 13:10
  • 1
    You need to use the universal [`after()`](https://web.archive.org/web/20190222214221id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html) method not `time.sleep()` to introduce delays in tkinter apps and prevent them from "hanging" because its `mainloop()` is being interfered-with. – martineau Jun 14 '20 at 13:20

1 Answers1

-2

Multithreading seems a bit overkill for this problem. You could try something like this.

import time
send_message()
last_message = time.time() 
while:
    do_stuff()
    if time.time()-last_message > 60*x:  # time.time() - last_message gives the difference in seconds between now and when the last message was send.
        send_new_message()
        last_message = time.time()
debsim
  • 582
  • 4
  • 19