import asyncio
import aiohttp
from aiolimiter import AsyncLimiter
import time
from bisect import bisect_left
from tqdm.asyncio import tqdm
import backoff
import logging
import numpy as np
from scipy import stats
from scipy.optimize import brentq, minimize_scalar
from google.oauth2 import service_account
from googleapiclient.discovery import build
import discord
from discord import app_commands
from discord.ext import commands
import gspread
from oauth2client.service_account import ServiceAccountCredentials
def get_tier(percentile):
cumulative_percentage = 0
for i, threshold in enumerate(tier_distribution):
cumulative_percentage += threshold
if percentile <= cumulative_percentage * 100:
return i + 1
return N_TIERS
def get_tier_name(tier_number):
"""Returns the name of the tier given its number."""
return TIER_NAMES[tier_number - 1]
Create a limiter that allows 10 requests per second
limiter = AsyncLimiter(10, 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)
if cells:
cell = cells[0]
row_number = cell.row
stats = sheet.row_values(row_number)
headers = sheet.row_values(1)
tier_image_url = stats[-1]
# Get tier name directly from the tier_images dictionary
for tier_number, image_url in TIER_IMAGES.items():
if image_url == tier_image_url:
tier_name = get_tier_name(tier_number)
break # Exit loop once tier name is found
embed = discord.Embed(title=f"Stats for {player_name}")
# Add Tier before Rank
embed.add_field(name="Tier", value=tier_name, inline=True)
embed.add_field(name=headers[1], value=stats[1], inline=True) # Rank
# Add the rest of the fields, skipping "Tier" header
for i, (header, stat) in enumerate(zip(headers[2:-1], stats[2:-1])):
if i != 1: # Skip the "Rank" header and value
embed.add_field(name=header, value=stat, inline=True)
embed.set_image(url=tier_image_url)
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
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()
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."""
@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)
# 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)
connector = aiohttp.TCPConnector(limit_per_host=13)
async with aiohttp.ClientSession(connector=connector) as session:
with tqdm(total=MAX_SKIP * len(TRACK_URLS), desc="Processing tracks") as pbar:
tasks = [process_track(session, url_template, i, pbar) for i, url_template in enumerate(TRACK_URLS)]
await asyncio.gather(*tasks)
player_data = {}
for player, times in player_times.items():
if all(t is not None for t in times):
total_time = sum(times)
ranks = [bisect_left(sorted_track_times[TRACK_URLS[i]], times[i], key=lambda x: x[i]) + 1 for i in
range(len(TRACK_URLS))]
average_rank = sum(ranks) / len(ranks)
best_rank = min(ranks)
worst_rank = max(ranks)
player_data[player] = {
"total_time": total_time,
"average_rank": average_rank,
"best_rank": best_rank,
"worst_rank": worst_rank,
"track_times": times,
"ranks": ranks
}
sorted_players = sorted(player_data.items(), key=lambda x: x[1]["total_time"])
sheet_data = []
header = ["Rank", "Percentile", "Player Name", "Total Time", "Average Rank", "Best Rank", "Worst Rank"]
header.extend(TRACK_NAMES)
header.append("Tier")
sheet_data.append(header)
for i, (player, data) in enumerate(sorted_players, start=1):
overall_percentile = (len(sorted_players) - i + 1) / len(sorted_players) * 100
tier = get_tier(overall_percentile)
tier = max(1, min(tier, N_TIERS))
tier_image = TIER_IMAGES.get(tier, "")
total_seconds = data["total_time"] / 1000
hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
milliseconds = int((seconds % 1) * 1000)
seconds = int(seconds)
minutes = int(minutes)
hours = int(hours)
if hours > 0:
formatted_total_time = f"{hours}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}"
elif minutes > 0:
formatted_total_time = f"{minutes}:{seconds:02d}.{milliseconds:03d}"
else:
formatted_total_time = f"{seconds}.{milliseconds:03d}"
formatted_average_rank = format_average_rank(data['average_rank'])
row = [i, f"{overall_percentile:.2f}", f"'{player}", formatted_total_time, formatted_average_rank,
data["best_rank"], data["worst_rank"]]
for j in range(len(TRACK_URLS)):
track_time = data["track_times"][j]
rank = data["ranks"][j]
formatted_track_time = f"{track_time / 1000:.3f}"
row.append(f"{formatted_track_time}s (#{rank})")
row.append(tier_image)
sheet_data.append(row)
update_google_sheet(sheet_data)
end_time = time.time()
total_time = end_time - start_time
hours, remainder = divmod(total_time, 3600)
minutes, seconds = divmod(remainder, 60)
milliseconds = int((seconds % 1) * 1000)
seconds = int(seconds)
minutes = int(minutes)
hours = int(hours)
if hours > 0:
formatted_time = f"{hours}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}"
elif minutes > 0:
formatted_time = f"{minutes}:{seconds:02d}.{milliseconds:03d}"
else:
formatted_time = f"{seconds}.{milliseconds:03d}"
print(f"Data updated in Google Sheets")
print(f"Total execution time: {formatted_time}")
--- Run the Discord Bot ---
bot.run(DISCORD_BOT_TOKEN)
"C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Scripts\python.exe" "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on\Polystats 0.1\polystats.py"
2024-10-06 19:42:33,753 WARNING PyNaCl is not installed, voice will NOT be supported
[2024-10-06 19:42:34] [INFO ] discord.client: logging in using static token
2024-10-06 19:42:34,789 INFO logging in using static token
[2024-10-06 19:42:35] [INFO ] discord.gateway: Shard ID None has connected to Gateway (Session ID: 0776bb9b02ead7789beb4ebe47694f93).
2024-10-06 19:42:35,617 INFO Shard ID None has connected to Gateway (Session ID: 0776bb9b02ead7789beb4ebe47694f93).
PolyStats#7286 has connected to Discord!
Processing tracks: 39%|███▉ | 336000/851968 [28:01<40:41, 211.37it/s][2024-10-06 20:10:48] [INFO ] discord.gateway: Shard ID None has successfully RESUMED session 0776bb9b02ead7789beb4ebe47694f93.
2024-10-06 20:10:48,161 INFO Shard ID None has successfully RESUMED session 0776bb9b02ead7789beb4ebe47694f93.
Processing tracks: 852020it [1:22:44, 171.62it/s]
2024-10-06 21:05:30,777 INFO file_cache is only supported with oauth2client<4.0.0
2024-10-06 21:05:30,780 INFO file_cache is only supported with oauth2client<4.0.0
'0.4'!B1:Z2745 range cleared.
None cells updated.
54957 cells updated.
Data updated in Google Sheets
Total execution time: 1:22:50.935
[2024-10-06 21:05:38] [ERROR ] discord.app_commands.tree: Ignoring exception in command 'player'
Traceback (most recent call last):
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on\Polystats 0.1\polystats.py", line 272, in player
await interaction.followup.send(embed=embed)
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\webhook\async_.py", line 1843, in send
data = await adapter.execute_webhook(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\webhook\async_.py", line 223, in request
raise HTTPException(response, data)
discord.errors.HTTPException: 401 Unauthorized (error code: 50027): Invalid Webhook Token
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\app_commands\commands.py", line 858, in do_call
return await self.callback(interaction, **params) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on\Polystats 0.1\polystats.py", line 277, in player
await interaction.followup.send(f"An error occurred: {e}")
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\webhook\async.py", line 1843, in send
data = await adapter.execute_webhook(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\webhook\async.py", line 223, in request
raise HTTPException(response, data)
discord.errors.HTTPException: 401 Unauthorized (error code: 50027): Invalid Webhook Token
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\app_commands\tree.py", line 1310, in _call
await command._invoke_with_namespace(interaction, namespace)
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\app_commands\commands.py", line 883, in _invoke_with_namespace
return await self.do_call(interaction, transformed_values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\app_commands\commands.py", line 876, in do_call
raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'player' raised an exception: HTTPException: 401 Unauthorized (error code: 50027): Invalid Webhook Token
2024-10-06 21:05:38,795 ERROR Ignoring exception in command 'player'
Traceback (most recent call last):
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on\Polystats 0.1\polystats.py", line 272, in player
await interaction.followup.send(embed=embed)
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\webhook\async.py", line 1843, in send
data = await adapter.execute_webhook(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\webhook\async.py", line 223, in request
raise HTTPException(response, data)
discord.errors.HTTPException: 401 Unauthorized (error code: 50027): Invalid Webhook Token
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\app_commands\commands.py", line 858, in do_call
return await self.callback(interaction, **params) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on\Polystats 0.1\polystats.py", line 277, in player
await interaction.followup.send(f"An error occurred: {e}")
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\webhook\async.py", line 1843, in send
data = await adapter.execute_webhook(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\webhook\async.py", line 223, in request
raise HTTPException(response, data)
discord.errors.HTTPException: 401 Unauthorized (error code: 50027): Invalid Webhook Token
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\app_commands\tree.py", line 1310, in _call
await command._invoke_with_namespace(interaction, namespace)
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\app_commands\commands.py", line 883, in _invoke_with_namespace
return await self._do_call(interaction, transformed_values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\sebas\PycharmProjects\figuring out what the hell is going on.venv\Lib\site-packages\discord\app_commands\commands.py", line 876, in _do_call
raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'player' raised an exception: HTTPException: 401 Unauthorized (error code: 50027): Invalid Webhook Token
The error you're encountering, discord.errors.HTTPException: 401 Unauthorized (error code: 50027): Invalid Webhook Token, typically occurs when you attempt to send a follow-up message to a Discord interaction that has expired. In your case, the issue arises because the interaction is being held open for too long due to the lengthy execution time of update_rankings_function().
Explanation:
Discord Interaction Time Limits: Discord requires that interactions (like slash commands) receive an initial response within 3 seconds. If you need more time, you can defer the response using await interaction.response.defer(), which acknowledges the interaction and gives you up to 15 minutes to send a follow-up message.
Long-Running Tasks: Your update_rankings_function() takes over 1 hour to complete. This exceeds Discord's maximum allowed duration for an interaction, causing the webhook token to become invalid by the time you attempt to send the follow-up message.
Solution:
To resolve this, you should avoid performing long-running tasks directly within an interaction command. Instead, you can:
Use Background Tasks or Caching:
Precompute Data: Run update_rankings_function() periodically in the background (e.g., every hour) and cache the results.
Quick Response: When the /player command is invoked, immediately use the cached data to generate a response without delay.
Notify the User:
Initial Acknowledgement: Send an immediate response to the user indicating that the data is being processed.
Follow-Up Message: Since the processing exceeds 15 minutes, you cannot send a follow-up interaction. Instead, consider sending the result via a direct message or in a specific channel once processing is complete.
Redesign the Command:
Separate Commands: Have a command that updates the rankings (/updaterankings) and restrict its usage to authorized users.
User Command: The /player command simply fetches and displays data from the most recently updated cache.
Here's how you can modify your code:
Schedule Regular Updates:
Use Python's asyncio to create a background task that updates the rankings periodically.
async def periodic_update():
await bot.wait_until_ready()
while not bot.is_closed():
await update_rankings_function()
await asyncio.sleep(3600) # Wait for 1 hour before next update
bot.loop.create_task(periodic_update())
Modify the /player Command:
Remove the call to await update_rankings_function() inside the command.
@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"):
try:
cells = sheet.findall(player_name, in_column=4, case_sensitive=True)
# ... rest of your code
Handle Data Not Found:
If the data is outdated or the player is not found, inform the user appropriately.
if not cells:
await interaction.response.send_message(
f"{player_name} not found in the current rankings or data is outdated. Please try again later."
)
Provide Feedback on Update Status:
Optionally, create a command for admins to manually trigger an update and inform users about the last update time.
@bot.tree.command(name="last_update", description="Shows the last rankings update time.")
async def last_update(interaction: discord.Interaction):
# Assume you store the last update time in a variable
await interaction.response.send_message(
f"The rankings were last updated at {last_update_time}."
)
Summary:
Don't perform long tasks in interactions. Instead, use background tasks or scheduled updates.
Ensure responses are within Discord's interaction limits.
By restructuring your bot to update rankings in the background and respond to user commands promptly using cached data, you can prevent the Invalid Webhook Token error and provide a better user experience.