-
Notifications
You must be signed in to change notification settings - Fork 1
/
driver.py
254 lines (208 loc) · 9.52 KB
/
driver.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import time
import re
import os
import logging
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class JKLMBot:
def __init__(self, room_code):
self.room_code = room_code
self.driver = None
self.wordlist = []
# Initialize wordlist from file
self.load_wordlist()
# Pre-compile the regular expression pattern for !helpbot command
self.helpbot_pattern = re.compile(r'^!helpbot:([a-zA-Z]{2,3})$')
''' OLD INIT FUNCTION
def __init__(self, room_code):
self.room_code = room_code
self.driver = None
self.wordlist = []
wordlist_file = "wordlist.txt"
if os.path.exists(wordlist_file):
with open(wordlist_file, "r") as file:
self.wordlist = file.read().splitlines()
else:
logging.warning(f"Wordlist file '{wordlist_file}' not found.")
'''
#This function is new just to optimize the loading of the wordlist file
#It makes the process a bit easier to visualise and a bit more user-friendly
def load_wordlist(self):
wordlist_file = "wordlist.txt"
if os.path.exists(wordlist_file):
with open(wordlist_file, "r") as file:
self.wordlist = file.read().splitlines()
else:
logging.warning(f"Wordlist file '{wordlist_file}' not found.")
def setup_driver(self):
service = Service(executable_path="chromedriver.exe")
options = webdriver.ChromeOptions()
options.set_capability('goog:loggingPrefs', {'browser': 'ALL'})
self.driver = webdriver.Chrome(service=service, options=options)
link = f"https://jklm.fun/{self.room_code}"
self.driver.get(link)
print("Driver setup complete and navigated to the room.")
def enter_room(self):
try:
WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CLASS_NAME, "styled.nickname"))
)
nickname_input = self.driver.find_element(By.CLASS_NAME, "styled.nickname")
nickname_input.clear()
nickname_input.send_keys("HelpBot by AgusCode", Keys.ENTER)
print("Entered the room with nickname.")
time.sleep(3)
except Exception as e:
print(f"ERROR entering room: {e}")
def present(self):
try:
message = " ⛑️ Hey! I'm ready to help everyone in this room, command: !helpbot:(syllable) ⛑️ .\n Example: !helpbot:mis"
WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "textarea[data-placeholder-text='typeHereToChat']"))
)
textarea = self.driver.find_element(By.CSS_SELECTOR, "textarea[data-placeholder-text='typeHereToChat']")
textarea.send_keys(message, Keys.ENTER)
print("Sent presentation message.")
time.sleep(5)
except Exception as e:
print(f"ERROR presenting: {e}")
def inject_mutation_observer(self):
script = """
var targetNode = document.querySelector('.log.darkScrollbar');
if (!targetNode) {
console.error("Target node not found. MutationObserver not injected.");
return;
}
var config = { childList: true, subtree: true };
var callback = function(mutationsList, observer) {
mutationsList.forEach(function(mutation) {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(function(newNode) {
var textNode = newNode.querySelector('.text');
if (textNode) {
console.log('NEW_MESSAGE: ' + textNode.innerText);
}
});
}
});
};
var observer = new MutationObserver(callback);
observer.observe(targetNode, config);
console.log("MutationObserver injected successfully.");
"""
try:
self.driver.execute_script(script)
print("Injected MutationObserver script.")
except Exception as e:
print(f"Error injecting MutationObserver script: {e}")
''' OLD MUTATION INJECTION METHOD
def inject_mutation_observer(self):
script = """
var targetNode = document.querySelector('.log.darkScrollbar');
var config = { childList: true, subtree: true };
var callback = function(mutationsList, observer) {
for(var mutation of mutationsList) {
if (mutation.type === 'childList') {
var newNodes = mutation.addedNodes;
for (var i = 0; i < newNodes.length; i++) {
var newNode = newNodes[i];
// Check if the newNode has a class 'text'
var textNode = newNode.querySelector('.text');
if (textNode) {
console.log('NEW_MESSAGE: ' + textNode.innerText);
}
}
}
}
};
var observer = new MutationObserver(callback);
observer.observe(targetNode, config);
console.log("MutationObserver injected successfully.");
"""
self.driver.execute_script(script)
print("Injected MutationObserver script.")
'''
def capture_messages(self):
while True:
logs = self.driver.get_log('browser')
for log in logs:
if 'NEW_MESSAGE:' in log['message']:
message = log['message'].split('NEW_MESSAGE:', 1)[1].strip()
message = message[:-1]
print("New message captured:", message)
self.process_message(message)
time.sleep(1) # Small pause to avoid an overly fast loop
def process_message(self, message):
if message == '!helpbot':
self.send_message("Helpbot guide - Type '!helpbot:[syllable]' so I can help you find words!")
match = self.helpbot_pattern.match(message.lower())
if match:
letters = match.group(1).lower()
print("Let the magic happen...")
self.help_user(letters)
''' [ORIGINAL PROCESS MESSAGE METHOD]
def process_message(self, message):
if message == '!helpbot':
self.send_message("Helpbot guide - Type '!helpbot:[syllable]' so i can help you find words!")
# Define a regex pattern to match '!helpbot:xx' or '!helpbot:xxx' where xx or xxx can be any letters
pattern = r'^!helpbot:([a-zA-Z]{2,3})$'
# Check if the message matches the pattern
match = re.match(pattern, message.lower())
if match:
letters = match.group(1).lower() # Extract the letters xx or xxx
print("Ayudando...")
self.help_user(letters)
'''
def help_user(self, letters, max_words=10):
words_by_prefix = {}
for word in self.wordlist:
prefix = word[:len(letters)].lower()
if prefix == letters:
words_by_prefix.setdefault(word[0].lower(), []).append(word.capitalize())
if len(words_by_prefix) >= max_words:
break
possible_words = [word for sublist in words_by_prefix.values() for word in sublist]
if possible_words:
word_list_message = "\n".join(possible_words[:max_words]) # Limit to max_words
self.send_message(f"Don't worry! Here are some words that might help:\n{word_list_message}")
else:
self.send_message("Sorry, I couldn't find any words matching that criteria.")
'''
def help_user(self, letters):
words_by_prefix = {}
for word in self.wordlist:
prefix = word[:len(letters)].lower()
if prefix == letters:
words_by_prefix.setdefault(word[0].lower(), []).append(word.capitalize())
if len(words_by_prefix) >= 6:
break
possible_words = [word for sublist in words_by_prefix.values() for word in sublist]
if possible_words:
word_list_message = "\n".join(possible_words[:6]) # Limit to first 6 words
self.send_message(f"Don't worry! Here are some words that might help:\n{word_list_message}")
else:
self.send_message("Sorry, I couldn't find any words matching that criteria.")
'''
def send_message(self, message):
try:
WebDriverWait(self.driver, 20).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "textarea[data-placeholder-text='typeHereToChat']"))
)
textarea = self.driver.find_element(By.CSS_SELECTOR, "textarea[data-placeholder-text='typeHereToChat']")
textarea.send_keys(message, Keys.ENTER)
print(f"Bot response sent: {message}")
except Exception as e:
print(f"ERROR sending message: {e}")
def run(self):
self.setup_driver()
self.enter_room()
self.present()
self.inject_mutation_observer()
self.capture_messages()
if __name__ == "__main__":
bot = JKLMBot("your_room_code_here")
bot.run()