Making the timestamp handling a litte better for trader resets

This commit is contained in:
Luke Robles 2024-10-17 19:17:39 -07:00
parent d30bcb85ab
commit 9d8fe1c1e3

View File

@ -12,6 +12,13 @@ else:
channel_id = 932476007439552522 channel_id = 932476007439552522
def format_timedelta(td):
total_seconds = int(td.total_seconds())
hours, remainder = divmod(total_seconds, 3600) # 3600 seconds in an hour
minutes, seconds = divmod(remainder, 60) # 60 seconds in a minute
return f"{hours}h {minutes}m {seconds}s"
class Tarkov(commands.Cog): class Tarkov(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot: commands.Bot = bot self.bot: commands.Bot = bot
@ -35,30 +42,49 @@ class Tarkov(commands.Cog):
description="-------", color=discord.Color.blue(), type="rich" description="-------", color=discord.Color.blue(), type="rich"
) )
embed.set_author(name="Trader Reset Times") embed.set_author(name="PVE Trader Reset Times")
embed.set_thumbnail(url="https://i.ytimg.com/vi/8He5q7qOzNw/maxresdefault.jpg") embed.set_thumbnail(url="https://i.ytimg.com/vi/8He5q7qOzNw/maxresdefault.jpg")
times = requests.get( import requests
"https://tarkovbot.eu/tools/pve/trader-resets/gettimes" from datetime import datetime, timezone
).json()
for time in times: query = """
target_time = datetime.fromisoformat( {
time["resetTime"].replace("000Z", "+00:00") traders(gameMode:pve) {
) name
resetTime
}
}
"""
# Get the current time in UTC headers = {"Content-Type": "application/json"}
current_time = datetime.now(timezone.utc) response = requests.post(
"https://api.tarkov.dev/graphql", headers=headers, json={"query": query}
).json()["data"]["traders"]
# Calculate the difference # print(response)
time_difference = target_time - current_time current_time = datetime.now(timezone.utc)
# Convert the difference to minutes # Loop through the dataset
minutes_until = time_difference.total_seconds() / 60 for entry in response:
name = entry["name"]
if name in ["BTR Driver", "Lightkeeper"]:
break
reset_time = datetime.fromisoformat(
entry["resetTime"].replace("Z", "+00:00")
) # Convert to datetime object
time_until = reset_time - current_time # Calculate time until the reset
# Format the time until as human-readable
if time_until.total_seconds() > 0: # Only format if there's time remaining
readable_time = format_timedelta(time_until)
else:
readable_time = "Just Reset"
# Print the name and formatted time until reset
embed.add_field( embed.add_field(
name=time["name"], name=name,
value=f"{minutes_until:.2f} minutes", value=f"`{readable_time}`",
inline=True, inline=True,
) )
await ctx.send_followup(embed=embed) await ctx.send_followup(embed=embed)