forked from zRitsu/MuseHeart-MusicBot
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathweb_app.py
438 lines (312 loc) · 13.9 KB
/
web_app.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# -*- coding: utf-8 -*-
from __future__ import annotations
import asyncio
import json
import logging
from os import environ
from traceback import print_exc
from typing import TYPE_CHECKING, Optional
import aiohttp
import disnake
import tornado.ioloop
import tornado.web
import tornado.websocket
from packaging import version
from config_loader import load_config
if TYPE_CHECKING:
from utils.client import BotPool
logging.getLogger('tornado.access').disabled = True
users_ws = {}
bots_ws = []
minimal_version = version.parse("2.6.1")
class IndexHandler(tornado.web.RequestHandler):
def initialize(self, pool: Optional[BotPool] = None, message: str = "", config: dict = None):
self.message = message
self.pool = pool
self.config = config
async def prepare(self):
bots = [asyncio.create_task(bot.wait_until_ready()) for bot in self.pool.bots if not bot.is_ready()]
if bots:
self.write("")
await self.flush()
await asyncio.wait(bots, timeout=7)
async def get(self):
try:
killing_state = self.pool.killing_state
except:
killing_state = False
if killing_state is True:
self.write('<h1 style=\"font-size:5vw\">Application will restart shortly...</h1>')
return
msg = ""
if self.message:
msg += self.message.replace("\n", "</br>")
style = """<style>
table, th, td {
border:1px solid black;
text-align: center;
}
a {
color: blue;
visited: blue;
}
</style>"""
failed_bots = []
pending_bots = []
ready_bots = []
kwargs = {}
for identifier, exception in self.pool.failed_bots.items():
failed_bots.append(f"<tr><td>{identifier}</td><td>{exception}</td></tr>")
for bot in sorted(self.pool.bots, key=lambda b: b.identifier):
if bot.is_ready():
avatar = bot.user.display_avatar.replace(size=256, static_format="png").url
guilds = len(bot.guilds)
ready_bots.append(
f"<tr><td><img src=\"{avatar}\" width=128 weight=128></img></td>\n"
"<td style=\"padding-top: 10px ; padding-bottom: 10px; padding-left: 10px; padding-right: 10px\">"
f"Add:<br><a href=\"{disnake.utils.oauth_url(bot.user.id, permissions=disnake.Permissions(bot.config['INVITE_PERMISSIONS']), scopes=('bot', 'applications.commands'), **kwargs)}\" "
f"rel=\"nofollow\" target=\"_blank\">{bot.user}</a>" + (f"<br>Servers: {guilds}" if guilds else "") + "</td></tr>"
)
else:
pending_bots.append(f"<tr><td>{bot.identifier}</td></tr>")
if ready_bots:
msg += f"\n<p style=\"font-size:20px\">Available Bots:</p>" \
f"{style}\n<table cellpadding=\"3\">{''.join(ready_bots)}</table>"
if pending_bots:
msg += f"\n<p style=\"font-size:20px\">Bots initializing:</p>" \
f"{style}\n<table cellpadding=\"10\">{''.join(pending_bots)}</table>\n" \
f"Note: Refresh the page to check if the bots are available."
if failed_bots:
failed_table_style = """<style>
table, th, td {
border:1px solid black;
text-align: left;
}
</style>"""
msg += f"\n<p style=\"font-size:20px\">The following tokens configured in ENV/SECRET/.env failed " \
f"during initialization:</p>" \
f"{failed_table_style}\n<table cellpadding=\"10\">{''.join(failed_bots)}</table>"
ws_url = "<Body onLoad=\" rpcUrl()\" ><p id=\"url\" style=\"color:blue\"></p><script>function rpcUrl(){document." \
"getElementById(\"url\").innerHTML = window.location.href.replace(\".replit.dev\", \".replit.dev:443\").replace(\"http\", \"ws\")" \
".replace(\"https\", \"wss\") + \"ws\"}</script></body>"
msg += f"<p><a href=\"https://github.com/zRitsu/DC-MusicBot-RPC" \
f"/releases\" target=\"_blank\">Download rich presence app here.</a></p>Link to add to RPC app: {ws_url}"
if self.config["ENABLE_RPC_AUTH"]:
msg += f"\nDon't forget to get the token to configure in the app, use the command /rich_presence to get one.\n<br><br>"
msg += f"\nDefault Prefix: {self.pool.config['DEFAULT_PREFIX']}<br><br>"
if self.pool.commit:
msg += f"\nCurrent Commit: <a href=\"{self.pool.remote_git_url}/commit/{self.pool.commit}\" target=\"_blank\">{self.pool.commit[:7]}</a>"
self.write(msg)
class WebSocketHandler(tornado.websocket.WebSocketHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user_ids: list = []
self.bot_ids: list = []
self.token = ""
self.blocked = False
self.auth_enabled = False
def on_message(self, message):
data = json.loads(message)
ws_id = data.get("user_ids")
bot_id = data.get("bot_id")
token = data.pop("token", "") or ""
app_version = version.parse(data.get("version", "0"))
self.auth_enabled = data.pop("auth_enabled", False)
if not ws_id:
if not bot_id:
print(f"disconnecting: due to lack of user id {self.request.remote_ip}\nData: {data}")
self.write_message(json.dumps({"op": "disconnect", "reason": "Disconnecting due to lack of user ids"}))
self.close(code=4200)
return
try:
if self.auth_enabled:
if users_ws[data["user"]].token != token:
if users_ws[data["user"]].blocked:
return
data.update(
{
"op": "exception",
"message": "invalid token! Just in case, generate a new token using the command in the bot: /rich_presence."
}
)
for d in ("token", "track", "info"):
data.pop(d, None)
users_ws[data["user"]].blocked = True
else:
users_ws[data["user"]].blocked = False
users_ws[data["user"]].write_message(json.dumps(data))
except KeyError:
pass
except Exception as e:
print(f"Error processing rpc data for user [{data['user']}]: {repr(e)}")
return
is_bot = data.pop("bot", False)
if is_bot:
print(f"🤖 - New connection - Bot: {ws_id} {self.request.remote_ip}")
self.bot_ids = ws_id
bots_ws.append(self)
return
if app_version < minimal_version:
self.write_message(json.dumps({"op": "disconnect", "reason": "App version not supported! Make sure you are using "
f"the latest version of the app ({minimal_version} or higher)."}))
self.close(code=4200)
return
if len(ws_id) > 3:
self.write_message(json.dumps({"op": "disconnect", "reason": "You are trying to connect more than 3 users consecutively..."}))
self.close(code=4200)
return
if len(token) not in (0, 50):
self.write_message(
json.dumps({"op": "disconnect", "reason": f"The token needs to be 50 characters long..."}))
self.close(code=4200)
return
self.user_ids = ws_id
print("\n".join(f"👤 - New connection - User: {u}" for u in self.user_ids))
for u_id in ws_id:
try:
users_ws[u_id].write_message(json.dumps({"op": "disconnect",
"reason": "New session started elsewhere..."}))
users_ws[u_id].close(code=4200)
except:
pass
users_ws[u_id] = self
self.token = token
for w in bots_ws:
try:
w.write_message(json.dumps(data))
except Exception as e:
print(f"🤖 - Error processing rpc data for bots {w.bot_ids}: {repr(e)}")
def check_origin(self, origin: str):
return True
def on_close(self):
if self.user_ids:
print("\n".join(f"👤 - Connection Closed - User: {u}" for u in self.user_ids))
for u_id in self.user_ids:
try:
del users_ws[u_id]
except KeyError:
continue
return
if not self.bot_ids:
print(f"Connection Closed - IP: {self.request.remote_ip}")
else:
print(f"🌐 - Connection Closed - Bot ID's: {self.bot_ids}")
data = {"op": "close", "bot_id": self.bot_ids}
for w in users_ws.values():
if w.blocked:
continue
try:
w.write_message(data)
except Exception as e:
print(
f"👤 - Error processing RPC data for users: [{', '.join(str(i) for i in w.user_ids)}]: {repr(e)}")
bots_ws.remove(self)
class WSClient:
def __init__(self, url: str, pool: BotPool):
self.url: str = url
self.pool = pool
self.all_bots = None
self.connection = None
self.backoff: int = 7
self.data: dict = {}
self.session: Optional[aiohttp.ClientSession] = None
self.connect_task = []
async def connect(self):
if not self.session:
self.session = aiohttp.ClientSession()
self.connection = await self.session.ws_connect(self.url, heartbeat=30)
self.backoff = 7
print("🌐 - RPC client connected, synchronizing RPC of the bots...")
if not self.all_bots:
self.all_bots = self.pool.get_all_bots()
self.connect_task = [asyncio.create_task(self.connect_bot_rpc())]
@property
def is_connected(self):
return self.connection and not self.connection.closed
async def connect_bot_rpc(self):
bot_ids = set()
for bot in self.all_bots:
await bot.wait_until_ready()
bot_ids.add(bot.user.id)
if not bot_ids:
print("🌐 - Connection to RPC server ignored: Empty bot list...")
return
await self.send({"user_ids": list(bot_ids), "bot": True, "auth_enabled": self.pool.config["ENABLE_RPC_AUTH"]})
await asyncio.sleep(1)
for bot in self.all_bots:
for player in bot.music.players.values():
if not player.guild.me.voice:
continue
if player.guild.me.voice.channel.voice_states:
bot.loop.create_task(player.process_rpc(player.last_channel))
print(f"🌐 - [RPC client] - Rpc data synchronized successfully.")
async def send(self, data: dict):
if not self.is_connected:
return
try:
await self.connection.send_json(data)
except:
print_exc()
def clear_tasks(self):
for t in self.connect_task:
try:
t.cancel()
except:
continue
self.connect_task.clear()
async def ws_loop(self):
while True:
try:
if not self.is_connected:
self.clear_tasks()
await self.connect()
except Exception as e:
if isinstance(e, aiohttp.WSServerHandshakeError):
print(f"🌐 - Failed to connect to RPC server, trying again in {(b:=int(self.backoff))} second{'s'[:b^1]}.")
else:
print(f"🌐 - Lost connection to RPC server - Reconnecting in {(b:=int(self.backoff))} second{'s'[:b^1]}.")
await asyncio.sleep(self.backoff)
self.backoff *= 2.5
continue
message = await self.connection.receive()
if message.type in (aiohttp.WSMsgType.CLOSED, aiohttp.WSMsgType.ERROR):
print(f"🌐 - RPC Websocket Closed: {message.extra}\nReconnecting in {self.backoff}s")
await asyncio.sleep(self.backoff)
continue
elif message.type in (aiohttp.WSMsgType.CLOSING, aiohttp.WSMsgType.CLOSE):
print(f"🌐 - RPC Websocket Closed: {message.extra}")
return
data = json.loads(message.data)
users: list = data.get("user_ids")
if not users:
continue
op = data.get("op")
if op == "rpc_update":
for bot in self.all_bots:
for player in bot.music.players.values():
if not player.guild.me.voice:
continue
vc = player.guild.me.voice.channel
vc_user_ids = [i for i in vc.voice_states if i in users]
if vc_user_ids:
bot.loop.create_task(player.process_rpc(vc))
for i in vc_user_ids:
users.remove(i)
def run_app(pool: BotPool, message: str = "", config: dict = None):
if not config:
try:
config = pool.config
except IndexError:
pass
app = tornado.web.Application([
(r'/', IndexHandler, {'pool': pool, 'message': message, 'config': config}),
(r'/ws', WebSocketHandler),
])
app.listen(port=config.get("PORT") or environ.get("PORT", 80))
def start(pool: BotPool, message="", config: dict = None):
if not config:
config = load_config()
run_app(pool, message, config)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
start(BotPool())