dynamic config reloading
This commit is contained in:
@@ -7,7 +7,7 @@ from typing import Dict, Optional
|
||||
from datetime import datetime
|
||||
from services.server_state import STATE
|
||||
from services.activity_log import activity_logger
|
||||
from config import ASSIGNMENT_TIMEOUT, ASSIGNMENT_REMINDER, ALLOWED_CHANNEL_ID, CHAIN_TIMER_THRESHOLD, TORN_API_KEY
|
||||
import config
|
||||
|
||||
class BotAssignmentManager:
|
||||
def __init__(self, bot):
|
||||
@@ -54,7 +54,7 @@ class BotAssignmentManager:
|
||||
return None
|
||||
|
||||
try:
|
||||
url = f"https://api.torn.com/v2/faction/{STATE.friendly_faction_id}/chain?key={TORN_API_KEY}"
|
||||
url = f"https://api.torn.com/v2/faction/{STATE.friendly_faction_id}/chain?key={config.TORN_API_KEY}"
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url) as resp:
|
||||
if resp.status != 200:
|
||||
@@ -161,7 +161,7 @@ class BotAssignmentManager:
|
||||
return
|
||||
|
||||
self.chain_timeout = timeout
|
||||
threshold_seconds = CHAIN_TIMER_THRESHOLD * 60
|
||||
threshold_seconds = config.CHAIN_TIMER_THRESHOLD * 60
|
||||
|
||||
# Check if we should enter chain mode
|
||||
if timeout > 0 and timeout <= threshold_seconds and not self.chain_active:
|
||||
@@ -203,7 +203,7 @@ class BotAssignmentManager:
|
||||
async def send_chain_expiration_warning(self):
|
||||
#Send @here alert that chain is about to expire
|
||||
try:
|
||||
channel = self.bot.get_channel(ALLOWED_CHANNEL_ID)
|
||||
channel = self.bot.get_channel(config.ALLOWED_CHANNEL_ID)
|
||||
if channel and STATE.friendly_faction_id:
|
||||
faction_link = f"https://www.torn.com/factions.php?step=your#/tab=chain"
|
||||
message = f"@here **CHAIN EXPIRING IN 30 SECONDS!** Attack a faction member to keep it alive!\n{faction_link}"
|
||||
@@ -366,16 +366,16 @@ class BotAssignmentManager:
|
||||
|
||||
# Send Discord message to channel
|
||||
attack_link = f"https://www.torn.com/loader.php?sid=attack&user2ID={enemy_id}"
|
||||
message = f"**New target for {discord_user.mention}!**\n\n[**{enemy.name}** (Level {enemy.level})]({attack_link})\n\nYou have {ASSIGNMENT_TIMEOUT} seconds!"
|
||||
message = f"**New target for {discord_user.mention}!**\n\n[**{enemy.name}** (Level {enemy.level})]({attack_link})\n\nYou have {config.ASSIGNMENT_TIMEOUT} seconds!"
|
||||
|
||||
channel = self.bot.get_channel(ALLOWED_CHANNEL_ID)
|
||||
channel = self.bot.get_channel(config.ALLOWED_CHANNEL_ID)
|
||||
if channel:
|
||||
await channel.send(message)
|
||||
print(f"Assigned {enemy.name} to {friendly.name} (Discord: {discord_user.name})")
|
||||
# Log to activity
|
||||
await activity_logger.log_action("System", "Hit Assigned", f"{friendly.name} -> {enemy.name} (Level {enemy.level})")
|
||||
else:
|
||||
print(f"Assignment channel {ALLOWED_CHANNEL_ID} not found")
|
||||
print(f"Assignment channel {config.ALLOWED_CHANNEL_ID} not found")
|
||||
self.active_targets[key]["failed"] = True
|
||||
except Exception as e:
|
||||
print(f"Failed to send Discord message to channel: {e}")
|
||||
@@ -434,12 +434,12 @@ class BotAssignmentManager:
|
||||
continue
|
||||
|
||||
# Send reminder (only for successful assignments)
|
||||
if elapsed >= ASSIGNMENT_REMINDER and not data["reminded"]:
|
||||
if elapsed >= config.ASSIGNMENT_REMINDER and not data["reminded"]:
|
||||
discord_id = data["discord_id"]
|
||||
try:
|
||||
discord_user = await self.bot.fetch_user(discord_id)
|
||||
remaining = ASSIGNMENT_TIMEOUT - ASSIGNMENT_REMINDER
|
||||
channel = self.bot.get_channel(ALLOWED_CHANNEL_ID)
|
||||
remaining = config.ASSIGNMENT_TIMEOUT - config.ASSIGNMENT_REMINDER
|
||||
channel = self.bot.get_channel(config.ALLOWED_CHANNEL_ID)
|
||||
if channel:
|
||||
await channel.send(f"**Reminder:** {discord_user.mention} - Target {enemy.name} - {remaining} seconds left!")
|
||||
data["reminded"] = True
|
||||
@@ -447,7 +447,7 @@ class BotAssignmentManager:
|
||||
pass
|
||||
|
||||
# Reassign after timeout
|
||||
if elapsed >= ASSIGNMENT_TIMEOUT:
|
||||
if elapsed >= config.ASSIGNMENT_TIMEOUT:
|
||||
to_reassign.append((data["group_id"], enemy_id))
|
||||
del self.active_targets[key]
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import aiohttp
|
||||
from config import FFSCOUTER_KEY
|
||||
import config
|
||||
|
||||
async def fetch_batch_stats(ids: list[int]):
|
||||
#Fetches predicted stats for a list of Torn IDs in a single FFScouter request.
|
||||
@@ -9,7 +9,7 @@ async def fetch_batch_stats(ids: list[int]):
|
||||
return {}
|
||||
|
||||
ids_str = ",".join(map(str, ids))
|
||||
url = f"https://ffscouter.com/api/v1/get-stats?key={FFSCOUTER_KEY}&targets={ids_str}"
|
||||
url = f"https://ffscouter.com/api/v1/get-stats?key={config.FFSCOUTER_KEY}&targets={ids_str}"
|
||||
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url) as resp:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# services/torn_api.py
|
||||
import aiohttp
|
||||
import asyncio
|
||||
from config import TORN_API_KEY
|
||||
import config
|
||||
from .ffscouter import fetch_batch_stats
|
||||
from .server_state import STATE
|
||||
|
||||
@@ -21,7 +21,7 @@ async def populate_faction(faction_id: int, kind: str):
|
||||
#Fetch members + FFScouter estimates once and store in STATE.
|
||||
#kind: "friendly" or "enemy"
|
||||
|
||||
url = f"https://api.torn.com/v2/faction/{faction_id}?selections=members&key={TORN_API_KEY}"
|
||||
url = f"https://api.torn.com/v2/faction/{faction_id}?selections=members&key={config.TORN_API_KEY}"
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
@@ -86,7 +86,7 @@ async def refresh_status_loop(faction_id: int, kind: str, lock: asyncio.Lock, in
|
||||
#Periodically refresh member statuses in STATE.
|
||||
while True:
|
||||
try:
|
||||
url = f"https://api.torn.com/v2/faction/{faction_id}?selections=members&key={TORN_API_KEY}"
|
||||
url = f"https://api.torn.com/v2/faction/{faction_id}?selections=members&key={config.TORN_API_KEY}"
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url) as resp:
|
||||
if resp.status != 200:
|
||||
|
||||
Reference in New Issue
Block a user