This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_bot.py
187 lines (163 loc) · 5.15 KB
/
run_bot.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
import asyncio
import logging
import os
import sys
import time
from typing import List
import requests
from dotenv import load_dotenv
import config
from app.classes.cluster import Cluster
from app.utils import webhooklog
load_dotenv()
WEBHOOK_URL = os.getenv("UPTIME_HOOK")
TOKEN = os.getenv("TOKEN")
SHARDS = config.SHARDS
log = logging.getLogger("Cluster#Launcher")
log.setLevel(logging.DEBUG)
hdlr = logging.StreamHandler()
hdlr.setFormatter(
logging.Formatter("[%(asctime)s %(name)s/%(levelname)s] %(message)s")
)
log.handlers = [hdlr]
CLUSTER_NAMES = (
"Alpha (0)",
"Beta (1)",
"Gamma (2)",
"Delta (3)",
"Epsilon (4)",
"Zeta (5)",
"Eta (6)",
"Theta (7)",
"Iota (8)",
"Kappa (9)",
"Lambda (10)",
"Mu (11)",
"Nu (12)",
"Xi (13)",
"Omicron (14)",
"Pi (15)",
"Rho (16)",
"Sigma (17)",
"Tau (18)",
"Upsilon (19)",
"Phi (20)",
"Chi (21)",
"Psi (22)",
"Omega (23)",
)
NAMES = iter(CLUSTER_NAMES)
def get_shard_count():
if SHARDS != 0:
log.info(f"Launching with {SHARDS} shards")
return SHARDS
data = requests.get(
"https://discordapp.com/api/v9/gateway/bot",
headers={
"Authorization": "Bot " + TOKEN,
"User-Agent": (
"DiscordBot (https://github.com/Rapptz/discord.py "
"1.3.0a) Python/3.7 aiohttp/3.6.1"
),
},
)
data.raise_for_status()
content = data.json()
log.info(
f"Successfully got shard count of {content['shards']}"
f" ({data.status_code, data.reason})"
)
return content["shards"]
class Launcher:
def __init__(self, loop: asyncio.AbstractEventLoop):
log.info("Hello, world!")
self.cluster_queue: List["Cluster"] = []
self.clusters = []
self.fut = None
self.loop = loop
self.alive = True
self.keep_alive = None
self.init = time.perf_counter()
def start(self):
self.loop.run_until_complete(self.startup())
try:
self.loop.run_until_complete(self.rebooter())
except KeyboardInterrupt:
pass
finally:
self.loop.run_until_complete(self.shutdown())
self.cleanup()
def cleanup(self):
self.loop.stop()
if sys.platform == "win32":
print("press ^C again")
self.loop.close()
def task_complete(self, task: asyncio.Task):
if task.cancelled():
return
if task.exception():
task.print_stack()
self.cleanup()
async def startup(self):
shards = list(range(get_shard_count()))
size = [shards[x : x + 4] for x in range(0, len(shards), 4)]
log.info(f"Preparing {len(size)} clusters")
for shard_ids in size:
self.cluster_queue.append(
Cluster(self, next(NAMES), shard_ids, len(shards))
)
await self.start_cluster()
self.keep_alive = self.loop.create_task(self.rebooter())
self.keep_alive.add_done_callback(self.task_complete)
log.info(f"Startup completed in {time.perf_counter()-self.init}s")
async def shutdown(self):
log.info("Shutting down clusters")
self.alive = False
if self.keep_alive:
self.keep_alive.cancel()
for cluster in self.clusters:
cluster.stop()
async def rebooter(self):
while self.alive:
# log.info("Cycle!")
if not self.clusters:
log.warning("All clusters appear to be dead")
return
to_remove = []
for cluster in self.clusters:
if not cluster.process.is_alive():
webhooklog(
f":red_circle: Cluster **{cluster.name}** "
"is offline.",
WEBHOOK_URL,
)
if cluster.process.exitcode != 0:
# ignore safe exits
log.info(
f"Cluster#{cluster.name} exited with code "
f"{cluster.process.exitcode}"
)
log.info(f"Restarting cluster#{cluster.name}")
await cluster.start()
else:
log.info(f"Cluster#{cluster.name} found dead")
to_remove.append(cluster)
cluster.stop() # ensure stopped
for rem in to_remove:
self.clusters.remove(rem)
await asyncio.sleep(5)
async def start_cluster(self):
if self.cluster_queue:
cluster = self.cluster_queue.pop(0)
log.info(f"Starting Cluster#{cluster.name}")
await cluster.start()
log.info("Done!")
self.clusters.append(cluster)
await self.start_cluster()
else:
log.info("All clusters launched")
if __name__ == "__main__":
loop = asyncio.get_event_loop()
webhooklog(":white_circle: Bot logging in...", WEBHOOK_URL)
Launcher(loop).start()
webhooklog(":brown_circle: Bot logged out.", WEBHOOK_URL)