Browse Source

chore: Update docs and type annotations

pull/9685/head
DA-344 5 months ago
parent
commit
2a189c7469
  1. 11
      discord/enums.py
  2. 82
      discord/scheduled_event.py
  3. 36
      docs/api.rst

11
discord/enums.py

@ -78,7 +78,6 @@ __all__ = (
'SubscriptionStatus', 'SubscriptionStatus',
'MessageReferenceType', 'MessageReferenceType',
'ScheduledEventRecurrenceFrequency', 'ScheduledEventRecurrenceFrequency',
'ScheduledEventRecurrenceWeekday',
) )
@ -871,16 +870,6 @@ class ScheduledEventRecurrenceFrequency(Enum):
daily = 3 daily = 3
class ScheduledEventRecurrenceWeekday(Enum):
monday = 0
tuesday = 1
wednesday = 2
thursday = 3
friday = 4
saturday = 5
sunday = 6
def create_unknown_value(cls: Type[E], val: Any) -> E: def create_unknown_value(cls: Type[E], val: Any) -> E:
value_cls = cls._enum_value_cls_ # type: ignore # This is narrowed below value_cls = cls._enum_value_cls_ # type: ignore # This is narrowed below
name = f'unknown_{val}' name = f'unknown_{val}'

82
discord/scheduled_event.py

@ -43,7 +43,6 @@ from .enums import (
EntityType, EntityType,
PrivacyLevel, PrivacyLevel,
ScheduledEventRecurrenceFrequency, ScheduledEventRecurrenceFrequency,
ScheduledEventRecurrenceWeekday,
try_enum, try_enum,
) )
from .mixins import Hashable from .mixins import Hashable
@ -68,8 +67,8 @@ if TYPE_CHECKING:
GuildScheduledEventPayload = Union[BaseGuildScheduledEventPayload, GuildScheduledEventWithUserCountPayload] GuildScheduledEventPayload = Union[BaseGuildScheduledEventPayload, GuildScheduledEventWithUserCountPayload]
Week = Literal[1, 2, 3, 4, 5] Week = Literal[1, 2, 3, 4, 5]
NWeekday = Tuple[Week, ScheduledEventRecurrenceWeekday]
WeekDay = Literal[0, 1, 2, 3, 4, 5, 6] WeekDay = Literal[0, 1, 2, 3, 4, 5, 6]
NWeekday = Tuple[Week, WeekDay]
# fmt: off # fmt: off
__all__ = ( __all__ = (
@ -90,19 +89,30 @@ class ScheduledEventRecurrenceRule:
The frequency on which the event will recur. The frequency on which the event will recur.
interval: :class:`int` interval: :class:`int`
The spacing between events, defined by ``frequency``. The spacing between events, defined by ``frequency``.
Must be ``1`` except if ``frequency`` is :attr:`ScheduledEventRecurrenceFrequency.weekly`,
in which case it can also be ``2``.
weekdays: List[:class:`int`] weekdays: List[:class:`int`]
The days within a week the event will recur on. Must be between The days within a week the event will recur on. Must be between
0 (Monday) and 6 (Sunday). 0 (Monday) and 6 (Sunday).
n_weekdays: List[Tuple[:class:`int`, :class:`ScheduledEventRecurrenceWeekday`]]
If ``frequency`` is ``2`` this can only have 1 item.
This is mutally exclusive with ``n_weekdays`` and ``month_days``.
n_weekdays: List[Tuple[:class:`int`, :class:`int`]]
A (week, weekday) pairs list that represent the specific day within a A (week, weekday) pairs list that represent the specific day within a
specific week the event will recur on. specific week the event will recur on.
``week`` must be between 1 and 5, representing the first and last week of a month ``week`` must be between 1 and 5, representing the first and last week of a month
respectively. respectively.
``weekday`` must be a :class:`ScheduledEventRecurrenceWeekday` enum member. ``weekday`` must be an integer between 0 (Monday) and 6 (Sunday).
This is mutually exclusive with ``weekdays`` and ``month_days``.
month_days: List[:class:`datetime.date`] month_days: List[:class:`datetime.date`]
The specific days and months in which the event will recur on. The year will be ignored. The specific days and months in which the event will recur on. The year will be ignored.
This is mutually exclusive with ``weekdays`` and ``n_weekdays``.
Attributes Attributes
---------- ----------
end_date: Optional[:class:`datetime.datetime`] end_date: Optional[:class:`datetime.datetime`]
@ -110,6 +120,64 @@ class ScheduledEventRecurrenceRule:
count: Optional[:class:`int`] count: Optional[:class:`int`]
The amount of times the event will recur before stopping. Will be ``None`` The amount of times the event will recur before stopping. Will be ``None``
if :attr:`ScheduledEventRecurrenceRule.end_date` is ``None``. if :attr:`ScheduledEventRecurrenceRule.end_date` is ``None``.
Examples
--------
Creating a recurrence rule that repeats every weekday: ::
rrule = discord.ScheduledEventRecurrenceRule(
start_date=...,
frequency=discord.ScheduledEventRecurrenceFrequency.daily,
interval=1,
weekdays=[0, 1, 2, 3, 4], # from monday to friday
)
Creating a recurrence rule that repeats every Wednesday: ::
rrule = discord.ScheduledEventRecurrenceRule(
start_date=...,
frequency=discord.ScheduledEventRecurrenceFrequency.weekly,
interval=1, # interval must be 1 for the rule to be "every Wednesday"
weekdays=[2], # wednesday
)
Creating a recurrence rule that repeats every other Wednesday: ::
rrule = discord.ScheduledEventRecurrenceRule(
start_date=...,
frequency=discord.ScheduledEventRecurrenceFrequency.weekly,
interval=2, # interval CAN ONLY BE 2 in this context, and makes the rule be "every other Wednesday"
weekdays=[2],
)
Creating a recurrence rule that repeats every monthly on the fourth Wednesday: ::
rrule = discord.ScheduledEventRecurrenceRule(
start_date=...,
frequency=discord.ScheduledEventRecurrenceFrequency.monthly,
interval=1,
n_weekdays=[
(
4, # fourth week
2, # wednesday
),
],
)
Creating a recurrence rule that repeats anually on July 24: ::
rrule = discord.ScheduledEventRecurrenceRule(
start_date=...,
frequency=discord.ScheduledEventRecurrenceFrequency.yearly,
month_days=[
datetime.date(
year=1900, # use a placeholder year, it is ignored anyways
month=7, # July
day=24, # 24th
),
],
)
""" """
def __init__( def __init__(
@ -184,7 +252,7 @@ class ScheduledEventRecurrenceRule:
---------- ----------
weekdays: List[:class:`int`] weekdays: List[:class:`int`]
The weekdays the event will recur on. Must be between 0 (Monday) and 6 (Sunday). The weekdays the event will recur on. Must be between 0 (Monday) and 6 (Sunday).
n_weekdays: List[Tuple[:class:`int`, :class:`ScheduledEventRecurrenceWeekday`]] n_weekdays: List[Tuple[:class:`int`, :class:`int`]]
A (week, weekday) pairs list that the event will recur on. A (week, weekday) pairs list that the event will recur on.
month_days: List[:class:`datetime.date`] month_days: List[:class:`datetime.date`]
A list of :class:`datetime.date` objects that represent a specific day on a month A list of :class:`datetime.date` objects that represent a specific day on a month
@ -243,7 +311,7 @@ class ScheduledEventRecurrenceRule:
self._weekdays = weekdays self._weekdays = weekdays
n_weekdays = data.get('by_n_weekday', []) or [] n_weekdays = data.get('by_n_weekday', []) or []
self._n_weekdays = [(data['n'], try_enum(ScheduledEventRecurrenceWeekday, data['day'])) for data in n_weekdays] self._n_weekdays = [(data['n'], data['day']) for data in n_weekdays]
months = data.get('by_month') months = data.get('by_month')
month_days = data.get('by_month_day') month_days = data.get('by_month_day')
@ -270,7 +338,7 @@ class ScheduledEventRecurrenceRule:
if self._n_weekdays is not MISSING: if self._n_weekdays is not MISSING:
payload['by_n_weekday'] = list( payload['by_n_weekday'] = list(
map( map(
lambda nw: {'n': nw[0], 'day': nw[1].value}, lambda nw: {'n': nw[0], 'day': nw[1]},
self._n_weekdays, self._n_weekdays,
), ),
) )

36
docs/api.rst

@ -3799,42 +3799,6 @@ of :class:`enum.Enum`.
The event repeats daily. The event repeats daily.
.. class:: ScheduledEventRecurrenceWeekday
Represents a recurrence rule weekday.
.. versionadded:: 2.5
.. attribute:: monday
The ``0`` day of the week.
.. attribute:: tuesday
The ``1`` day of the week.
.. attribute:: wednesday
The ``2`` day of the week.
.. attribute:: thursday
The ``3`` day of the week.
.. attribute:: friday
The ``4`` day of the week.
.. attribute:: saturday
The ``5`` day of the week.
.. attribute:: sunday
The ``6`` day of the week.
.. class:: VoiceChannelEffectAnimationType .. class:: VoiceChannelEffectAnimationType
Represents the animation type of a voice channel effect. Represents the animation type of a voice channel effect.

Loading…
Cancel
Save