-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.py
867 lines (777 loc) · 47 KB
/
api.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
import json
import os
import pathlib
import traceback
import uvicorn
import fastapi
import Bot.config
import fastapi.security
import secrets
from typing import Annotated
import astroidapi.attachment_processor
import astroidapi.endpoint_update_handler
import astroidapi.errors
import astroidapi.get_channel_information
import astroidapi.health_check
import astroidapi.read_handler
import astroidapi.surrealdb_handler
import astroidapi.statistics
import astroidapi.suspension_handler
import beta_users
from fastapi.middleware.cors import CORSMiddleware
import requests
import sentry_sdk
from PIL import Image
from fastapi import HTTPException
import slowapi
from slowapi.errors import RateLimitExceeded
from slowapi import Limiter
from slowapi.util import get_remote_address
import logging
import astroidapi
# Configure logging to log to a file
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s")
rootLogger = logging.getLogger()
fileHandler = logging.FileHandler("_astroidapi.log", mode="a")
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
sentry_sdk.init(
dsn=Bot.config.SENTRY_DSN,
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
traces_sample_rate=1.0,
# Set profiles_sample_rate to 1.0 to profile 100%
# of sampled transactions.
# We recommend adjusting this value in production.
profiles_sample_rate=1.0,
)
limiter = Limiter(key_func=get_remote_address)
api = fastapi.FastAPI(
title="Astroid API",
description="Astroid API for getting and modifying endpoints.",
version="2.1.4",
docs_url=None
)
api.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@api.get("/viewlogs")
def view_logs(token: Annotated[str, fastapi.Query(max_length=85, min_length=10)]) :
if token == Bot.config.LOG_TOKEN:
with open("astroidapi.log", "r") as file:
return fastapi.responses.PlainTextResponse(status_code=200, content=file.read())
else:
return fastapi.responses.Response(status_code=404)
@api.get("/ga-test")
def ga_test():
return fastapi.responses.JSONResponse(status_code=200, content={"commited": "210824-15"})
@api.get("/assets/{asset}", description="Get an asset.")
def get_asset(asset: str, width: int = None, height: int = None):
if not width and not height:
try:
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/{asset}")
except:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This asset does not exist."})
else:
if asset == "logo_no_bg":
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/Astroid Logo no bg.png")
new_image = image.resize((width, height))
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo no bg.png")
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo no bg{width}x{height}.png")
elif asset == "logo":
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/Astroid Logo.png")
new_image = image.resize((width, height))
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo.png")
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid Logo{width}x{height}.png")
elif asset == "banner":
image = Image.open(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner.png")
new_image = image.resize((width, height))
new_image.save(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner.png")
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.parent.resolve()}/assets/resized/Astroid-banner{width}x{height}.png")
else:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This asset does not exist."})
@api.get("/docs", description="Get the documentation.")
def docs():
return fastapi.responses.RedirectResponse(status_code=301, url="https://docs.astroid.cc")
@api.get("/", description="Home.")
def root():
home_data = {
"heading": "Astroid API",
"description": "Astroid API for getting and modifying endpoints.",
"website": "https://astroid.cc",
"privacy": "https://astroid.cc/privacy",
"terms": "https://astroid.cc/terms",
"imprint": "https://deutscher775.de/imprint.html",
"docs": "https://astroid.cc/docs",
"discord": "https://discord.gg/DbrFADj6Xw",
}
return fastapi.responses.JSONResponse(status_code=200, content=home_data)
@api.get("/statistics", description="Get the statistics.")
async def get_statistics():
return await astroidapi.statistics.get_statistics()
@api.get("/invite/{platform}", description="Get the invite link for the astroid bot.")
def invite(platform: str, token: Annotated[str, fastapi.Query(max_length=85)] = None):
if platform == "discord":
return fastapi.responses.RedirectResponse(status_code=301, url="https://discord.com/oauth2/authorize?client_id=1046057269202268303&permissions=2687625280&scope=bot%20applications.commands")
elif platform == "guilded":
return fastapi.responses.RedirectResponse(status_code=301, url="https://www.guilded.gg/b/00c19caa-e176-45f6-b1f6-7bee5ba73db9")
elif platform == "nerimity":
return fastapi.responses.RedirectResponse(status_code=301, url="https://nerimity.com/bot/1570083353837875200?perms=202")
elif platform == "nerimity-beta":
return fastapi.responses.RedirectResponse(status_code=301, url="https://nerimity.com/bot/1528027197628129280?perms=202")
elif platform == "discord-beta" and token in beta_users.TOKENS:
return fastapi.responses.RedirectResponse(status_code=301, url="https://discord.com/oauth2/authorize?client_id=1230579871059804280&permissions=138046467089&scope=applications.commands+bot")
elif platform == "guilded-beta" and token in beta_users.TOKENS:
return fastapi.responses.RedirectResponse(status_code=301, url="https://www.guilded.gg/b/3f887186-82d5-4fe1-abbd-045748b125b3")
elif platform == "revolt-beta" and token in beta_users.TOKENS:
return fastapi.responses.RedirectResponse(status_code=301, url="https://app.revolt.chat/bot/01HZH72H3PZKTEG1ADQ5P2XBF5")
else:
return fastapi.responses.JSONResponse(status_code=400, content={"message": "Invalid platform."})
@api.get("/discord", description="Discord Server")
def discord():
return fastapi.responses.RedirectResponse(
status_code=301,
url="https://discord.gg/DbrFADj6Xw"
)
@api.get("/cdn/{assetId}", description="Get an asset from the CDN.")
async def get_cdn_asset(assetId: str):
asset = await astroidapi.surrealdb_handler.AttachmentProcessor.get_attachment(assetId)
try:
if asset:
return fastapi.responses.FileResponse(f"{pathlib.Path(__file__).parent.resolve()}/astroidapi/TMP_attachments/{assetId}.{asset['type']}")
else:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This asset does not exist."})
except FileNotFoundError:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This asset does not exist."})
except Exception as e:
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"})
@api.get("/docs/props", description="Get the properties of the API.")
def props():
return json.load(open(f"{pathlib.Path(__file__).parent.resolve()}/props.json", "r"))
@api.get("/docs/parameters", description="Get the parameters of the API.")
def parameters():
return json.load(open(f"{pathlib.Path(__file__).parent.resolve()}/parameters.json", "r"))
@api.get("/docs/responses", description="Get the responses of the API.")
def responses():
return json.load(open(f"{pathlib.Path(__file__).parent.resolve()}/responses.json", "r"))
@api.get("/getserverstructure", description="Get a server structure.")
def get_server_structure(id: int, token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None):
global data_token
try:
data_token = json.load(open(f"{pathlib.Path(__file__).parent.resolve()}/tokens.json", "r"))[f"{id}"]
except:
logging.exception(traceback.print_exc())
data_token = None
pass
if token is not None:
if token == data_token or token == Bot.config.MASTER_TOKEN:
headers = {
'Authorization': f'Bot {Bot.config.DISCORD_TOKEN}',
'Content-Type': 'application/json'
}
response = requests.get(f'https://discord.com/api/v9/guilds/{id}/channels', headers=headers)
if response.status_code == 200:
channels = {}
response_channels = response.json()
for channel in response_channels:
if channel["type"] == 0 or channel["type"] == 5 or channel["type"] == 2:
channel_category = requests.get(f'https://discord.com/api/v9/channels/{channel["parent_id"]}', headers=headers)
category = None
try:
category = channel_category.json()["name"]
except:
category = None
pass
if not channels.get(category):
channels[category] = []
if channel["type"] == 0 or channel["type"] == 5:
channel_data = {
"name": channel["name"],
"id": channel["id"],
"type": "text",
}
channels[category].append(channel_data)
if channel["type"] == 2:
channel_data = {
"name": channel["name"],
"id": channel["id"],
"type": "voice",
}
channels[category].append(channel_data)
return channels
else:
return response.json()
@api.get("/allendpoints", description="Get all endpoints. [Master token or admin token required.]")
async def get_all_endpoints(token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None):
if token == Bot.config.MASTER_TOKEN:
return await astroidapi.surrealdb_handler.get_all_endpoints()
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})
@api.get("/{endpoint}", description="Get an endpoint.")
async def get_endpoint(endpoint: int,
token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None, download: bool = False):
suspend_status = await astroidapi.suspension_handler.Endpoint.is_suspended(endpoint)
if suspend_status:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."})
global data_token
try:
data_token = await astroidapi.surrealdb_handler.TokenHandler.get_token(endpoint)
except:
data_token = None
pass
if token is not None:
if token == data_token or token == Bot.config.MASTER_TOKEN:
try:
return fastapi.responses.JSONResponse(status_code=200, content=await astroidapi.surrealdb_handler.get_endpoint(endpoint, __file__))
except astroidapi.errors.SurrealDBHandler.EndpointNotFoundError as e:
return fastapi.responses.JSONResponse(status_code=404, content={"message": f"Endpoint {endpoint} not found."})
except astroidapi.errors.SurrealDBHandler.GetEndpointError as e:
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"})
except Exception as e:
logging.exception(traceback.print_exc())
else:
return fastapi.responses.JSONResponse(status_code=401,
content={"message": "The provided token is invalid."})
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "You must provide a token."})
@api.get("/bridges/{endpoint}", description="Get an endpoint.")
async def get_bridges(endpoint: int,
token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None):
suspend_status = await astroidapi.suspension_handler.Endpoint.is_suspended(endpoint)
if suspend_status:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."})
global data_token
try:
data_token = await astroidapi.surrealdb_handler.TokenHandler.get_token(endpoint)
except:
data_token = None
pass
if token is not None:
if token == data_token or token == Bot.config.MASTER_TOKEN:
try:
bridges_json = await astroidapi.surrealdb_handler.get_endpoint(endpoint, __file__)
bridges_discord = []
bridges_guilded = []
bridges_revolt = []
bridges_nerimity = []
for bridge in bridges_json["config"]["channels"]["discord"]:
bridges_discord.append(bridge)
for bridge in bridges_json["config"]["channels"]["revolt"]:
bridges_revolt.append(bridge)
for bridge in bridges_json["config"]["channels"]["guilded"]:
bridges_guilded.append(bridge)
for bridge in bridges_json["config"]["channels"]["nerimity"]:
bridges_nerimity.append(bridge)
return fastapi.responses.JSONResponse(
{"discord": bridges_discord, "guilded": bridges_guilded, "revolt": bridges_revolt, "nerimity": bridges_nerimity}, status_code=200)
except FileNotFoundError:
return fastapi.responses.JSONResponse(status_code=404,
content={"message": "This endpoint does not exist."})
else:
return fastapi.responses.JSONResponse(status_code=401,
content={"message": "The provided token is invalid."})
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "You must provide a token."})
@api.post("/token/{endpoint}", description="Generate a new token. (Only works with astroid-Bot)")
async def new_token(endpoint: int,
master_token: Annotated[str, fastapi.Query(max_length=85, min_length=85)]):
suspend_status = await astroidapi.suspension_handler.Endpoint.is_suspended(endpoint)
if suspend_status:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."})
if master_token == Bot.config.MASTER_TOKEN:
token = secrets.token_urlsafe(53)
exists = await astroidapi.surrealdb_handler.TokenHandler.get_token(endpoint)
if exists:
await astroidapi.surrealdb_handler.TokenHandler.update_token(endpoint, token)
else:
await astroidapi.surrealdb_handler.TokenHandler.create_token(endpoint, token)
else:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "The provided token is invalid."})
@api.patch("/daily_endpoint_check", description="Check all endpoints for their health. (Master token required)")
async def daily_endpoint_check(master_token: Annotated[str, fastapi.Query(max_length=85, min_length=85)]):
if master_token == Bot.config.MASTER_TOKEN:
try:
summary = await astroidapi.health_check.HealthCheck.EndpointCheck.daily_check()
random_token = secrets.token_urlsafe(53)
txt_file = open(f"{pathlib.Path(__file__).parent.resolve()}/healtcheck_summaries/{random_token}.txt", "w", encoding="utf-8")
for line in summary:
txt_file.write(line + "\n")
txt_file.close()
requests.post("https://discord.com/api/webhooks/1279497897016299553/3GrZI75dDYwIkwYBac4o2ApJgzlVVCPIZnon_iE5RtaRIyiYUwcdaXxA327oNZyWZXs4", json={"content": f"[Astroid API - Daily Endpoint Check] {len(summary)} endpoints checked. Summary: https://api.astroid.cc/healtcheck_summaries/{random_token}"})
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Success."})
except Exception as e:
logging.exception(traceback.print_exc())
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"})
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})
@api.get("/healtcheck_summaries/{token}")
def get_summary(token: str):
try:
with open(f"{pathlib.Path(__file__).parent.resolve()}/healtcheck_summaries/{token}.txt", "r") as file:
return fastapi.responses.PlainTextResponse(status_code=200, content=file.read())
except FileNotFoundError:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This summary does not exist."})
@api.delete("/tempattachments", description="Clear the temporary attachments.")
async def clear_temporary_attachments(master_token: Annotated[str, fastapi.Query(max_length=85, min_length=85)]):
if master_token == Bot.config.MASTER_TOKEN:
try:
total_files = len(os.listdir(f"{pathlib.Path(__file__).parent.resolve()}/astroidapi/TMP_attachments")) - 1
await astroidapi.attachment_processor.force_clear_temporary_attachments()
requests.post("https://discord.com/api/webhooks/1279497897016299553/3GrZI75dDYwIkwYBac4o2ApJgzlVVCPIZnon_iE5RtaRIyiYUwcdaXxA327oNZyWZXs4", json={"content": f"[Astroid API - TMP Attachments] Deleted {total_files} temporary attachments."})
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Success."})
except Exception as e:
logging.exception(traceback.print_exc())
requests.post("https://discord.com/api/webhooks/1279497897016299553/3GrZI75dDYwIkwYBac4o2ApJgzlVVCPIZnon_iE5RtaRIyiYUwcdaXxA327oNZyWZXs4", json={"content": f"[Astroid API - TMP Attachments] An error occurred while deleting temporary attachments:\n\n `{e}`"})
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"})
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})
@api.post("/update/{endpoint}", description="Modify an endpoint.", response_description="Endpoint with updated data.")
async def post_endpoint(
endpoint: int,
index: int = None,
webhook_discord: Annotated[str, fastapi.Query(max_length=350, min_length=50)] = None,
webhook_guilded: Annotated[str, fastapi.Query(max_length=350, min_length=50)] = None,
webhook_revolt: Annotated[str, fastapi.Query(max_length=350, min_length=50)] = None,
webhook_nerimity: Annotated[str, fastapi.Query(max_length=350, min_length=50)] = None,
log_discord: str = None,
log_guilded: Annotated[str, fastapi.Query(max_length=50, min_length=5)] = None,
log_revolt: Annotated[str, fastapi.Query(max_length=50, min_length=5)] = None,
log_nerimity: Annotated[str, fastapi.Query(max_length=50, min_length=5)] = None,
channel_discord: str = None,
channel_guilded: Annotated[str, fastapi.Query(max_length=150, min_length=5)] = None,
channel_revolt: Annotated[str, fastapi.Query(max_length=50, min_length=5)] = None,
channel_nerimity: Annotated[str, fastapi.Query(max_length=50, min_length=5)] = None,
blacklist: Annotated[str, fastapi.Query(max_length=250, min_length=1)] = None,
sender_channel: Annotated[str, fastapi.Query(max_length=80, min_length=10)] = None,
trigger: bool = None,
sender: Annotated[str, fastapi.Query(max_length=10, min_length=5)] = None,
message_author_name: Annotated[str, fastapi.Query(max_length=50, min_length=1)] = None,
message_author_avatar: Annotated[str, fastapi.Query(max_length=250, min_length=50)] = None,
allowed_ids: Annotated[str, fastapi.Query(max_length=50, min_length=5)] = None,
message_reply: bool = None,
message_reply_message: Annotated[str, fastapi.Query(max_length=1500)] = None,
message_reply_author: Annotated[str, fastapi.Query(max_length=50, min_length=5)] = None,
message_author_id: Annotated[str, fastapi.Query(max_length=50, min_length=5)] = None,
message_content: Annotated[str, fastapi.Query(max_length=1500)] = None,
message_attachments: Annotated[str, fastapi.Query(max_length=1550, min_length=20)] = None,
message_embed: Annotated[str, fastapi.Query(max_length=1500)] = None,
selfuse: bool = None,
token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None,
beta: bool = False,
only_check = False,
):
suspend_status = await astroidapi.suspension_handler.Endpoint.is_suspended(endpoint)
if suspend_status:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."})
if not token:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "You must provide a token."})
try:
data_token = await astroidapi.surrealdb_handler.TokenHandler.get_token(endpoint)
if token != data_token and token != Bot.config.MASTER_TOKEN:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})
except KeyError:
if token != Bot.config.MASTER_TOKEN:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})
else:
pass
await astroidapi.endpoint_update_handler.UpdateHandler.update_endpoint(
endpoint=endpoint,
index=index,
webhook_discord=webhook_discord,
webhook_guilded=webhook_guilded,
webhook_revolt=webhook_revolt,
webhook_nerimity=webhook_nerimity,
log_discord=log_discord,
log_guilded=log_guilded,
log_revolt=log_revolt,
log_nerimity=log_nerimity,
channel_discord=channel_discord,
channel_guilded=channel_guilded,
channel_revolt=channel_revolt,
channel_nerimity=channel_nerimity,
blacklist=blacklist,
sender_channel=sender_channel,
trigger=trigger,
sender=sender,
message_author_name=message_author_name,
message_author_avatar=message_author_avatar,
allowed_ids=allowed_ids,
message_reply=message_reply,
message_reply_message=message_reply_message,
message_reply_author=message_reply_author,
message_author_id=message_author_id,
message_content=message_content,
message_attachments=message_attachments,
message_embed=message_embed,
selfuse=selfuse,
token=token,
beta=beta,
only_check=only_check,
)
return await astroidapi.surrealdb_handler.get_endpoint(endpoint, __file__)
@api.patch("/sync", description="Sync the local files with the database.")
async def sync_files(endpoint: int = None, token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None):
if token == Bot.config.MASTER_TOKEN:
if endpoint:
await astroidapi.surrealdb_handler.sync_local_files(f"{pathlib.Path(__file__).parent.resolve()}/endpoints/{endpoint}.json", True)
else:
await astroidapi.surrealdb_handler.sync_local_files(f"{pathlib.Path(__file__).parent.resolve()}/endpoints")
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Success."})
else:
return fastapi.responses.JSONResponse(status_code=404, content={"detail": "Not found"})
@api.post("/read/{endpoint}",
description="Mark the 'meta' as read on the platform(s). "
"[Note: Currently only used in the astroid Revolt-bot.]")
async def mark_read(endpoint: int,
token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None,
read_discord: bool = None,
read_guilded: bool = None,
read_revolt: bool = None,
read_nerimity: bool = None):
suspend_status = await astroidapi.suspension_handler.Endpoint.is_suspended(endpoint)
if suspend_status:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."})
if token == data_token or token == Bot.config.MASTER_TOKEN:
try:
if read_discord:
await astroidapi.read_handler.ReadHandler.mark_read(endpoint, "discord")
if read_guilded:
await astroidapi.read_handler.ReadHandler.mark_read(endpoint, "guilded")
if read_revolt:
await astroidapi.read_handler.ReadHandler.mark_read(endpoint, "revolt")
if read_nerimity:
await astroidapi.read_handler.ReadHandler.mark_read(endpoint, "nerimity")
except Exception as e:
logging.exception(traceback.print_exc())
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"})
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Success."})
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})
@api.get("/healthcheck/{endpoint}", description="Validate the endpoints strucuture.")
async def endpoint_healthcheck(endpoint: int, token: str):
suspend_status = await astroidapi.suspension_handler.Endpoint.is_suspended(endpoint)
if suspend_status:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."})
if token == Bot.config.MASTER_TOKEN:
try:
healty = await astroidapi.health_check.HealthCheck.EndpointCheck.check(endpoint)
if healty:
return fastapi.responses.JSONResponse(status_code=200, content={"message": "This endpoint is healthy.", "details": None})
except astroidapi.errors.HealtCheckError.EndpointCheckError.EndpointConfigError as e:
return fastapi.responses.JSONResponse(status_code=200, content={"message": f"There seems to be an error in the endpoint configuration: {e}",
"details": "configerror"})
except astroidapi.errors.HealtCheckError.EndpointCheckError.EndpointMetaDataError as e:
return fastapi.responses.JSONResponse(status_code=200, content={"message": f"There seems to be an error in the endpoint meta data: {e}",
"details": "metadataerror"})
except astroidapi.errors.HealtCheckError.EndpointCheckError as e:
return fastapi.responses.JSONResponse(status_code=200, content={"message": f"An error occurred: {e}",
"details": "unexpectederror"})
except astroidapi.errors.SurrealDBHandler.EndpointNotFoundError:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This endpoint does not exist.",
"details": "notfound"})
except astroidapi.errors.SurrealDBHandler.GetEndpointError as e:
traceback.print_exc()
return fastapi.responses.JSONResponse(status_code=404, content={"message": f"An error occurred: {e}",
"details": "getendpointerror"})
@api.post("/healthcheck/{endpoint}/repair", description="Repair the endpoint.")
async def repair_endpoint(endpoint: int, token: str):
suspend_status = await astroidapi.suspension_handler.Endpoint.is_suspended(endpoint)
if suspend_status:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."})
if token == Bot.config.MASTER_TOKEN:
try:
summary = await astroidapi.health_check.HealthCheck.EndpointCheck.repair_structure(endpoint)
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Repaired.", "summary": summary})
except Exception as e:
logging.exception(traceback.print_exc())
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"})
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})
@api.post("/create", description="Create an endpoint.",
response_description="Endpoints data.")
async def create_endpoint(endpoint: int):
try:
suspend_status = await astroidapi.suspension_handler.Endpoint.is_suspended(endpoint)
if suspend_status:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."})
except:
pass
try:
data = {
"config": {
"self-user": False,
"webhooks": {
"discord": [],
"guilded": [],
"revolt": [],
"nerimity": []
},
"channels": {
"discord": [],
"guilded": [],
"revolt": [],
"nerimity": []
},
"logs": {
"discord": None,
"guilded": None,
"revolt": None,
"nerimity": None
},
"blacklist": [],
"allowed-ids": [],
"isbeta": False
},
"meta": {
"sender-channel": None,
"trigger": False,
"sender": None,
"read": {
"discord": False,
"guilded": False,
"revolt": False,
"nerimity": False
},
"message": {
"isReply": False,
"reply": {
"message": None,
"author": None
},
"author": {
"name": None,
"avatar": None,
"id": None
},
"content": None,
"attachments": []
}
}
}
await astroidapi.surrealdb_handler.create(endpoint, data)
return fastapi.responses.JSONResponse(status_code=201, content={"message": "Created."})
except FileExistsError:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint exists already."})
@api.delete("/delete/{endpoint}", description="Delete an endpoint.")
async def delete_endpoint(endpoint: int,
token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None):
try:
data_token = await astroidapi.surrealdb_handler.TokenHandler.get_token(endpoint)
if token is not None:
if token == data_token or token == Bot.config.MASTER_TOKEN:
try:
await astroidapi.surrealdb_handler.delete(endpoint)
await astroidapi.surrealdb_handler.TokenHandler.delete_token(endpoint)
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Deleted."})
except FileNotFoundError:
return fastapi.responses.JSONResponse(status_code=404,
content={"message": "This endpoint does not exist."})
else:
return fastapi.responses.JSONResponse(status_code=401,
content={"message": "The provided token is invalid."})
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "You must provide a token."})
except KeyError:
if token == Bot.config.MASTER_TOKEN:
try:
await astroidapi.surrealdb_handler.delete(endpoint)
await astroidapi.surrealdb_handler.TokenHandler.delete_token(endpoint)
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Deleted."})
except FileNotFoundError:
return fastapi.responses.JSONResponse(status_code=404,
content={"message": "This endpoint does not exist."})
else:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This endpoint does not exist."})
@api.delete("/delete/data/{endpoint}", description="Edit or delete specific data of endpoint")
async def delete_enpoint_data(endpoint: int,
webhook_discord: Annotated[str, fastapi.Query(max_length=350, min_length=50)] = None,
webhook_guilded: Annotated[str, fastapi.Query(max_length=350, min_length=50)] = None,
webhook_revolt: Annotated[str, fastapi.Query(max_length=350, min_length=50)] = None,
webhook_nerimity: Annotated[str, fastapi.Query(max_length=350, min_length=50)] = None,
log_discord: bool = None,
log_guilded: bool = None,
log_revolt: bool = None,
log_nerimity: bool = None,
channel_discord: int = None,
channel_guilded: Annotated[str, fastapi.Query(max_length=150, min_length=5)] = None,
channel_revolt: Annotated[str, fastapi.Query(max_length=50, min_length=5)] = None,
channel_nerimity: Annotated[str, fastapi.Query(max_length=50, min_length=5)] = None,
blacklist: Annotated[str, fastapi.Query(max_length=250, min_length=1)] = None,
sender_channel: bool = None,
sender: bool = None,
message_author_name: bool = None,
message_author_avatar: bool = None,
allowed_ids: Annotated[str, fastapi.Query(max_length=50, min_length=5)] = None,
message_author_id: bool = None,
message_content: bool = None,
message_attachments: Annotated[str, fastapi.Query(max_length=1550, min_length=20)] = None,
token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None):
suspend_status = await astroidapi.suspension_handler.Endpoint.is_suspended(endpoint)
if suspend_status:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."})
data_token = await astroidapi.surrealdb_handler.TokenHandler.get_token(endpoint)
if token is not None:
if token == data_token or token == Bot.config.MASTER_TOKEN:
try:
json_data = await astroidapi.surrealdb_handler.get_endpoint(endpoint, __file__)
if webhook_discord:
json_data["config"]["webhooks"]["discord"].pop(json_data["config"]["webhooks"]["discord"].index(webhook_discord))
if webhook_guilded:
json_data["config"]["webhooks"]["guilded"].pop(json_data["config"]["webhooks"]["guilded"].index(webhook_guilded))
if webhook_revolt:
json_data["config"]["webhooks"]["revolt"].pop(json_data["config"]["webhooks"]["revolt"].index(webhook_revolt))
if webhook_nerimity:
json_data["config"]["webhooks"]["nerimity"].pop(json_data["config"]["webhooks"]["nerimity"].index(webhook_nerimity))
if log_discord:
json_data["config"]["logs"]["discord"][json_data["config"]["logs"]["discord"]] = None
if log_guilded:
json_data["config"]["logs"]["guilded"][json_data["config"]["logs"]["guilded"]] = None
if log_revolt:
json_data["config"]["logs"]["revolt"][json_data["config"]["logs"]["revolt"]] = None
if log_nerimity:
json_data["config"]["logs"]["nerimity"][json_data["config"]["logs"]["nerimity"]] = None
if blacklist:
if "," in blacklist:
blacklist = blacklist.split(",")
for word in blacklist:
if word in json_data["config"]["blacklist"]:
json_data["config"]["blacklist"].pop(json_data["config"]["blacklist"].index(word))
else:
if blacklist in json_data["config"]["blacklist"]:
json_data["config"]["blacklist"].pop(json_data["config"]["blacklist"].index(blacklist))
if allowed_ids:
if "," in allowed_ids:
allowed_ids = allowed_ids.split(",")
for id in allowed_ids:
if id in json_data["config"]["allowed-ids"]:
json_data["config"]["allowed-ids"].pop(json_data["config"]["allowed-ids"].index(id))
else:
if allowed_ids in json_data["config"]["allowed-ids"]:
json_data["config"]["allowed-ids"].pop(json_data["config"]["allowed-ids"].index(allowed_ids))
if channel_discord:
json_data["config"]["channels"]["discord"].pop(json_data["config"]["channels"]["discord"].index(str(channel_discord)))
if channel_guilded:
json_data["config"]["channels"]["guilded"].pop(json_data["config"]["channels"]["guilded"].index(channel_guilded))
if channel_revolt:
json_data["config"]["channels"]["revolt"].pop(json_data["config"]["channels"]["revolt"].index(channel_revolt))
if channel_nerimity:
json_data["config"]["channels"]["nerimity"].pop(json_data["config"]["channels"]["nerimity"].index(str(channel_nerimity)))
if sender_channel:
json_data["meta"]["sender_channel"] = None
if sender:
json_data["meta"]["sender"] = None
if message_author_name:
json_data["meta"]["message"]["author"]["name"] = None
if message_author_id:
json_data["meta"]["message"]["author"]["id"] = None
if message_author_avatar:
json_data["meta"]["message"]["author"]["avatar"] = None
if message_content:
json_data["meta"]["message"]["content"] = None
if message_attachments:
json_data["meta"]["message"]["attachments"].pop(json_data["meta"]["message"]["attachments"].index(message_attachments))
data = await astroidapi.surrealdb_handler.update(endpoint, json_data)
return fastapi.responses.JSONResponse(status_code=200, content=data)
except astroidapi.errors.SurrealDBHandler.EndpointNotFoundError as e:
return fastapi.responses.JSONResponse(status_code=404,
content={"message": "This endpoint does not exist."})
else:
return fastapi.responses.JSONResponse(status_code=401,
content={"message": "The provided token is invalid."})
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "You must provide a token."})
@api.get("/getendpoint/{platform}", description="Get an endpoint via a platform server id.")
async def get_endpoint_platform(platform: str, id: str, token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None):
if not token == Bot.config.MASTER_TOKEN:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid. (Only the master token can be used to view or create relations.)"})
try:
if platform == "guilded":
return await astroidapi.surrealdb_handler.GetEndpoint.from_guilded_id(id)
elif platform == "revolt":
return await astroidapi.surrealdb_handler.GetEndpoint.from_revolt_id(id)
elif platform == "nerimity":
return await astroidapi.surrealdb_handler.GetEndpoint.from_nerimity_id(id)
else:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This platform does not exist."})
except:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This endpoint does not exist."})
@api.post("/createendpoint/{platform}", description="Create an endpoint via a platform server id.")
async def create_endpoint_platform(platform: str, endpoint: int, id: str, token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None):
suspend_status = await astroidapi.suspension_handler.Endpoint.is_suspended(endpoint)
if suspend_status:
return fastapi.responses.JSONResponse(status_code=403, content={"message": "This endpoint is suspended."})
if not token == Bot.config.MASTER_TOKEN:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid. (Only the master token can be used to view or create relations.)"})
try:
if platform == "guilded":
return await astroidapi.surrealdb_handler.CreateEndpoint.for_guilded(endpoint, id)
elif platform == "revolt":
return await astroidapi.surrealdb_handler.CreateEndpoint.for_revolt(endpoint, id)
elif platform == "nerimity":
return await astroidapi.surrealdb_handler.CreateEndpoint.for_nerimity(endpoint, id)
else:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This platform does not exist."})
except:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This endpoint does not exist."})
@api.patch("/syncserverrelations", description="Sync the server relations.")
async def syncserverrelaions():
try:
await astroidapi.surrealdb_handler.sync_server_relations()
return fastapi.responses.JSONResponse(status_code=200, content={"message": "Success."})
except Exception as e:
return fastapi.responses.JSONResponse(status_code=500, content={"message": f"An error occurred: {e}"})
@api.get("/channel/name/{platform}", description="Get the channel name.")
async def get_channel_name(platform: str, id: str, token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None):
try:
if platform == "discord":
return await astroidapi.get_channel_information.GetChannelName.from_discord_id(int(id))
elif platform == "guilded":
return await astroidapi.get_channel_information.GetChannelName.from_guilded_id(id)
elif platform == "revolt":
return await astroidapi.get_channel_information.GetChannelName.from_revolt_id(id)
elif platform == "nerimity":
return await astroidapi.get_channel_information.GetChannelName.from_nerimity_id(int(id))
else:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This platform does not exist."})
except:
return fastapi.responses.JSONResponse(status_code=404, content={"message": "This channel does not exist."})
@api.get("/contribution/contributors", description="Get contributors that worked on Astroid.")
async def get_contributors():
return await astroidapi.surrealdb_handler.Contributions.Contributors.get_contributors()
@api.get("/contribution/contributor", description="Get a contributor that worked on Astroid.")
async def get_contributor(name: str = None, id: int = None):
if name and not id:
return await astroidapi.surrealdb_handler.Contributions.Contributors.get_contributor_by_username(name)
elif id and not name:
return await astroidapi.surrealdb_handler.Contributions.Contributors.get_contributor(id)
elif id and name:
return fastapi.responses.JSONResponse(status_code=400, content={"message": "You can only provide either a name or an id."})
else:
return fastapi.responses.JSONResponse(status_code=400, content={"message": "You must provide a name or an id."})
@api.get("/contribution/contributor/avatar", description="Get contributions that were made to Astroid.")
async def get_contributor_avatar(name: str = None, id: int = None):
if name and not id:
data = await astroidapi.surrealdb_handler.Contributions.Contributors.get_contributor_by_username(name)
return data["avatar"]
elif id and not name:
data = await astroidapi.surrealdb_handler.Contributions.Contributors.get_contributor(id)
return data["avatar"]
elif id and name:
return fastapi.responses.JSONResponse(status_code=400, content={"message": "You can only provide either a name or an id."})
else:
return fastapi.responses.JSONResponse(status_code=400, content={"message": "You must provide a name or an id."})
@api.post("/contribution/addcontributor/{id}", description="Add a contributor that worked on Astroid.")
async def add_contributor(id: int, username: str = None, avatar: str = None, token: Annotated[str, fastapi.Query(max_length=85, min_length=71)] = None):
if token == Bot.config.MASTER_TOKEN:
return await astroidapi.surrealdb_handler.Contributions.Contributors.create_contributor(id, username, avatar)
else:
return fastapi.responses.JSONResponse(status_code=401, content={"message": "The provided token is invalid."})
logging.info("[CORE] API started.")
uvicorn.run(api, host="localhost", port=9921, reload=False)