-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
277 lines (219 loc) · 8.32 KB
/
gui.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import re
import time
import customtkinter as ctk
import subprocess
import os
import json
import psutil
from dotenv import load_dotenv
from tkinter import messagebox
from datetime import datetime
load_dotenv()
def load_json(filename) -> dict:
"""Load data from a JSON file."""
try:
if os.path.exists(filename):
with open(filename, "r") as file:
return json.load(file)
return {}
except (FileNotFoundError, json.JSONDecodeError) as e:
print(f"Error loading {filename}: {e}")
return {}
def save_json(filename, data) -> None:
"""Save data to a JSON file."""
try:
with open(filename, "w") as file:
json.dump(data, file, indent=4)
except Exception as e:
print(f"Error saving {filename}: {e}")
def terminate_bot_processes() -> None:
"""checks/Terminates any existing bot processes."""
current_directory = os.getcwd()
for proc in psutil.process_iter(["pid", "name", "exe", "cmdline"]):
try:
if (
proc.info["name"] in ["python", "python3"]
and "main.py" in proc.info["cmdline"]
):
if proc.info["exe"] and current_directory in proc.info["exe"]:
print(
f"Terminating process: {proc.info['pid']}, {proc.info['cmdline']}"
)
proc.terminate()
break
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
def start_bot() -> None:
"""Starts the bot"""
if not check_settings():
return
try:
terminate_bot_processes()
log_file = open("bot_log.log", "a")
global bot_start_time
bot_start_time = datetime.now()
process = subprocess.Popen(
["python3", "main.py"],
cwd=os.getcwd(),
env=os.environ.copy(),
stdout=log_file,
stderr=log_file,
)
messagebox.showinfo("Success", "Bot started.")
time.sleep(3)
root.after(500, update_bot_username)
except Exception as e:
messagebox.showerror("Error", f"Failed to start the bot: {str(e)}")
def save_settings() -> None:
"""Save the current settings."""
try:
api_key = api_key_entry.get()
username = username_entry.get()
shock_code = shock_code_entry.get()
token = token_entry.get()
with open(".env", "w") as f:
f.write(f"SHOCKER_APIKEY={api_key}\n")
f.write(f"SHOCKER_USERNAME={username}\n")
f.write(f"SHOCKER_CODE={shock_code}\n")
f.write(f"DISCORD_TOKEN={token}\n")
whitelist_data = {
"whitelist": [
int(user_id.strip()) for user_id in whitelist_entry.get().split(",")
]
}
wordlist_data = {
"words": [word.strip() for word in wordlist_entry.get().split(",")]
}
save_json("whitelist.json", whitelist_data)
save_json("wordlist.json", wordlist_data)
messagebox.showinfo("Success", "Settings saved successfully!")
except Exception as e:
custom_error(f"Failed to save settings: {str(e)}")
def custom_error(message: str) -> None:
"""displays the custom error"""
error_popup = ctk.CTkToplevel()
error_popup.title("Error")
error_popup.geometry("300x200")
error_popup.resizable(False, False)
error_popup.configure(bg_color="#FF4C4C")
label = ctk.CTkLabel(
error_popup,
text=message,
text_color="white",
font=("Arial", 14, "bold"),
width=400,
)
label.pack(pady=30)
close_button = ctk.CTkButton(
error_popup,
text="Close",
command=error_popup.destroy,
fg_color="#FF1C1C",
hover_color="#FF4040",
width=120,
height=40,
corner_radius=10,
font=("Arial", 12, "bold"),
)
close_button.pack(pady=10)
error_popup.mainloop()
def check_settings() -> bool:
"""checks if all required settings are filled out."""
if not all(
[
api_key_entry.get(),
username_entry.get(),
shock_code_entry.get(),
token_entry.get(),
]
):
custom_error("Please fill out all the settings.")
return False
return True
def autofill_settings() -> None:
"""Autofill the settings."""
api_key = os.getenv("SHOCKER_APIKEY")
username = os.getenv("SHOCKER_USERNAME")
shock_code = os.getenv("SHOCKER_CODE")
token = os.getenv("DISCORD_TOKEN")
whitelist_data = load_json("whitelist.json")
wordlist_data = load_json("wordlist.json")
api_key_entry.insert(0, api_key or "")
username_entry.insert(0, username or "")
shock_code_entry.insert(0, shock_code or "")
token_entry.insert(0, token or "")
if "whitelist" in whitelist_data:
whitelist_entry.insert(
0, ", ".join(str(user_id) for user_id in whitelist_data["whitelist"])
)
if "words" in wordlist_data:
wordlist_entry.insert(0, ", ".join(wordlist_data["words"]))
def update_bot_username() -> None:
"""Update the bot's username on login"""
with open("bot_log.log", "r") as log_file:
log_lines = log_file.readlines()
# hacky but eh
bot_username = None
for line in reversed(log_lines):
match = re.search(r"Logged on as (\S+#\d+)", line)
if match:
bot_username = match.group(1)
break
if bot_username:
bot_username_label.configure(text=f"Logged in as: {bot_username}")
return
root.after(1200, update_bot_username)
def update_uptime() -> None:
"""Update the bot's uptime."""
if "bot_start_time" in globals():
elapsed_time = datetime.now() - bot_start_time
hours, remainder = divmod(elapsed_time.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
uptime = f"{elapsed_time.days}d {hours}h {minutes}m {seconds}s"
uptime_label.configure(text=f"Uptime: {uptime}")
root.after(1000, update_uptime) # Updates every second
ctk.set_appearance_mode("Dark")
ctk.set_default_color_theme("blue")
root = ctk.CTk()
root.title("PiShock SB Settings")
root.geometry("400x380")
root.resizable(True, False)
bot_username_label = ctk.CTkLabel(root, text="Bot Username: waiting...", anchor="w")
bot_username_label.grid(row=0, column=0, columnspan=2, padx=10, pady=5, sticky="w")
uptime_label = ctk.CTkLabel(root, text="Uptime: 0d 0h 0m 0s", anchor="w")
uptime_label.grid(row=1, column=0, columnspan=2, padx=10, pady=5, sticky="w")
api_key_label = ctk.CTkLabel(root, text="API Key:")
api_key_label.grid(row=2, column=0, padx=10, pady=5, sticky="w")
api_key_entry = ctk.CTkEntry(root)
api_key_entry.grid(row=2, column=1, padx=10, pady=5)
username_label = ctk.CTkLabel(root, text="Username:")
username_label.grid(row=3, column=0, padx=10, pady=5, sticky="w")
username_entry = ctk.CTkEntry(root)
username_entry.grid(row=3, column=1, padx=10, pady=5)
shock_code_label = ctk.CTkLabel(root, text="Shock Code:")
shock_code_label.grid(row=4, column=0, padx=10, pady=5, sticky="w")
shock_code_entry = ctk.CTkEntry(root)
shock_code_entry.grid(row=4, column=1, padx=10, pady=5)
token_label = ctk.CTkLabel(root, text="Discord Token:")
token_label.grid(row=5, column=0, padx=10, pady=5, sticky="w")
token_entry = ctk.CTkEntry(root)
token_entry.grid(row=5, column=1, padx=10, pady=5)
whitelist_label = ctk.CTkLabel(root, text="Whitelist (comma-separated IDs):")
whitelist_label.grid(row=6, column=0, padx=10, pady=5, sticky="w")
whitelist_entry = ctk.CTkEntry(root)
whitelist_entry.grid(row=6, column=1, padx=10, pady=5)
wordlist_label = ctk.CTkLabel(root, text="Wordlist (comma-separated words):")
wordlist_label.grid(row=7, column=0, padx=10, pady=5, sticky="w")
wordlist_entry = ctk.CTkEntry(root)
wordlist_entry.grid(row=7, column=1, padx=10, pady=5)
start_button = ctk.CTkButton(root, text="Start Bot", command=start_bot)
start_button.grid(row=8, column=0, padx=10, pady=5)
save_button = ctk.CTkButton(root, text="Save Settings", command=save_settings)
save_button.grid(row=8, column=1, padx=10, pady=5)
autofill_button = ctk.CTkButton(
root, text="Autofill Settings", command=autofill_settings
)
autofill_button.grid(row=9, column=0, padx=10, pady=5, columnspan=2)
root.after(1000, update_uptime)
autofill_settings() # Autofill settings on start
root.mainloop()