From efd76b3ddb992778c44710cd512622e6dd9109e8 Mon Sep 17 00:00:00 2001 From: pixeldeee Date: Fri, 1 Sep 2023 00:07:23 +0300 Subject: [PATCH] Timestamp object --- pytecord/__init__.py | 1 + pytecord/client.py | 17 ++++++--- pytecord/timer.py | 16 +++++++++ pytecord/timestamp.py | 80 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 pytecord/timestamp.py diff --git a/pytecord/__init__.py b/pytecord/__init__.py index 534a4fd..f52ce85 100644 --- a/pytecord/__init__.py +++ b/pytecord/__init__.py @@ -5,3 +5,4 @@ from .user import User, GuildMember from .commands import Interaction from .presence import Presence, Activity +from .timestamp import Timestamp diff --git a/pytecord/client.py b/pytecord/client.py index 3af9826..aa6531f 100644 --- a/pytecord/client.py +++ b/pytecord/client.py @@ -9,7 +9,7 @@ from .utils import get_option_type, rget from .web import BaseWebhook from .presence import Presence -from .timer import TimerLoop +from .timer import TimerLoop, At if TYPE_CHECKING: from .web import GatewayOutput @@ -18,7 +18,8 @@ class Client: def __init__(self, token: str, debug: bool = False) -> None: self.webhook = BaseWebhook(token, debug) self.token = token - self.timers: list[TimerLoop] = [] + self.__timers: list[TimerLoop] = [] + self.__ats: list[At] = [] self.__intents = GatewayIntents.GUILD_INTEGRATIONS self.__presence = None @@ -50,7 +51,7 @@ def decorator(func_to_decorate: Callable[..., Coroutine[Any, Any, Any]]): match event_name: case 'ready': async def func(data: 'GatewayOutput'): - for i in self.timers: + for i in self.__timers: i.run() await self.webhook.register_app_commands(data) await func_to_decorate() @@ -80,7 +81,13 @@ async def func(data: 'GatewayOutput'): def timer(self, *, days: int = 0, hours: int = 0, minutes: int = 0, seconds: int = 0): def wrapper(func_to_decorate: Callable[..., Coroutine[Any, Any, Any]]): timer = TimerLoop(func_to_decorate, days, hours, minutes, seconds) - self.timers.append(timer) + self.__timers.append(timer) + return wrapper + + def at(self, hours: int = 0, minutes: int = 0): + def wrapper(func_to_decorate: Callable[..., Coroutine[Any, Any, Any]]): + at = At(func_to_decorate, hours, minutes) + self.__ats.append(at) return wrapper def command(self): @@ -110,7 +117,7 @@ def get_channel(self, id: int) -> GuildChannel: def run(self): if not self.webhook.listener.events.get('READY'): async def func(data: 'GatewayOutput'): - for i in self.timers: + for i in self.__timers: i.run() await self.webhook.register_app_commands(data) self.webhook.add_event('READY', func) diff --git a/pytecord/timer.py b/pytecord/timer.py index 23a3c15..d87c11d 100644 --- a/pytecord/timer.py +++ b/pytecord/timer.py @@ -23,6 +23,22 @@ async def __thread(self): await self.callable() + def run(self): + create_task(self.__thread()) + + +class At: + def __init__(self, callable: Callable, hours: int = 0, minutes: int = 0) -> None: + self.callable = callable + self.time = hours, minutes + + async def __thread(self): + while True: + date_and_time = datetime.now() + current_time = date_and_time.hour, date_and_time.minute + + if current_time == self.time: + await self.callable() def run(self): create_task(self.__thread()) diff --git a/pytecord/timestamp.py b/pytecord/timestamp.py new file mode 100644 index 0000000..9e42259 --- /dev/null +++ b/pytecord/timestamp.py @@ -0,0 +1,80 @@ +from typing import Literal + +from datetime import datetime +from time import mktime + +class Timestamp: + @staticmethod + def from_iso(timestamp: str) -> 'Timestamp': + return Timestamp(datetime.fromisoformat(timestamp)) + + @staticmethod + def from_unix(unix_time: int) -> 'Timestamp': + return Timestamp(datetime.fromtimestamp(unix_time)) + + def __init__(self, timestamp: datetime = datetime.now()) -> None: + self.__timestamp = timestamp + + def get(self) -> datetime: + return self.__timestamp + + @property + def second(self) -> int: + return self.__timestamp.second + + @property + def minute(self) -> int: + return self.__timestamp.minute + + @property + def hour(self) -> int: + return self.__timestamp.hour + + @property + def day(self) -> int: + return self.__timestamp.day + + @property + def month(self) -> int: + return self.__timestamp.month + + @property + def year(self) -> int: + return self.__timestamp.year + + def to_unix(self) -> int: + return int(mktime(self.__timestamp.timetuple())) + + def to_iso(self) -> str: + return self.__timestamp.isoformat() + + def to_discord(self) -> str: + return f'' + + def to_tuple(self) -> tuple[int, ...]: + return (self.year, self.month, self.day, self.hour, self.minute, self.second) + + def to_list(self) -> list[int]: + return list(self.to_tuple()) + + def to_dict(self) -> dict[Literal['year', 'month', 'day', 'hour', 'minute', 'second'], int]: + return { + 'year': self.year, + 'month': self.month, + 'day': self.day, + 'hour': self.hour, + 'minute': self.minute, + 'second': self.second + } + + def __str__(self) -> str: + return self.to_discord() + + def __int__(self) -> int: + return self.to_unix() + + def __iter__(self): + return self.to_tuple().__iter__() + + def __repr__(self) -> str: + return self.to_discord()