1
Fork 0
mirror of https://github.com/allthingslinux/tux.git synced 2024-10-02 16:43:12 +00:00

refactor(timezones.py): replace json file with hardcoded timezones for better performance

feat(timezones.py): add support for reactionmenu to improve user interaction
style(timezones.py): improve code readability by removing unnecessary comments and adding more descriptive variable names
This commit is contained in:
kzndotsh 2024-08-24 05:37:32 +00:00
parent 94abe651d2
commit bcf4ef1551

View file

@ -1,67 +1,139 @@
import json
from datetime import datetime
from pathlib import Path
from datetime import UTC, datetime
import discord
import pytz
from discord.ext import commands
from reactionmenu import Page, ViewButton, ViewMenu, ViewSelect
from tux.utils.embeds import EmbedCreator
timezones = {
"North America": [
("🇺🇸", "US", "Pacific/Honolulu", "HST", -10),
("🇺🇸", "US", "America/Anchorage", "AKST", -9),
("🇺🇸", "US", "America/Los_Angeles", "PST", -8),
("🇺🇸", "US", "America/Denver", "MST", -7),
("🇺🇸", "US", "America/Chicago", "CST", -6),
("🇺🇸", "US", "America/New_York", "EST", -5),
("🇲🇽", "MX", "America/Mexico_City", "CST", -6),
("🇨🇦", "CA", "America/Toronto", "EST", -5),
("🇨🇦", "CA", "America/Vancouver", "PST", -8),
],
"South America": [
("🇧🇷", "BR", "America/Sao_Paulo", "BRT", -3),
("🇦🇷", "AR", "America/Argentina/Buenos_Aires", "ART", -3),
("🇨🇱", "CL", "America/Santiago", "CLT", -3),
("🇵🇪", "PE", "America/Lima", "PET", -5),
("🇨🇴", "CO", "America/Bogota", "COT", -5),
("🇻🇪", "VE", "America/Caracas", "VET", -4),
("🇧🇴", "BO", "America/La_Paz", "BOT", -4),
("🇵🇾", "PY", "America/Asuncion", "PYT", -4),
("🇺🇾", "UY", "America/Montevideo", "UYT", -3),
],
"Africa": [
("🇬🇭", "GH", "Africa/Accra", "GMT", 0),
("🇳🇬", "NG", "Africa/Lagos", "WAT", 1),
("🇿🇦", "ZA", "Africa/Johannesburg", "SAST", 2),
("🇪🇬", "EG", "Africa/Cairo", "EET", 2),
("🇰🇪", "KE", "Africa/Nairobi", "EAT", 3),
("🇲🇦", "MA", "Africa/Casablanca", "WET", 0),
("🇹🇿", "TZ", "Africa/Dar_es_Salaam", "EAT", 3),
("🇩🇿", "DZ", "Africa/Algiers", "CET", 1),
("🇳🇦", "NA", "Africa/Windhoek", "CAT", 2),
],
"Europe": [
("🇬🇧", "GB", "Europe/London", "GMT", 0),
("🇩🇪", "DE", "Europe/Berlin", "CET", 1),
("🇫🇷", "FR", "Europe/Paris", "CET", 1),
("🇮🇹", "IT", "Europe/Rome", "CET", 1),
("🇪🇸", "ES", "Europe/Madrid", "CET", 1),
("🇳🇱", "NL", "Europe/Amsterdam", "CET", 1),
("🇧🇪", "BE", "Europe/Brussels", "CET", 1),
("🇷🇺", "RU", "Europe/Moscow", "MSK", 3),
("🇬🇷", "GR", "Europe/Athens", "EET", 2),
],
"Asia": [
("🇦🇪", "AE", "Asia/Dubai", "GST", 4),
("🇮🇳", "IN", "Asia/Kolkata", "IST", 5.5),
("🇧🇩", "BD", "Asia/Dhaka", "BST", 6),
("🇲🇲", "MM", "Asia/Yangon", "MMT", 6.5),
("🇹🇭", "TH", "Asia/Bangkok", "ICT", 7),
("🇻🇳", "VN", "Asia/Ho_Chi_Minh", "ICT", 7),
("🇨🇳", "CN", "Asia/Shanghai", "CST", 8),
("🇭🇰", "HK", "Asia/Hong_Kong", "HKT", 8),
("🇯🇵", "JP", "Asia/Tokyo", "JST", 9),
],
"Australia/Oceania": [
("🇦🇺", "AU", "Australia/Perth", "AWST", 8),
("🇦🇺", "AU", "Australia/Sydney", "AEST", 10),
("🇫🇯", "FJ", "Pacific/Fiji", "FJT", 12),
("🇳🇿", "NZ", "Pacific/Auckland", "NZDT", 13),
("🇵🇬", "PG", "Pacific/Port_Moresby", "PGT", 10),
("🇼🇸", "WS", "Pacific/Apia", "WSST", 13),
("🇸🇧", "SB", "Pacific/Guadalcanal", "SBT", 11),
("🇻🇺", "VU", "Pacific/Efate", "VUT", 11),
("🇵🇫", "PF", "Pacific/Tahiti", "THAT", -10),
],
}
continent_emojis = {
"North America": "🌎",
"South America": "🌎",
"Africa": "🌍",
"Europe": "🌍",
"Asia": "🌏",
"Australia/Oceania": "🌏",
}
class Timezones(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
def loadjson(self, json_file: str) -> dict:
"""
Opens the JSON file and returns a dictionary
@commands.hybrid_command(
name="timezones",
aliases=["tz"],
usage="timezones",
)
async def timezones(self, ctx: commands.Context[commands.Bot]) -> None:
utc_now = datetime.now(UTC)
Parameters
----------
json_file : str
The path to the json file
"""
menu = ViewMenu(ctx, menu_type=ViewMenu.TypeEmbed)
with Path.open(json_file) as file:
return json.load(file)
default_embeds: list[discord.Embed] = []
options: dict[discord.SelectOption, list[Page]] = {}
async def buildtzstring(self, json_file: str) -> str:
"""
Formats the timezone data within the timezones.json file into a string.
for continent, tz_list in timezones.items():
embeds: list[discord.Embed] = []
pages = [tz_list[i : i + 9] for i in range(0, len(tz_list), 9)]
Parameters
----------
json_file : str
The path to the json file
"""
for page in pages:
embed = discord.Embed(title=f"Timezones in {continent}", color=discord.Color.blurple())
timezone_data = self.loadjson(json_file)
for flag, _country, tz_name, abbr, utc_offset in page:
tz = pytz.timezone(tz_name)
local_time = utc_now.astimezone(tz)
time_24hr = local_time.strftime("%H:%M")
time_12hr = local_time.strftime("%I:%M %p")
formatted_lines = []
utc_now = datetime.now(pytz.utc)
for entry in timezone_data:
entry_tz = pytz.timezone(f'{entry["full_timezone"]}')
entry_time_now = utc_now.astimezone(entry_tz)
formatted_time = entry_time_now.strftime("%H:%M")
line = f'{entry["discord_emoji"]} `{entry["offset"]} {entry["timezone"]}` | **{formatted_time}**'
formatted_lines.append(line)
return "\n".join(formatted_lines)
@commands.hybrid_command(name="timezones")
async def timezones(self, ctx: commands.Context) -> None:
"""
Presents a list of the top 20 timezones in the world.
"""
embed = EmbedCreator.create_info_embed(
title="List of timezones",
description=await self.buildtzstring("./tux/utils/data/timezones.json"),
ctx=ctx,
embed.add_field(
name=f"{flag} {abbr} (UTC{utc_offset:+.2f})",
value=f"`{time_24hr} | {time_12hr}`",
inline=True,
)
await ctx.send(embed=embed)
embeds.append(embed)
default_embeds.extend(embeds)
options[discord.SelectOption(label=continent, emoji=continent_emojis[continent])] = Page.from_embeds(embeds)
for embed in default_embeds:
menu.add_page(embed)
select = ViewSelect(title="Select Continent", options=options)
menu.add_select(select)
menu.add_button(ViewButton.end_session())
await menu.start()
async def setup(bot: commands.Bot) -> None: