Browse Source

Reworked everything 😔

pull/9685/head
Developer Anonymous 1 year ago
parent
commit
8df1460fc4
  1. 254
      discord/scheduled_event.py

254
discord/scheduled_event.py

@ -25,7 +25,17 @@ DEALINGS IN THE SOFTWARE.
from __future__ import annotations from __future__ import annotations
from datetime import datetime from datetime import datetime
from typing import TYPE_CHECKING, AsyncIterator, Dict, Optional, Union, overload, Literal, List, Tuple from typing import (
TYPE_CHECKING,
AsyncIterator,
Dict,
Optional,
Union,
overload,
Literal,
List,
NamedTuple,
)
from .asset import Asset from .asset import Asset
from .enums import EventStatus, EntityType, PrivacyLevel, try_enum from .enums import EventStatus, EntityType, PrivacyLevel, try_enum
@ -53,223 +63,135 @@ if TYPE_CHECKING:
# fmt: off # fmt: off
__all__ = ( __all__ = (
"ScheduledEvent", "ScheduledEvent",
"ScheduledEventRecurrence",
"ScheduledEventExceptionCount"
) )
# fmt: on # fmt: on
class NWeekdays(NamedTuple):
n: int
day: int
class ScheduledEventExceptionCount:
"""Represents the exception counts in a Scheduled Event.
.. versionadded:: 2.4
.. container:: operations
.. describe:: x == y
Checks if two Exception Counts are equal.
"""
def __init__(self, data: GuildScheduledEventExceptionCountsPayload) -> None:
self.count: int = int(data.get('guild_scheduled_event_count'))
self._exception_snowflakes: Dict[Union[str, int], int] = data.get('guild_scheduled_event_exception_counts')
def __eq__(self, other: object) -> bool:
if isinstance(other, self.__class__):
return self.exception_ids == other.exception_ids
return NotImplemented
@property
def exception_ids(self) -> List[int]:
"""List[:class:`int`]: A list containing all the exception event IDs"""
return [int(id) for id in self._exception_snowflakes.keys()]
@property
def exceptions(self) -> Dict[int, int]:
"""Dict[:class:`int`, :class:`int`]: A dictionary containing all the
event IDs as keys and their respective exception counts as value.
"""
return {int(snowflake): count for snowflake, count in self._exception_snowflakes.items()} class ScheduledEventRecurrenceRule:
"""Represents a scheduled event recurrence rule.
class ScheduledEventRecurrence:
"""Represents a Scheduled Event Recurrence
.. versionadded:: 2.4 .. versionadded:: 2.4
.. container:: operations
.. describe:: x == y
Checks if two Scheduled Event Recurrences are equal
Parameters Parameters
---------- ----------
start: :class:`datetime.datetime` start: :class:`datetime.datetime`
When the first event of this series is started. An aware datetime object representing the recurrence start time.
end: Optional[:class:`datetime.datetime`] months: List[:class:`int`]
When the events of this series will stop. If it is `None`, it will repeat forever. The months this event will be repeated on.
weekday: :class:`int`
An integer representing the weekday this event will repeat in. Monday is 0
and Sunday is 6.
n_weekday: Tuple[:class:`int`, :class:`int`]
A tuple that contain the N weekday this event will repeat in.
For example, if you want for this event to repeat the 1st Monday of the month,
then this param should have a value of `(1, 0)`. Where ``1`` represents the
'first' and ``0`` the weekday, in this case, Monday.
month: :class:`int`
An integer representing the month this event will repeat in.
month_days: List[:class:`int`] month_days: List[:class:`int`]
A list of integers representing the month days this event will repeat in. The month days this event will be repeated on.
weekdays: List[:class:`int`]
This marks the days of the month this event will repeat in, for example, if it The weekdays this event will be reapeated on.
is set to `1`, this event will repeat the first day of every month.
year_days: List[:class:`int`] year_days: List[:class:`int`]
A list of integers representing the year days this event will repeat in. The year days this event will be repeated on.
n_weekdays: List[Tuple[:class:`int`, :class:`int`]]
This marks the days of the year this event will repeat in, for example, if it A ``(day, month)`` tuple that represents the N day of a month that this event
is set to `1`, this event will repeat the first day of every year. will be repeated on.
""" """
@overload @overload
def __init__( def __init__(
self, self,
start: datetime,
*, *,
weekdays: List[Literal[0, 1, 2, 3, 4, 5, 6]], start: datetime,
end: Optional[datetime] = ..., months: List[int],
month_days: List[int],
) -> None: ) -> None:
... ...
@overload @overload
def __init__( def __init__(
self, self,
start: datetime,
*, *,
n_weekday: Tuple[Literal[1, 2, 3, 4], int], start: datetime,
end: Optional[datetime] = ..., weekdays: List[int],
) -> None: ) -> None:
... ...
@overload @overload
def __init__( def __init__(
self, self,
start: datetime,
*, *,
month: Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], start: datetime,
month_days: List[int], year_days: List[int],
end: Optional[datetime] = ...,
) -> None: ) -> None:
... ...
@overload @overload
def __init__( def __init__(
self, self,
start: datetime,
*, *,
year_days: List[int], start: datetime,
end: Optional[datetime] = ..., n_weekdays: List[NWeekdays],
) -> None: ) -> None:
... ...
def __init__( def __init__(
self, self,
start: datetime,
*, *,
weekdays: List[Literal[0, 1, 2, 3, 4, 5, 6]] = MISSING, start: datetime,
n_weekday: Tuple[Literal[1, 2, 3, 4], int] = MISSING, months: List[int] = MISSING,
month: Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] = MISSING,
month_days: List[int] = MISSING, month_days: List[int] = MISSING,
weekdays: List[int] = MISSING,
year_days: List[int] = MISSING, year_days: List[int] = MISSING,
end: Optional[datetime] = MISSING, n_weekdays: List[NWeekdays] = MISSING,
) -> None: ) -> None:
if not start.tzinfo: if not start.tzinfo:
raise ValueError( raise ValueError(
'\'start\' must be an aware datetime. Consider using discord.utils.utcnow() or datetime.datetime.now().astimezone() for local time.' (
) "'start' must be an aware datetime. Consider using discord.utils.utcnow()"
" or datetime.datetime.now().astimezone() for local time."
if end not in (MISSING, None):
if not end.tzinfo:
raise ValueError(
'\'end\' must be an aware datetime. Consider using discord.utils.utcnow() or datetime.datetime.now().astimezone() for local time.'
) )
)
self.start: datetime = start self.start: datetime = start
self.end: Optional[datetime] = end if end is not MISSING else None self._months: List[int] = months
self._month_days: List[int] = month_days
self.weekdays: Optional[List[int]] = weekdays if weekdays is not MISSING else None self._weekdays: List[int] = weekdays
self.n_weekday: Optional[Tuple[int, int]] = n_weekday if n_weekday is not MISSING else None self._year_days: List[int] = year_days
self.month: Optional[int] = month if month is not MISSING else None self._n_weekdays: List[NWeekdays] = n_weekdays
self.month_days: Optional[List[int]] = month_days if month_days is not MISSING else None
self.year_days: Optional[List[int]] = year_days if year_days is not MISSING else None @property
self._interval: int = 1 def months(self) -> Optional[List[int]]:
"""Optional[List[:class:`int`]]: The list of months this event will be repeated
def __eq__(self, other: object) -> bool: on, or ``None``.
if isinstance(other, self.__class__): """
return ( return self._months if self._months is not MISSING else None
self.start == other.start
) @property
return NotImplemented def month_days(self) -> Optional[List[int]]:
"""Optional[List[:class:`int`]]: The list of month days this event will be repeated
def __set_interval(self, value: int) -> None: on, or ``None``.
# Inner function to set the interval to the one that we """
# recieved from the API return self._month_days if self._month_days is not MISSING else None
self._interval: int = value
@property @property
def frequency(self) -> int: def weekdays(self) -> Optional[List[int]]:
""":class:`int`: Returns the frequency of this recurrent scheduled event""" """Optional[List[:class:`int`]]: The list of weekdays this event will be repeated
on, or ``None``.
# This is now an internal parameter because if it is user-provided this could cause """
# HTTPExceptions when creating or editing events. return self._weekdays if self._weekdays is not MISSING else None
if self.weekdays is not None:
return 2 if len(self.weekdays) == 1 else 3
elif self.n_weekday is not None:
return 1
elif self.month is not None and self.month_days is not None:
return 0
return 0 # In case None of the cases matches (i.e.: year_days) then we return 0
@property @property
def interval(self) -> int: def year_days(self) -> Optional[List[int]]:
""":class:`int`: Returns the interval of this recurrent scheduled event""" """Optional[List[:class:`int`]]: The list of year days this event will be repeated
return self._interval on, or ``None``.
"""
def to_dict(self) -> GuildScheduledEventRecurrencePayload: return self._year_days if self._year_days is not MISSING else None
return {
"start": self.start.isoformat(), @property
"end": self.end.isoformat() if self.end else None, def n_weekdays(self) -> Optional[List[NWeekdays]]:
"by_weekday": self.weekdays or [], """Optional[List[Tuple[:class:`int`, :class:`int`]]]: The ``(day, month)`` pairs
"by_month": [self.month,] if self.month else [], this event will be repeated on, or ``None``.
"by_month_day": self.month_days or [], """
"by_n_weekday": [self.n_weekday,] if self.n_weekday else [], return self._n_weekdays if self._n_weekdays is not MISSING else None
"by_year_day": self.year_days or [],
"count": None, # There isn't counts, yet def edit(self):
"frequency": self.frequency, ... # TODO: finish this thingy
"interval": self.interval,
} # type: ignore
@classmethod
def from_dict(cls, data: GuildScheduledEventRecurrencePayload) -> ScheduledEventRecurrence:
self: cls = cls(
start=datetime.fromisoformat(data.get('start')),
weekdays=data.get('by_weekday', MISSING),
n_weekdays=((d['n'], d['day']) for d in data.get('by_n_weekday')) if data.get('by_n_weekday', MISSING) is not MISSING else MISSING,
month=data.get('by_month')[0] if len(data.get('by_month', [])) > 0 and data.get('by_month', MISSING) is not MISSING else MISSING,
month_days=data.get('by_month_day', MISSING),
year_days=data.get('by_year_day', MISSING),
end=data.get('end', MISSING)
) # type: ignore
self.__set_interval(int(data.get('interval', 1)))
return self
class ScheduledEvent(Hashable): class ScheduledEvent(Hashable):
@ -621,7 +543,7 @@ class ScheduledEvent(Hashable):
image: bytes = MISSING, image: bytes = MISSING,
location: str = MISSING, location: str = MISSING,
reason: Optional[str] = None, reason: Optional[str] = None,
recurrence: Optional[ScheduledEventRecurrence] = MISSING, recurrence_rule: Optional[ScheduledEventRecurrence] = MISSING,
) -> ScheduledEvent: ) -> ScheduledEvent:
r"""|coro| r"""|coro|
@ -783,9 +705,9 @@ class ScheduledEvent(Hashable):
else: else:
payload['scheduled_end_time'] = end_time payload['scheduled_end_time'] = end_time
if recurrence is not MISSING: if recurrence_rule is not MISSING:
if recurrence is not None: if recurrence_rule is not None:
payload['recurrence_rule'] = recurrence.to_dict() payload['recurrence_rule'] = recurrence_rule.to_dict()
else: else:
payload['recurrence_rule'] = None payload['recurrence_rule'] = None

Loading…
Cancel
Save