Skip to content

Commit

Permalink
Untested, json validation
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Feb 22, 2025
1 parent ff221ec commit 6ce85f5
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import os
import aiohttp
import asyncio
import sys
import re
import io
import json

intents = discord.Intents.none()
intents.message_content = True
logger = logging.getLogger('discord.bot')
base_url = os.getenv('BASE_URL', 'https://api.deckthemes.com/mappings.json')
bot_token = os.getenv('BOT_TOKEN')
Expand Down Expand Up @@ -236,6 +237,35 @@ async def convert(interaction : discord.Interaction, file: discord.Attachment):

await interaction.followup.send(file=discord_file)

# Json validator hack as beebles asked for it
@bot.event
async def on_message(msg : discord.Message):
if msg.author.bot:
return

if len(msg.attachments) <= 0:
return

filtered_attachments = [x for x in msg.attachments if x.content_type == "application/json"]

if len(filtered_attachments) <= 0:
return

failed_results : list[str, str] = []

for x in filtered_attachments:
try:
json.loads(await x.read())
except Exception as e:
failed_results.append((x.filename, str(e)))

if len(failed_results) <= 0:
await msg.add_reaction("✅")
return

await msg.add_reaction("❌")
await msg.reply(content="JSON Validation failed!\n\n" + "\n\n".join([f"**{x[0]}**\n{x[1]}" for x in failed_results]), mention_author=True)

@bot.event
async def on_ready():
asyncio.create_task(mappings_manager_instance.update_mappings())
Expand Down

0 comments on commit 6ce85f5

Please sign in to comment.