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
|
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
|