Files
QuizBot/modules/utils.py

44 lines
1.3 KiB
Python

from datetime import datetime
from typing import List, Dict, Any
from zoneinfo import ZoneInfo, available_timezones
from discord import AutocompleteContext, OptionChoice
from modules.database import col_events
def hex_to_int(hex_color: str) -> int:
return int(hex_color.lstrip("#"), 16)
def int_to_hex(integer_color: int) -> str:
return "#" + format(integer_color, "06x")
# TODO Maybe move to a separate module
async def autofill_timezones(ctx: AutocompleteContext) -> List[str]:
return sorted(list(available_timezones()))
# TODO Maybe move to a separate module
async def autofill_languages(ctx: AutocompleteContext) -> List[str]:
# TODO Discord normally uses a different set of locales.
# For example, "en" being "en-US", etc. This will require changes to locale handling later.
return ctx.bot.locales.keys()
# TODO Maybe move to a separate module
async def autofill_active_events(ctx: AutocompleteContext) -> List[OptionChoice]:
query: Dict[str, Any] = {
"ended": None,
"ends": {"$gt": datetime.now(tz=ZoneInfo("UTC"))},
"cancelled": {"$ne": True},
}
event_names: List[OptionChoice] = []
async for result in col_events.find(query):
event_names.append(OptionChoice(result["name"], str(result["_id"])))
return event_names