Make the minutes=1 a variable that I can change in config.py
import asyncio
import aiohttp
from aiolimiter import AsyncLimiter
import time
from bisect import bisect_left
from tqdm.asyncio import tqdm
import backoff
import logging
from google.oauth2 import service_account
from googleapiclient.discovery import build
import discord
from discord import app_commands
from discord.ext import commands
from discord.ext import tasks
import gspread
from oauth2client.service_account import ServiceAccountCredentials
Create a limiter that allows 10 requests per second
limiter = AsyncLimiter(20, 1) # 10 requests per 1 second
@backoff.on_exception(backoff.expo,
(aiohttp.ClientError, asyncio.TimeoutError),
max_tries=RETRIES,
jitter=backoff.full_jitter,
max_time=60) # Set a maximum backoff time of 60 seconds
async def fetch_data(session, url_template, skip, track_index):
async with limiter: # Use the limiter here
url = url_template.format(skip=skip)
try:
async with session.get(url, timeout=30) as response:
if response.status == 200:
data = await response.json()
return data.get("entries", [])
elif response.status == 503:
logger.warning(f"Server overloaded (503). Retrying URL: {url}")
raise aiohttp.ClientError
else:
logger.error(f"Error fetching data from {url}. Status code: {response.status}. Retrying...")
raise aiohttp.ClientError
except (aiohttp.ClientError, asyncio.TimeoutError) as e:
logger.error(f"Exception for URL {url}: {e}")
raise
while skip < max_entries:
try:
entries = await fetch_data(session, url_template, skip, track_index)
if entries:
for entry in entries:
player_name = entry["name"]
frames = entry["frames"]
player_times.setdefault(player_name, [None] * len(TRACK_URLS))
if player_times[player_name][track_index] is None or frames < player_times[player_name][track_index]:
player_times[player_name][track_index] = frames
sorted_track_times[url_template] = sorted([t for t in player_times.values() if t[track_index] is not None],
key=lambda x: x[track_index])
skip += len(entries)
else:
logger.info(f"No more entries found for track {track_index} at skip {skip}.")
break # Exit only if no more entries are found
except aiohttp.ClientError as e:
logger.error(f"Failed to fetch data for {url_template.format(skip=skip)} after retries. Error: {e}.")
# DO NOT BREAK HERE. Let backoff retry.
finally: # Ensure pbar update even on error
pbar.update(len(entries) if entries else 0) # Update with 0 if entries is None due to an error
if command_name in _command_cooldowns and current_time < _command_cooldowns[command_name]:
remaining_time = _command_cooldowns[command_name] - current_time
await interaction.response.send_message(
f"This command is on cooldown. Try again in {remaining_time:.2f} seconds.",
ephemeral=True,
)
return False
else:
_command_cooldowns[command_name] = current_time + cooldown_duration
return True
@bot.tree.command(name="player", description="Get stats for a player")
@app_commands.describe(player_name="The name of the player to look up")
async def player(interaction: discord.Interaction, player_name: str):
if await check_cooldown(interaction, "player"):
await interaction.response.defer()
try:
await update_rankings_function()
cells = sheet.findall(player_name, in_column=4, case_sensitive=True)
code
if cells:
cell = cells[0]
row_number = cell.row
stats = sheet.row_values(row_number)
headers = sheet.row_values(1)
embed = discord.Embed(title=f"Stats for {player_name}")
embed.add_field(name=headers[1], value=stats[1], inline=True) # Rank
# Add the rest of the fields, starting from index 2
for i, (header, stat) in enumerate(zip(headers[2:], stats[2:])):
embed.add_field(name=header, value=stat, inline=True)
await interaction.followup.send(embed=embed)
else:
await interaction.followup.send(
f"{player_name} not found. Please complete every track!")
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}")
@bot.tree.command(name="wr", description="Shows the world record for each track and players with the most WRs.")
async def world_records(interaction: discord.Interaction):
if await check_cooldown(interaction, "wr"):
await interaction.response.defer()
embed = discord.Embed(title="Polytrack World Records", color=discord.Color.blue())
wr_holders = {} # Dictionary to store players and their WR count
code
async with aiohttp.ClientSession() as session:
for i, url in enumerate(TRACK_URLS[:13]):
try:
async with session.get(url.format(skip=0), timeout=30) as response:
if response.status == 200:
data = await response.json()
entries = data.get("entries", [])
if entries:
world_record_frames = entries[0]["frames"]
world_record_seconds = world_record_frames / 1000
world_record_holder = entries[0]["name"]
embed.add_field(name=TRACK_NAMES[i],
value=f"**{world_record_seconds:.3f}s** by **{world_record_holder}**",
inline=False)
# Update WR count for the player
if world_record_holder in wr_holders:
wr_holders[world_record_holder] += 1
else:
wr_holders[world_record_holder] = 1
else:
embed.add_field(name=TRACK_NAMES[i],
value=f"Error fetching data. Status code: {response.status}",
inline=False)
except Exception as e:
embed.add_field(name=TRACK_NAMES[i],
value=f"An error occurred: {e}",
inline=False)
# Sort players by WR count in descending order
sorted_wr_holders = sorted(wr_holders.items(), key=lambda item: item[1], reverse=True)
# Add WR holders and their count to the embed without the header
embed.add_field(name="-" * 20, value="", inline=False) # Add a separator line without space
for player, count in sorted_wr_holders:
embed.add_field(name=player, value=f"{count} WRs", inline=False)
await interaction.followup.send(embed=embed)
import discord
from discord import app_commands
import numpy as np
... (Your other imports, TRACK_URLS, sorted_track_times, player_times, etc.)
@bot.tree.command(name="improvement", description="Get a player's improvement potential for each track.")
@app_commands.describe(player_name="The name of the player to analyze.")
async def improvement(interaction: discord.Interaction, player_name: str):
if await check_cooldown(interaction, "improvement"):
await interaction.response.defer()
try:
await update_rankings_function()
code
player_data = player_times.get(player_name)
if player_data is None:
await interaction.followup.send(f"Player '{player_name}' not found in the rankings.")
return
table_data = []
total_pb_seconds = 0
total_wr_seconds = 0
for i, track_url in enumerate(TRACK_URLS):
pb_frames = player_data[i]
if pb_frames is None:
table_data.append(
[f"T{i+1}", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A"])
continue
wr_frames = sorted_track_times[track_url][0][i]
pb_seconds = pb_frames / 1000
wr_seconds = wr_frames / 1000
total_pb_seconds += pb_seconds
total_wr_seconds += wr_seconds
# Calculate metrics with significant figures
difference = pb_seconds - wr_seconds
sig_figs = 3 # Example: using 3 significant figures
# Function to format a number with significant figures
def format_sigfigs(value, sig_figs):
if value == 0:
return "0"
return f"{float(f'{value:.{sig_figs}g}')}"
difference_str = format_sigfigs(difference, sig_figs)
inverse_performance_ratio_str = format_sigfigs(wr_seconds / pb_seconds if pb_seconds else float("inf"),
sig_figs)
relative_difference_str = format_sigfigs(difference / wr_seconds if wr_seconds else float("inf"),
sig_figs)
logarithmic_difference_str = format_sigfigs(
np.log(pb_seconds / wr_seconds) if wr_seconds and pb_seconds > 0 else float("inf"), sig_figs)
harmonic_mean_str = format_sigfigs(
2 * (pb_seconds * wr_seconds) / (pb_seconds + wr_seconds) if pb_seconds + wr_seconds else float(
"inf"), sig_figs)
normalized_difference_str = format_sigfigs(
difference / (pb_seconds + wr_seconds) if pb_seconds + wr_seconds else float("inf"), sig_figs)
exponential_difference_str = format_sigfigs(
np.exp(difference / wr_seconds) if wr_seconds else float("inf"), sig_figs)
inverse_relative_difference_str = format_sigfigs(
wr_seconds / difference if difference else float("inf"), sig_figs)
quadratic_difference_str = format_sigfigs(
(difference / wr_seconds) ** 2 if wr_seconds else float("inf"), sig_figs)
table_data.append([
f"T{i+1}", # Abbreviated Track column
f"{pb_seconds:.3f}",
f"{wr_seconds:.3f}",
difference_str,
inverse_performance_ratio_str,
relative_difference_str,
logarithmic_difference_str,
harmonic_mean_str,
normalized_difference_str,
exponential_difference_str,
inverse_relative_difference_str,
quadratic_difference_str
])
# Total Row Calculations (using the same significant figures)
total_difference = total_pb_seconds - total_wr_seconds
total_difference_str = format_sigfigs(total_pb_seconds - total_wr_seconds, sig_figs)
total_inverse_performance_ratio_str = format_sigfigs(
total_wr_seconds / total_pb_seconds if total_pb_seconds else float("inf"), sig_figs)
total_relative_difference_str = format_sigfigs(
total_difference / total_wr_seconds if total_wr_seconds else float("inf"), sig_figs)
total_logarithmic_difference_str = format_sigfigs(
np.log(total_pb_seconds / total_wr_seconds) if total_wr_seconds and total_pb_seconds > 0 else float(
"inf"), sig_figs)
total_harmonic_mean_str = format_sigfigs(2 * (total_pb_seconds * total_wr_seconds) / (
total_pb_seconds + total_wr_seconds) if total_pb_seconds + total_wr_seconds else float("inf"),
sig_figs)
total_normalized_difference_str = format_sigfigs(total_difference / (
total_pb_seconds + total_wr_seconds) if total_pb_seconds + total_wr_seconds else float("inf"),
sig_figs)
total_exponential_difference_str = format_sigfigs(
np.exp(total_difference / total_wr_seconds) if total_wr_seconds else float("inf"), sig_figs)
total_inverse_relative_difference_str = format_sigfigs(
total_wr_seconds / total_difference if total_difference else float("inf"), sig_figs)
total_quadratic_difference_str = format_sigfigs(
(total_difference / total_wr_seconds) ** 2 if total_wr_seconds else float("inf"), sig_figs)
table_data.append([
"All",
f"{total_pb_seconds:.3f}",
f"{total_wr_seconds:.3f}",
total_difference_str,
total_inverse_performance_ratio_str,
total_relative_difference_str,
total_logarithmic_difference_str,
total_harmonic_mean_str,
total_normalized_difference_str,
total_exponential_difference_str,
total_inverse_relative_difference_str,
total_quadratic_difference_str
])
# Compact column widths further
table_str = "```\n"
table_str += f"{'T':<3} {'PB':<7} {'WR':<7} {'Diff':<5} {'IPR':<5} {'RelD':<8} {'LogD':<8} {'Harm':<5} {'NormD':<8} {'ExpD':<6} {'InvRD':<7} {'QuadD':<6}\n" # Shortened column names
table_str += "-" * 94 + "\n" # Adjusted separator length
for row in table_data:
table_str += f"{row[0]:<3} {row[1]:<7} {row[2]:<7} {row[3]:<5} {row[4]:<5} {row[5]:<8} {row[6]:<8} {row[7]:<5} {row[8]:<8} {row[9]:<6} {row[10]:<7} {row[11]:<6}\n"
table_str += "```\n"
await interaction.followup.send(f"Improvement potential for **{player_name}**:\n{table_str}")
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}")
--- Metric Explanation Commands ---
def create_metric_explanation_command(metric_name, explanation):
"""Creates a slash command function for a metric explanation."""
code
@bot.tree.command(name=metric_name.lower(), description=f"Explanation of the {metric_name} metric.")
async def metric_explanation_command(interaction: discord.Interaction):
if await check_cooldown(interaction, metric_name.lower()):
embed = discord.Embed(title=f"{metric_name} Metric Explanation", color=discord.Color.blue())
embed.add_field(name="Explanation", value=explanation, inline=False)
await interaction.response.send_message(embed=embed)
return metric_explanation_command
Create a command for each metric explanation from config.py
for metric_name, explanation in METRIC_EXPLANATIONS.items():
create_metric_explanation_command(metric_name, explanation)
@bot.tree.command(name="help", description="Show available commands and their usage.")
async def help_command(interaction: discord.Interaction):
if await check_cooldown(interaction, "help"):
embed = discord.Embed(title="Available Commands", color=discord.Color.blue())
embed.add_field(name="/player <player_name>", value="Get stats for a player.", inline=False)
embed.add_field(name="/rankings", value="Get the link to the global rankings.", inline=False)
embed.add_field(name="/wr", value="Shows the world record for each track.", inline=False)
embed.add_field(name="/improvement <player_name>",
value="Shows the improvement potential for each track for the given player.", inline=False)
embed.add_field(name="/help", value="Show this help message.", inline=False)
code
# Add a general note about metric explanation commands
embed.add_field(name="Metric Explanations",
value="For detailed explanations of each metric, use commands like `/diff`, `/ipr`, `/reldiff`, etc.",
inline=False)
await interaction.response.send_message(embed=embed)
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
update_spreadsheet_task.start() # Start the background task
await bot.tree.sync()
bot.run(DISCORD_BOT_TOKEN)
To make the minutes=1 in your background task loop a variable that can be changed in config.py, you'll need to define a variable in your config.py file and then use that variable in your main code.
Here's how you can achieve this:
Update your config.py:
Add a new variable called LOOP_INTERVAL_MINUTES (or any name you prefer) to your config.py file.
python
# config.py
# ... (other configuration variables)
LOOP_INTERVAL_MINUTES = 1 # You can change this value to set the desired interval
Modify your main code to use this variable:
In your main code, since you're already importing everything from config.py with from config import *, you can directly use LOOP_INTERVAL_MINUTES in place of the hardcoded minutes=1.
python
# ... (your existing code)
# --- Background Task to Update Spreadsheet ---
@tasks.loop(minutes=LOOP_INTERVAL_MINUTES)
async def update_spreadsheet_task():
try:
await update_rankings_function()
except Exception as e:
print(f"Error updating spreadsheet: {e}")
# --- Run the Discord Bot ---
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
update_spreadsheet_task.start() # Start the background task
await bot.tree.sync()
Ensure the variable is correctly imported:
Make sure the import statement from config import * is present at the top of your main code so that all variables from config.py, including LOOP_INTERVAL_MINUTES, are available.
python
# Import all configuration variables
from config import *
Explanation:
By defining LOOP_INTERVAL_MINUTES in config.py, you centralize the configuration of the loop interval. This makes it easy to change without modifying your main code.
Using @tasks.loop(minutes=LOOP_INTERVAL_MINUTES) tells the task decorator to use the value specified in config.py for the interval.
This approach improves maintainability and flexibility, allowing changes to the loop interval to be made in one place.
Example Usage:
If you want to change the loop to run every 5 minutes instead of 1, simply update LOOP_INTERVAL_MINUTES in config.py:
python
# config.py
LOOP_INTERVAL_MINUTES = 5 # The task will now loop every 5 minutes
Final Code Snippet:
python
# main.py
import asyncio
import aiohttp
from aiolimiter import AsyncLimiter
import time
from bisect import bisect_left
from tqdm.asyncio import tqdm
import backoff
import logging
from google.oauth2 import service_account
from googleapiclient.discovery import build
import discord
from discord import app_commands
from discord.ext import commands
from discord.ext import tasks
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# Import all configuration variables
from config import *
# ... (rest of your imports and code)
# --- Background Task to Update Spreadsheet ---
@tasks.loop(minutes=LOOP_INTERVAL_MINUTES)
async def update_spreadsheet_task():
try:
await update_rankings_function()
except Exception as e:
print(f"Error updating spreadsheet: {e}")
# --- Run the Discord Bot ---
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
update_spreadsheet_task.start() # Start the background task
await bot.tree.sync()
bot.run(DISCORD_BOT_TOKEN)
Note: Remember to handle cases where LOOP_INTERVAL_MINUTES might be set to invalid values (e.g., zero or negative numbers). You might want to add validation in your code or ensure that config.py provides a sensible default.