diff --git a/.gitignore b/.gitignore index 8965cc5..4f1cc74 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.venv -temp.md \ No newline at end of file +temp.md +*data \ No newline at end of file diff --git a/__pycache__/config.cpython-313.pyc b/__pycache__/config.cpython-313.pyc index a6db65d..0ad30fd 100644 Binary files a/__pycache__/config.cpython-313.pyc and b/__pycache__/config.cpython-313.pyc differ diff --git a/cogs/__pycache__/commands.cpython-313.pyc b/cogs/__pycache__/commands.cpython-313.pyc index 006efa9..c61d92a 100644 Binary files a/cogs/__pycache__/commands.cpython-313.pyc and b/cogs/__pycache__/commands.cpython-313.pyc differ diff --git a/cogs/commands.py b/cogs/commands.py index 1da96a5..7946083 100644 --- a/cogs/commands.py +++ b/cogs/commands.py @@ -1,6 +1,6 @@ from discord.ext import commands -from services.torn_api import fetch_enemy_members -from services.ffscouter import fetch_batch_stats +from services.torn_api import update_enemy_faction, update_friendly_faction + class HitCommands(commands.Cog): def __init__(self, bot, enrolled_attackers, enemy_queue): @@ -26,29 +26,19 @@ class HitCommands(commands.Cog): self.enrolled_attackers.remove(user_id) await ctx.send(f"{ctx.author.mention} has been removed from the rotation.") + @commands.command() - async def stats(self, ctx): - members = await fetch_enemy_members() - if not members: - await ctx.send("No active members found.") - return + async def update_enemy(self, ctx): + success = await update_enemy_faction() + if success: + await ctx.send("Enemy faction data updated with estimated stats!") + else: + await ctx.send("Failed to update enemy faction data.") - ids = [m["id"] for m in members if m.get("status", {}).get("state") in ("Okay", "Idle")] - ff_map = await fetch_batch_stats(ids) - - lines = [] - for m in members: - pid = str(m["id"]) - est = ff_map.get(pid, {}).get("bs_estimate_human", "?") - if m.get("status", {}).get("state") not in ("Okay", "Idle"): - continue - lines.append(f"**{m['name']}** (ID:{pid}) | Lv {m['level']} | Estimated BS: {est}") - - chunk = "" - for line in lines: - if len(chunk) + len(line) > 1900: - await ctx.send(chunk) - chunk = "" - chunk += line + "\n" - if chunk: - await ctx.send(chunk) + @commands.command() + async def update_friendly(self, ctx): + success = await update_friendly_faction() + if success: + await ctx.send("Friendly faction data updated with estimated stats!") + else: + await ctx.send("Failed to update friendly faction data.") \ No newline at end of file diff --git a/config.py b/config.py index 67a5122..98f8668 100644 --- a/config.py +++ b/config.py @@ -1,7 +1,7 @@ # Torn API TORN_API_KEY = "9VLK0Wte1BwXOheB" -ENEMY_FACTION_ID = 52935 -YOUR_FACTION_ID = 654321 +ENEMY_FACTION_ID = 55325 +YOUR_FACTION_ID = 52935 ALLOWED_CHANNEL_ID = 1442876328536707316 # FFScouter API diff --git a/services/__pycache__/torn_api.cpython-313.pyc b/services/__pycache__/torn_api.cpython-313.pyc index c0256b1..582c96e 100644 Binary files a/services/__pycache__/torn_api.cpython-313.pyc and b/services/__pycache__/torn_api.cpython-313.pyc differ diff --git a/services/torn_api.py b/services/torn_api.py index 7ce297c..cfbbd0e 100644 --- a/services/torn_api.py +++ b/services/torn_api.py @@ -1,12 +1,68 @@ +# services/torn_api.py import aiohttp -from config import TORN_API_KEY, ENEMY_FACTION_ID +import json +from pathlib import Path +from config import TORN_API_KEY, ENEMY_FACTION_ID, YOUR_FACTION_ID +from .ffscouter import fetch_batch_stats + +ENEMY_FILE = Path("data/enemy_faction.json") +FRIENDLY_FILE = Path("data/friendly_faction.json") + + +async def fetch_and_save_faction(faction_id: int, file_path: Path) -> bool: + """ + Fetches faction members from Torn, fetches their estimated BS from FFScouter, + and saves everything to a JSON file. + """ + url = f"https://api.torn.com/v2/faction/{faction_id}?selections=members&key={TORN_API_KEY}" -async def fetch_enemy_members(): - url = f"https://api.torn.com/v2/faction/{ENEMY_FACTION_ID}?selections=members&key={TORN_API_KEY}" async with aiohttp.ClientSession() as session: async with session.get(url) as resp: if resp.status != 200: - print("Torn faction fetch error:", resp.status) - return [] + print(f"Torn faction fetch error: {resp.status}") + return False data = await resp.json() - return data.get("members", []) + + members_list = data.get("members", []) + if not members_list: + return False + + # Build list of IDs (Torn uses 'id', not 'player_id') + member_ids = [info.get("id") for info in members_list if "id" in info] + if not member_ids: + return False + + # Fetch batch FFScouter stats + ff_data = await fetch_batch_stats(member_ids) # returns dict keyed by player_id + + # Build final faction data + faction_data = [] + for info in members_list: + pid = info.get("id") + if pid is None: + continue + + est = ff_data.get(str(pid), {}).get("bs_estimate_human", "?") + member = { + "id": pid, + "name": info.get("name", "Unknown"), + "level": info.get("level", 0), + "estimate": est + } + faction_data.append(member) + + # Save to file + file_path.parent.mkdir(exist_ok=True, parents=True) # ensure folder exists + with open(file_path, "w", encoding="utf-8") as f: + json.dump(faction_data, f, indent=2) + + return True + + +# Wrappers for clarity +async def update_enemy_faction() -> bool: + return await fetch_and_save_faction(ENEMY_FACTION_ID, ENEMY_FILE) + + +async def update_friendly_faction() -> bool: + return await fetch_and_save_faction(YOUR_FACTION_ID, FRIENDLY_FILE)