Files
QuizBot/modules/utils.py

67 lines
1.9 KiB
Python

from datetime import datetime
from typing import List, Dict, Any
from zoneinfo import ZoneInfo, available_timezones
from bson import ObjectId
from discord import AutocompleteContext, OptionChoice
from pymongo import ASCENDING
from modules.database import col_events, col_stages
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 autocomplete_timezones(ctx: AutocompleteContext) -> List[str]:
return sorted(list(available_timezones()))
# TODO Maybe move to a separate module
async def autocomplete_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 autocomplete_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
# TODO Maybe move to a separate module
async def autocomplete_event_stages(ctx: AutocompleteContext) -> List[OptionChoice]:
event_id: str | None = ctx.options["event"]
if event_id is None:
return []
query: Dict[str, Any] = {
"event_id": ObjectId(event_id),
}
event_stages: List[OptionChoice] = []
async for result in col_stages.find(query).sort([("sequence", ASCENDING)]):
event_stages.append(
OptionChoice(f"{result['sequence']+1} ({result['question']})", str(result["_id"]))
)
return event_stages