-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
138 lines (113 loc) · 4.66 KB
/
main.py
File metadata and controls
138 lines (113 loc) · 4.66 KB
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
import requests, json, os, zipfile, shutil, asyncio, sys
from typing import List
UPDATE_FILE = "assets.zip"
class BotManager:
def __init__(self):
with open("setting/config.json") as f:
self.config = json.load(f)
self.version = self.config.get("version", "0.0.0")
def update(self):
latest = self.check_update()
if latest:
if input("\nEnter 'Y' or 'Yes' to update: ").lower() not in ['y', 'yes']:
print("Update cancelled")
return False
self.download_update(latest)
return True
return False
def check_update(self):
try:
res = requests.post(f"{self.config['api']}/update/check",
json = {'key': self.config['key'], 'version': self.version})
data = res.json()
if res.status_code == 200:
print(f"New version available: {data['latest_version']}\n{data['description']}")
return data['latest_version']
elif res.status_code == 304:
print(f"Already latest version ({self.version})")
return None
else:
print(f"Update check failed: {res.status_code}")
return None
except Exception as e:
print(f"Update check error: {e}")
return None
def download_update(self, latest):
print("Updating...")
try:
with open(UPDATE_FILE, "wb") as f:
f.write(requests.get(f"{self.config['api']}/update/download").content)
if os.path.exists("assets"):
shutil.rmtree("assets")
with zipfile.ZipFile(UPDATE_FILE) as zf:
zf.extractall(".")
os.remove(UPDATE_FILE)
self.config['version'] = latest
with open("setting/config.json", "w") as f:
json.dump(self.config, f, indent = 2)
print("Update complete")
except Exception as e:
print(f"Update failed: {e}")
async def run_bot(self, bot_type: str, config_path: str):
if bot_type == 'owo':
from assets.modules.selfbot.owo.client import OwOSelfbot
ClientClass = OwOSelfbot
else:
print(f"Unknown bot type: {bot_type}")
return
tokens = self._load_tokens(config_path)
if not tokens:
print(f"No tokens in {config_path}")
return
clients = []
tasks = []
for i, token in enumerate(tokens):
try:
print(f"Starting client {i + 1}/{len(tokens)}")
client = ClientClass(clients, token)
clients.append(client)
if i > 0:
await asyncio.sleep(2 + i * 0.5)
tasks.append(asyncio.create_task(self._run_client_safe(client, token)))
except Exception as e:
print(f"Client init failed: {e}")
if tasks:
print(f"Running {len(tasks)} clients")
await asyncio.gather(*tasks, return_exceptions = True)
else:
print(f"No valid {bot_type} tokens")
def _load_tokens(self, config_path: str) -> List[str]:
try:
with open(config_path) as f:
data = json.load(f)
return list(data.keys()) if isinstance(data, dict) else []
except Exception as e:
print(f"Token load failed: {e}")
return []
async def _run_client_safe(self, client, token: str):
try:
await client.start(token, reconnect = True)
except Exception as e:
print(f"Client error: {e}")
try:
await asyncio.sleep(30)
print(f"Reconnecting...")
await client.start(token, reconnect = True)
except Exception as re:
print(f"Reconnect failed: {re}")
async def run_all_bots(self):
tasks = []
if self.config.get('owo'):
tasks.append(self.run_bot('owo', 'setting/owo.json'))
if tasks:
await asyncio.gather(*tasks)
else:
print("No bots enabled")
def run(self):
if self.update():
print("Restarting...")
os.execl(sys.executable, sys.executable, *sys.argv)
print("Starting bots...")
asyncio.run(self.run_all_bots())
if __name__ == "__main__":
BotManager().run()