From b63c2933b244feebeb1c26fd31b02e5655a867e1 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 12 Jul 2026 10:19:24 +0200 Subject: init --- bot.py | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 bot.py (limited to 'bot.py') diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..299ae05 --- /dev/null +++ b/bot.py @@ -0,0 +1,112 @@ +import json +import os +import discord +from discord import app_commands +from discord.ext import commands, tasks +import api +from models import User + +async def sync(user): + print("Initializing active sync sequence...") + + patch_version = api.get_latest_ddragon_version() + id_to_champ = api.get_champion_mappings(patch_version) + + rank_string, rank_file_key = api.fetch_player_rank(user) + top_4_masteries = api.fetch_top_champions(user) + widget_payload = [] + + widget_payload.extend([ + {"type": 1, "name": "rank", "value": rank_string}, + { + "type": 3, + "name": "rank_icon", + "value": {"url": f"https://raw.communitydragon.org/latest/plugins/rcp-fe-lol-shared-components/global/default/{rank_file_key}.png"} + } + ]) + + for index, mastery in enumerate(top_4_masteries, start=1): + champ_id = mastery["championId"] + level = mastery["championLevel"] + points = mastery["championPoints"] + + champ_info = id_to_champ.get(champ_id, {"name": f"Unknown ({champ_id})", "image_key": "Aatrox"}) + pts_display = f"{points:,}" if points < 100000 else f"{points // 1000}k" + + widget_payload.extend([ + {"type": 1, "name": f"c{index}_name", "value": champ_info["name"]}, + {"type": 1, "name": f"c{index}_sub", "value": f"Lvl {level} • {pts_display}"}, + { + "type": 3, + "name": f"c{index}_img", + "value": {"url": f"https://ddragon.leagueoflegends.com/cdn/{patch_version}/img/champion/{champ_info['image_key']}.png"} + } + ]) + + api.update_discord_widget(widget_payload, user) + +DATA_FILE = "users.json" + +def load_users(): + if not os.path.exists(DATA_FILE): + return {} + with open(DATA_FILE, "r") as f: + data = json.load(f) + # Reconstruct the dict: {discord_id: User_object} + return {int(uid): User(**u_data) for uid, u_data in data.items()} + +def save_users(users_dict): + # Convert User objects to dicts for JSON storage + serializable = {uid: u.__dict__ for uid, u in users_dict.items()} + with open(DATA_FILE, "w") as f: + json.dump(serializable, f, indent=4) + +def setup_bot(): + intents = discord.Intents.default() + + bot = commands.Bot(command_prefix="!", intents=intents) + + tracked_users = load_users() + + @tasks.loop(minutes=30) + async def periodic_sync(): + for discord_id, user in tracked_users.items(): + try: + await sync(user) + except Exception as e: + print(f"Failed to sync user {discord_id}: {e}") + + @bot.event + async def on_ready(): + TEST_GUILD = discord.Object(id=1367601681931698397) + + bot.tree.copy_global_to(guild=TEST_GUILD) + + synced = await bot.tree.sync(guild=TEST_GUILD) + + print(f"Logged in as {bot.user}") + print(f"Synced {len(synced)} command(s) to guild 1367601681931698397") + + @bot.tree.command(name="sync", description="Sync or update your League profile") + async def setup_user(interaction: discord.Interaction, game_name: str, tag_line: str, platform: str): + await interaction.response.defer(thinking=True) + + user = User( + discord_id=interaction.user.id, + game_name=game_name, + tag_line=tag_line, + region=api.get_region_from_platform(platform), + platform=platform + ) + + user.puuid = api.get_player_puuid(user) + + tracked_users[interaction.user.id] = user + save_users(tracked_users) + + await sync(user) + + await interaction.followup.send(f"""Successfully synced {game_name}#{tag_line}! + https://discord.com/oauth2/authorize?client_id=1521477342286053517&response_type=token&redirect_uri=https%3A%2F%2Fdiscord.com&scope=sdk.social_layer+openid""") + + return bot \ No newline at end of file -- cgit v1.2.3