Skip to content

Commit

Permalink
Fixed high latency, switched to SurrealDB, splitted API into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Deutscher775 committed Jul 18, 2024
1 parent e875913 commit 060031d
Show file tree
Hide file tree
Showing 15 changed files with 1,132 additions and 403 deletions.
29 changes: 16 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
.idea
.git
src/Bot/guilded_servers
src/Bot/revolt.log
src/Bot/revolt_servers
src/Bot/config.py
*.log
*_servers*
.github
LICENSE
tokens.json
src/endpoints
*endpoints*
beta
Copy of endpoints
src/beta_users.py
beta_users.py
tokens.json
TMP
__pycache__
endpoints*
tokens*
src/tokens.json
src/Bot/config.py
src/Bot/config.py
src/Bot/discord.py
tokens.json
config.py
website*
_astroidapi.log
*.gitignore
src/tokens.json
src/tokens.json
/web
website.zip
src/tokens.json
src/tokens.json
/website
website/privacy.html
src/csdsc.py
src/Copy of Copy of api.py
src/Copy of api.py
src/Bot/config.py
Binary file added assets/Astroid Manager Logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Astroid PFP not found.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/StroidSans-Regular.ttf
Binary file not shown.
12 changes: 12 additions & 0 deletions assets/x-symbol-svgrepo-com.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 20 additions & 3 deletions src/Bot/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import aiohttp
import traceback
import sentry_sdk
import asyncio

sentry_sdk.init(
dsn=config.SENTRY_DSN,
Expand Down Expand Up @@ -36,6 +37,14 @@
async def on_ready():
await client.sync_all_application_commands()
print(f"Logged in as {client.user} ({client.user.id})")
#while True:
# async with aiohttp.ClientSession() as session:
# async with session.post(f"https://astroid.deutscher775.de/monitor/iamup/discord") as r:
# if r.status == 200:
# print("Sent up status.")
# else:
# print("Could not send up status.")
# await asyncio.sleep(10)

# Slash command to send an embed
@client.slash_command(name="send-embed")
Expand All @@ -52,6 +61,10 @@ async def send_embed(interaction: nextcord.Interaction):
# Event handler for message deletion
@client.event
async def on_message_delete(message: nextcord.Message):
async with session.get(f"https://astroid.deutscher775.de/{message.guild.id}?token={config.MASTER_TOKEN}") as isbeta:
isbeta = await isbeta.json()
if isbeta.get("config").get("isbeta"):
return
# Get channel information from the API
async with session.get(f"https://astroid.deutscher775.de/{message.guild.id}?token={config.MASTER_TOKEN}") as channel_request:
channel_json = await channel_request.json()
Expand All @@ -67,6 +80,10 @@ async def on_message_delete(message: nextcord.Message):
# Event handler for message edit
@client.event
async def on_message_edit(before: nextcord.Message, after: nextcord.Message):
async with session.get(f"https://astroid.deutscher775.de/{message.guild.id}?token={config.MASTER_TOKEN}") as isbeta:
isbeta = await isbeta.json()
if isbeta.get("config").get("isbeta"):
return
# Get channel information from the API
async with session.get(f"https://astroid.deutscher775.de/{before.guild.id}?token={config.MASTER_TOKEN}") as channel_request:
channel_json = await channel_request.json()
Expand Down Expand Up @@ -136,20 +153,20 @@ async def on_message(message: nextcord.Message):
for field in message.embeds[0].fields:
embed["fields"].append({"name": field.name, "value": field.value, "inline": str(field.inline).lower()})
# Update the message content with embed in the API
async with session.post(f"https://astroid.deutscher775.de/update/{message.channel.guild.id}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url}&message_author_id={message.author.id}&trigger=true&sender=discord&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_embed={embed}") as update_request:
async with session.post(f"https://astroid.deutscher775.de/update/{message.channel.guild.id}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url if message.author.avatar.url else 'https://astroid.deutscher775.de/assets/Astroid PFP not found.png'}&message_author_id={message.author.id}&trigger=true&sender=discord&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_embed={embed}") as update_request:
pass
if message.attachments:
print(3)
if len(message.attachments) == 1:
# Update the message content with attachment in the API
async with session.post(f"https://astroid.deutscher775.de/update/{message.channel.guild.id}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url}&message_author_id={message.author.id}&trigger=true&sender=discord&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_attachments={message.attachments[0].url}") as update_request:
async with session.post(f"https://astroid.deutscher775.de/update/{message.channel.guild.id}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url if message.author.avatar.url else 'https://astroid.deutscher775.de/assets/Astroid PFP not found.png'}&message_author_id={message.author.id}&trigger=true&sender=discord&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_attachments={message.attachments[0].url}") as update_request:
pass
else:
attachments = ""
for attachment in message.attachments:
attachments += attachment.url
# Update the message content with multiple attachments in the API
async with session.post(f"https://astroid.deutscher775.de/update/{message.channel.guild.id}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url}&message_author_id={message.author.id}&trigger=true&sender=discord&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_attachments={attachments[:-1]}") as update_request:
async with session.post(f"https://astroid.deutscher775.de/update/{message.channel.guild.id}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url if message.author.avatar.url else 'https://astroid.deutscher775.de/assets/Astroid PFP not found.png'}&message_author_id={message.author.id}&trigger=true&sender=discord&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_attachments={attachments[:-1]}") as update_request:
pass
except KeyError:
pass
Expand Down
70 changes: 57 additions & 13 deletions src/Bot/guilded_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import datetime
import traceback
import sentry_sdk
import asyncio

sentry_sdk.init(
dsn=config.SENTRY_DSN,
Expand Down Expand Up @@ -37,6 +38,18 @@ async def post_json(url, data):
async with session.post(url, json=data) as response:
return await response.json()

@client.event
async def on_ready():
print(f"Logged in as {client.user.name} - {client.user.id}")
#while True:
# async with aiohttp.ClientSession() as session:
# async with session.post(f"https://astroid.deutscher775.de/monitor/iamup/guilded") as r:
# if r.status == 200:
# print("Sent up status.")
# else:
# print("Could not send up status.")
# await session.close()
# await asyncio.sleep(10)

@client.event
async def on_message_delete(message: guilded.Message):
Expand All @@ -45,6 +58,18 @@ async def on_message_delete(message: guilded.Message):
"r",
)
endpoint = json.load(endpoint_file)["discord"]
try:
async with aiohttp.ClientSession() as session:
async with session.get(f"https://astroid.deutscher775.de/{endpoint}?token={config.MASTER_TOKEN}") as response:
data = await response.json()
if data["config"]["isbeta"]:
return
except IndexError:
pass
except KeyError:
pass
except:
traceback.print_exc()

channels_url = (
f"https://astroid.deutscher775.de/{endpoint}?token={config.MASTER_TOKEN}"
Expand Down Expand Up @@ -78,6 +103,18 @@ async def on_message_edit(before: guilded.Message, after: guilded.Message):
"r",
)
endpoint = json.load(endpoint_file)["discord"]
try:
async with aiohttp.ClientSession() as session:
async with session.get(f"https://astroid.deutscher775.de/{endpoint}?token={config.MASTER_TOKEN}") as response:
data = await response.json()
if data["config"]["isbeta"]:
return
except IndexError:
pass
except KeyError:
pass
except:
traceback.print_exc()

channels_url = (
f"https://astroid.deutscher775.de/{endpoint}?token={config.MASTER_TOKEN}"
Expand Down Expand Up @@ -251,19 +288,25 @@ async def send_embed(ctx):

@client.event
async def on_message(message: guilded.Message):
async with aiohttp.ClientSession() as session:
endpoint = json.load(open(f"{pathlib.Path(__file__).parent.parent.resolve()}/Bot/guilded_servers/{message.guild.id}.json", "r"))["discord"]
async with session.get(f"https://astroid.deutscher775.de/{endpoint}?token={config.MASTER_TOKEN}") as isbeta:
isbeta = await isbeta.json()
if isbeta.get("config").get("isbeta"):
return
await client.process_commands(message)
try:
endpoint_file = open(
f"{pathlib.Path(__file__).parent.parent.resolve()}/Bot/guilded_servers/{message.server.id}.json",
"r",
)
endpoint = json.load(endpoint_file)["discord"]
try:
async with aiohttp.ClientSession() as session:
async with session.get(f"https://astroid.deutscher775.de/{endpoint}?token={config.MASTER_TOKEN}") as response:
data = await response.json()
if data["config"]["isbeta"]:
return
except IndexError:
pass
except KeyError:
pass
except:
traceback.print_exc()
await client.process_commands(message)
async with aiohttp.ClientSession() as session:
async with session.get(
f"https://astroid.deutscher775.de/{endpoint}?token={config.MASTER_TOKEN}"
Expand All @@ -279,16 +322,14 @@ async def on_message(message: guilded.Message):
colour=0xF5C400,
)
await message.delete()
channel = await client.fetch_channel(
str(data["config"]["logs"]["guilded"])
).close()
channel = await client.fetch_channel(str(data["config"]["logs"]["guilded"]))
await channel.send(embed=embed)
return
allowed_ids = data["config"]["allowed-ids"]
if not message.author.bot or message.author.id in allowed_ids:
if not message.attachments and not message.embeds:
async with session.post(
f"https://astroid.deutscher775.de/update/{endpoint}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url}&message_author_id={message.author.id}&trigger=true&sender=guilded&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}"
f"https://astroid.deutscher775.de/update/{endpoint}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url if message.author.avatar.url else 'https://astroid.deutscher775.de/assets/Astroid PFP not found.png'}&message_author_id={message.author.id}&trigger=true&sender=guilded&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}"
) as response:
pass
elif message.embeds:
Expand All @@ -303,7 +344,7 @@ async def on_message(message: guilded.Message):
elif message.attachments:
if len(message.attachments) == 1:
async with session.post(
f"https://astroid.deutscher775.de/update/{endpoint}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url}&message_author_id={message.author.id}&trigger=true&sender=guilded&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_attachments={message.attachments[0].url.replace('![]', '').replace('(', '').replace(')', '')}"
f"https://astroid.deutscher775.de/update/{endpoint}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url if message.author.avatar.url else 'https://astroid.deutscher775.de/assets/Astroid PFP not found.png'}&message_author_id={message.author.id}&trigger=true&sender=guilded&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_attachments={message.attachments[0].url.replace('![]', '').replace('(', '').replace(')', '')}"
) as response:
pass
else:
Expand All @@ -315,14 +356,17 @@ async def on_message(message: guilded.Message):
.replace(")", "")
)
async with session.post(
f"https://astroid.deutscher775.de/update/{endpoint}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url}&message_author_id={message.author.id}&trigger=true&sender=guilded&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_attachments={attachments[:-1]}"
f"https://astroid.deutscher775.de/update/{endpoint}?message_content={message.content}&message_author_name={message.author.name}&message_author_avatar={message.author.avatar.url if message.author.avatar.url else 'https://astroid.deutscher775.de/assets/Astroid PFP not found.png'}&message_author_id={message.author.id}&trigger=true&sender=guilded&token={config.MASTER_TOKEN}&sender_channel={message.channel.id}&message_attachments={attachments[:-1]}"
) as response:
pass
except FileNotFoundError:
await client.process_commands(message)
pass
except KeyError:
await client.process_commands(message)
pass
except:
await client.process_commands(message)
traceback.print_exc()


Expand Down
2 changes: 1 addition & 1 deletion src/Bot/revolt_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ async def set_logs(self, ctx, channel: str = None):
await ctx.reply(":x: - Your endpoint could not be found.")


async def set_logs(self, ctx):
async def (self, ctx):
endpoint = get_endpoint(ctx.server)
if endpoint:
requests.post(
Expand Down
Loading

0 comments on commit 060031d

Please sign in to comment.