Simplify the whole operation and use discord's neat built ins for relative times

This commit is contained in:
Luke Robles 2024-10-17 19:31:59 -07:00
parent 59aa996895
commit 4618f722a8

View File

@ -1,5 +1,4 @@
from datetime import datetime from datetime import datetime
from datetime import timezone
from discord.ext import commands from discord.ext import commands
import core_utils import core_utils
import discord import discord
@ -15,13 +14,6 @@ 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
@ -43,7 +35,6 @@ class Tarkov(commands.Cog):
embed.set_author(name="PVE 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")
query = """ query = """
{ {
traders(gameMode:pve) { traders(gameMode:pve) {
@ -58,29 +49,21 @@ class Tarkov(commands.Cog):
"https://api.tarkov.dev/graphql", headers=headers, json={"query": query} "https://api.tarkov.dev/graphql", headers=headers, json={"query": query}
).json()["data"]["traders"] ).json()["data"]["traders"]
# print(response)
current_time = datetime.now(timezone.utc)
# Loop through the dataset # Loop through the dataset
for entry in response: for entry in response:
name = entry["name"] trader_name = entry["name"]
if name in ["BTR Driver", "Lightkeeper"]: if trader_name in ["BTR Driver", "Lightkeeper"]:
break break
# Convert to datetime object
reset_time = datetime.fromisoformat( reset_time = datetime.fromisoformat(
entry["resetTime"].replace("Z", "+00:00") 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 # Print the name and formatted time until reset
embed.add_field( embed.add_field(
name=name, name=trader_name,
value=f"`{readable_time}`", value=discord.utils.format_dt(reset_time, style="R"),
inline=True, inline=True,
) )
await ctx.send_followup(embed=embed) await ctx.send_followup(embed=embed)