Skip to content
This repository has been archived by the owner on May 5, 2024. It is now read-only.

Commit

Permalink
Timestamp object
Browse files Browse the repository at this point in the history
  • Loading branch information
pixeldeee committed Aug 31, 2023
1 parent 70e79a4 commit efd76b3
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 5 deletions.
1 change: 1 addition & 0 deletions pytecord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from .user import User, GuildMember
from .commands import Interaction
from .presence import Presence, Activity
from .timestamp import Timestamp
17 changes: 12 additions & 5 deletions pytecord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions pytecord/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
80 changes: 80 additions & 0 deletions pytecord/timestamp.py
Original file line number Diff line number Diff line change
@@ -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'<t:{self.to_unix()}>'

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()

0 comments on commit efd76b3

Please sign in to comment.