-
Notifications
You must be signed in to change notification settings - Fork 4
/
discordapp.py
86 lines (67 loc) · 2.46 KB
/
discordapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import time
default_delay = 0.5
default_timeout = 10
login_url = 'https://discordapp.com/login'
class Discordapp:
def __init__(self):
# Create instance of Chrome driver without GUI
options = webdriver.ChromeOptions()
options.headless = True
options.add_argument('--incognito')
options.add_argument('--disable-logging')
self.driver = webdriver.Chrome(options=options)
# Define timing attributes and set to default values
self.delay = default_delay
self.timeout = default_timeout
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
# Close Chrome driver
self.driver.quit()
def load(self, url: str) -> bool:
# Navigate to server channel
self.driver.get(url)
# Wait for redirect away from server channel
try:
wait = WebDriverWait(self.driver, self.timeout)
wait.until_not(EC.url_to_be(url))
# Redirect; return False
return False
except:
# No redirect; return True
return True
def login(self, email: str, password: str) -> bool:
# Navigate to login page
self.driver.get(login_url)
# Create WebDriverWait object
wait = WebDriverWait(self.driver, self.timeout)
# Wait for login form to load
try:
wait.until(EC.presence_of_element_located((By.TAG_NAME, 'input')))
except:
return False
# Find login input elements
inputs = self.driver.find_elements_by_tag_name('input')
# Input email address and password then return
inputs[0].send_keys(email)
inputs[1].send_keys(password, Keys.RETURN)
# Wait for redirect away from login URL
try:
wait.until_not(EC.url_to_be(login_url))
return True
except:
return False
def send(self, message: str) -> None:
try:
# Send message
ActionChains(self.driver).send_keys(message, Keys.RETURN)
# Delay for processing
time.sleep(self.delay)
except:
pass