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
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):
def __init__(self, bot):
self.bot: commands.Bot = bot
@ -35,30 +42,49 @@ class Tarkov(commands.Cog):
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")
times = requests.get(
"https://tarkovbot.eu/tools/pve/trader-resets/gettimes"
).json()
import requests
from datetime import datetime, timezone
for time in times:
target_time = datetime.fromisoformat(
time["resetTime"].replace("000Z", "+00:00")
)
query = """
{
traders(gameMode:pve) {
name
resetTime
}
}
"""
# Get the current time in UTC
headers = {"Content-Type": "application/json"}
response = requests.post(
"https://api.tarkov.dev/graphql", headers=headers, json={"query": query}
).json()["data"]["traders"]
# print(response)
current_time = datetime.now(timezone.utc)
# Calculate the difference
time_difference = target_time - current_time
# Loop through the dataset
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
# Convert the difference to minutes
minutes_until = time_difference.total_seconds() / 60
# 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(
name=time["name"],
value=f"{minutes_until:.2f} minutes",
name=name,
value=f"`{readable_time}`",
inline=True,
)
await ctx.send_followup(embed=embed)