Fix some typing errors and reformat enums.py
This commit is contained in:
parent
828e47d83f
commit
5d798aa5e6
192
discord/enums.py
192
discord/enums.py
@ -24,7 +24,7 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
|
|
||||||
import types
|
import types
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from typing import Any, Dict, Optional, TYPE_CHECKING, Type, TypeVar
|
from typing import Any, ClassVar, Dict, List, Optional, TYPE_CHECKING, Type, TypeVar
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'Enum',
|
'Enum',
|
||||||
@ -56,16 +56,25 @@ __all__ = (
|
|||||||
'NSFWLevel',
|
'NSFWLevel',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _create_value_cls(name):
|
def _create_value_cls(name):
|
||||||
cls = namedtuple('_EnumValue_' + name, 'name value')
|
cls = namedtuple('_EnumValue_' + name, 'name value')
|
||||||
cls.__repr__ = lambda self: f'<{name}.{self.name}: {self.value!r}>'
|
cls.__repr__ = lambda self: f'<{name}.{self.name}: {self.value!r}>'
|
||||||
cls.__str__ = lambda self: f'{name}.{self.name}'
|
cls.__str__ = lambda self: f'{name}.{self.name}'
|
||||||
return cls
|
return cls
|
||||||
|
|
||||||
|
|
||||||
def _is_descriptor(obj):
|
def _is_descriptor(obj):
|
||||||
return hasattr(obj, '__get__') or hasattr(obj, '__set__') or hasattr(obj, '__delete__')
|
return hasattr(obj, '__get__') or hasattr(obj, '__set__') or hasattr(obj, '__delete__')
|
||||||
|
|
||||||
|
|
||||||
class EnumMeta(type):
|
class EnumMeta(type):
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
__name__: ClassVar[str]
|
||||||
|
_enum_member_names_: ClassVar[List[str]]
|
||||||
|
_enum_member_map_: ClassVar[Dict[str, Any]]
|
||||||
|
_enum_value_map_: ClassVar[Dict[Any, Any]]
|
||||||
|
|
||||||
def __new__(cls, name, bases, attrs):
|
def __new__(cls, name, bases, attrs):
|
||||||
value_mapping = {}
|
value_mapping = {}
|
||||||
member_mapping = {}
|
member_mapping = {}
|
||||||
@ -101,7 +110,7 @@ class EnumMeta(type):
|
|||||||
attrs['_enum_member_names_'] = member_names
|
attrs['_enum_member_names_'] = member_names
|
||||||
attrs['_enum_value_cls_'] = value_cls
|
attrs['_enum_value_cls_'] = value_cls
|
||||||
actual_cls = super().__new__(cls, name, bases, attrs)
|
actual_cls = super().__new__(cls, name, bases, attrs)
|
||||||
value_cls._actual_enum_cls_ = actual_cls
|
value_cls._actual_enum_cls_ = actual_cls # type: ignore
|
||||||
return actual_cls
|
return actual_cls
|
||||||
|
|
||||||
def __iter__(cls):
|
def __iter__(cls):
|
||||||
@ -143,9 +152,11 @@ class EnumMeta(type):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
else:
|
else:
|
||||||
|
|
||||||
class Enum(metaclass=EnumMeta):
|
class Enum(metaclass=EnumMeta):
|
||||||
@classmethod
|
@classmethod
|
||||||
def try_value(cls, value):
|
def try_value(cls, value):
|
||||||
@ -154,80 +165,84 @@ else:
|
|||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
class ChannelType(Enum):
|
class ChannelType(Enum):
|
||||||
text = 0
|
text = 0
|
||||||
private = 1
|
private = 1
|
||||||
voice = 2
|
voice = 2
|
||||||
group = 3
|
group = 3
|
||||||
category = 4
|
category = 4
|
||||||
news = 5
|
news = 5
|
||||||
store = 6
|
store = 6
|
||||||
news_thread = 10
|
news_thread = 10
|
||||||
public_thread = 11
|
public_thread = 11
|
||||||
private_thread = 12
|
private_thread = 12
|
||||||
stage_voice = 13
|
stage_voice = 13
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class MessageType(Enum):
|
class MessageType(Enum):
|
||||||
default = 0
|
default = 0
|
||||||
recipient_add = 1
|
recipient_add = 1
|
||||||
recipient_remove = 2
|
recipient_remove = 2
|
||||||
call = 3
|
call = 3
|
||||||
channel_name_change = 4
|
channel_name_change = 4
|
||||||
channel_icon_change = 5
|
channel_icon_change = 5
|
||||||
pins_add = 6
|
pins_add = 6
|
||||||
new_member = 7
|
new_member = 7
|
||||||
premium_guild_subscription = 8
|
premium_guild_subscription = 8
|
||||||
premium_guild_tier_1 = 9
|
premium_guild_tier_1 = 9
|
||||||
premium_guild_tier_2 = 10
|
premium_guild_tier_2 = 10
|
||||||
premium_guild_tier_3 = 11
|
premium_guild_tier_3 = 11
|
||||||
channel_follow_add = 12
|
channel_follow_add = 12
|
||||||
guild_stream = 13
|
guild_stream = 13
|
||||||
guild_discovery_disqualified = 14
|
guild_discovery_disqualified = 14
|
||||||
guild_discovery_requalified = 15
|
guild_discovery_requalified = 15
|
||||||
guild_discovery_grace_period_initial_warning = 16
|
guild_discovery_grace_period_initial_warning = 16
|
||||||
guild_discovery_grace_period_final_warning = 17
|
guild_discovery_grace_period_final_warning = 17
|
||||||
thread_created = 18
|
thread_created = 18
|
||||||
reply = 19
|
reply = 19
|
||||||
application_command = 20
|
application_command = 20
|
||||||
thread_starter_message = 21
|
thread_starter_message = 21
|
||||||
guild_invite_reminder = 22
|
guild_invite_reminder = 22
|
||||||
|
|
||||||
|
|
||||||
class VoiceRegion(Enum):
|
class VoiceRegion(Enum):
|
||||||
us_west = 'us-west'
|
us_west = 'us-west'
|
||||||
us_east = 'us-east'
|
us_east = 'us-east'
|
||||||
us_south = 'us-south'
|
us_south = 'us-south'
|
||||||
us_central = 'us-central'
|
us_central = 'us-central'
|
||||||
eu_west = 'eu-west'
|
eu_west = 'eu-west'
|
||||||
eu_central = 'eu-central'
|
eu_central = 'eu-central'
|
||||||
singapore = 'singapore'
|
singapore = 'singapore'
|
||||||
london = 'london'
|
london = 'london'
|
||||||
sydney = 'sydney'
|
sydney = 'sydney'
|
||||||
amsterdam = 'amsterdam'
|
amsterdam = 'amsterdam'
|
||||||
frankfurt = 'frankfurt'
|
frankfurt = 'frankfurt'
|
||||||
brazil = 'brazil'
|
brazil = 'brazil'
|
||||||
hongkong = 'hongkong'
|
hongkong = 'hongkong'
|
||||||
russia = 'russia'
|
russia = 'russia'
|
||||||
japan = 'japan'
|
japan = 'japan'
|
||||||
southafrica = 'southafrica'
|
southafrica = 'southafrica'
|
||||||
south_korea = 'south-korea'
|
south_korea = 'south-korea'
|
||||||
india = 'india'
|
india = 'india'
|
||||||
europe = 'europe'
|
europe = 'europe'
|
||||||
dubai = 'dubai'
|
dubai = 'dubai'
|
||||||
vip_us_east = 'vip-us-east'
|
vip_us_east = 'vip-us-east'
|
||||||
vip_us_west = 'vip-us-west'
|
vip_us_west = 'vip-us-west'
|
||||||
vip_amsterdam = 'vip-amsterdam'
|
vip_amsterdam = 'vip-amsterdam'
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
|
|
||||||
class SpeakingState(Enum):
|
class SpeakingState(Enum):
|
||||||
none = 0
|
none = 0
|
||||||
voice = 1
|
voice = 1
|
||||||
soundshare = 2
|
soundshare = 2
|
||||||
priority = 4
|
priority = 4
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
@ -235,24 +250,27 @@ class SpeakingState(Enum):
|
|||||||
def __int__(self):
|
def __int__(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
|
|
||||||
class VerificationLevel(Enum):
|
class VerificationLevel(Enum):
|
||||||
none = 0
|
none = 0
|
||||||
low = 1
|
low = 1
|
||||||
medium = 2
|
medium = 2
|
||||||
high = 3
|
high = 3
|
||||||
highest = 4
|
highest = 4
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class ContentFilter(Enum):
|
class ContentFilter(Enum):
|
||||||
disabled = 0
|
disabled = 0
|
||||||
no_role = 1
|
no_role = 1
|
||||||
all_members = 2
|
all_members = 2
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
online = 'online'
|
online = 'online'
|
||||||
offline = 'offline'
|
offline = 'offline'
|
||||||
@ -264,27 +282,32 @@ class Status(Enum):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
|
|
||||||
class DefaultAvatar(Enum):
|
class DefaultAvatar(Enum):
|
||||||
blurple = 0
|
blurple = 0
|
||||||
grey = 1
|
grey = 1
|
||||||
gray = 1
|
gray = 1
|
||||||
green = 2
|
green = 2
|
||||||
orange = 3
|
orange = 3
|
||||||
red = 4
|
red = 4
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class NotificationLevel(Enum):
|
class NotificationLevel(Enum):
|
||||||
all_messages = 0
|
all_messages = 0
|
||||||
only_mentions = 1
|
only_mentions = 1
|
||||||
|
|
||||||
|
|
||||||
class AuditLogActionCategory(Enum):
|
class AuditLogActionCategory(Enum):
|
||||||
create = 1
|
create = 1
|
||||||
delete = 2
|
delete = 2
|
||||||
update = 3
|
update = 3
|
||||||
|
|
||||||
|
|
||||||
class AuditLogAction(Enum):
|
class AuditLogAction(Enum):
|
||||||
|
# fmt: off
|
||||||
guild_update = 1
|
guild_update = 1
|
||||||
channel_create = 10
|
channel_create = 10
|
||||||
channel_update = 11
|
channel_update = 11
|
||||||
@ -323,9 +346,11 @@ class AuditLogAction(Enum):
|
|||||||
stage_instance_create = 83
|
stage_instance_create = 83
|
||||||
stage_instance_update = 84
|
stage_instance_update = 84
|
||||||
stage_instance_delete = 85
|
stage_instance_delete = 85
|
||||||
|
# fmt: on
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def category(self) -> Optional[AuditLogActionCategory]:
|
def category(self) -> Optional[AuditLogActionCategory]:
|
||||||
|
# fmt: off
|
||||||
lookup: Dict[AuditLogAction, Optional[AuditLogActionCategory]] = {
|
lookup: Dict[AuditLogAction, Optional[AuditLogActionCategory]] = {
|
||||||
AuditLogAction.guild_update: AuditLogActionCategory.update,
|
AuditLogAction.guild_update: AuditLogActionCategory.update,
|
||||||
AuditLogAction.channel_create: AuditLogActionCategory.create,
|
AuditLogAction.channel_create: AuditLogActionCategory.create,
|
||||||
@ -366,6 +391,7 @@ class AuditLogAction(Enum):
|
|||||||
AuditLogAction.stage_instance_update: AuditLogActionCategory.update,
|
AuditLogAction.stage_instance_update: AuditLogActionCategory.update,
|
||||||
AuditLogAction.stage_instance_delete: AuditLogActionCategory.delete,
|
AuditLogAction.stage_instance_delete: AuditLogActionCategory.delete,
|
||||||
}
|
}
|
||||||
|
# fmt: on
|
||||||
return lookup[self]
|
return lookup[self]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -396,6 +422,7 @@ class AuditLogAction(Enum):
|
|||||||
elif v < 90:
|
elif v < 90:
|
||||||
return 'stage_instance'
|
return 'stage_instance'
|
||||||
|
|
||||||
|
|
||||||
class UserFlags(Enum):
|
class UserFlags(Enum):
|
||||||
staff = 1
|
staff = 1
|
||||||
partner = 2
|
partner = 2
|
||||||
@ -415,6 +442,7 @@ class UserFlags(Enum):
|
|||||||
verified_bot_developer = 131072
|
verified_bot_developer = 131072
|
||||||
discord_certified_moderator = 262144
|
discord_certified_moderator = 262144
|
||||||
|
|
||||||
|
|
||||||
class ActivityType(Enum):
|
class ActivityType(Enum):
|
||||||
unknown = -1
|
unknown = -1
|
||||||
playing = 0
|
playing = 0
|
||||||
@ -427,36 +455,44 @@ class ActivityType(Enum):
|
|||||||
def __int__(self):
|
def __int__(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
|
|
||||||
class TeamMembershipState(Enum):
|
class TeamMembershipState(Enum):
|
||||||
invited = 1
|
invited = 1
|
||||||
accepted = 2
|
accepted = 2
|
||||||
|
|
||||||
|
|
||||||
class WebhookType(Enum):
|
class WebhookType(Enum):
|
||||||
incoming = 1
|
incoming = 1
|
||||||
channel_follower = 2
|
channel_follower = 2
|
||||||
application = 3
|
application = 3
|
||||||
|
|
||||||
|
|
||||||
class ExpireBehaviour(Enum):
|
class ExpireBehaviour(Enum):
|
||||||
remove_role = 0
|
remove_role = 0
|
||||||
kick = 1
|
kick = 1
|
||||||
|
|
||||||
|
|
||||||
ExpireBehavior = ExpireBehaviour
|
ExpireBehavior = ExpireBehaviour
|
||||||
|
|
||||||
|
|
||||||
class StickerType(Enum):
|
class StickerType(Enum):
|
||||||
png = 1
|
png = 1
|
||||||
apng = 2
|
apng = 2
|
||||||
lottie = 3
|
lottie = 3
|
||||||
|
|
||||||
|
|
||||||
class InviteTarget(Enum):
|
class InviteTarget(Enum):
|
||||||
unknown = 0
|
unknown = 0
|
||||||
stream = 1
|
stream = 1
|
||||||
embedded_application = 2
|
embedded_application = 2
|
||||||
|
|
||||||
|
|
||||||
class InteractionType(Enum):
|
class InteractionType(Enum):
|
||||||
ping = 1
|
ping = 1
|
||||||
application_command = 2
|
application_command = 2
|
||||||
component = 3
|
component = 3
|
||||||
|
|
||||||
|
|
||||||
class InteractionResponseType(Enum):
|
class InteractionResponseType(Enum):
|
||||||
pong = 1
|
pong = 1
|
||||||
# ack = 2 (deprecated)
|
# ack = 2 (deprecated)
|
||||||
@ -464,7 +500,8 @@ class InteractionResponseType(Enum):
|
|||||||
channel_message = 4 # (with source)
|
channel_message = 4 # (with source)
|
||||||
deferred_channel_message = 5 # (with source)
|
deferred_channel_message = 5 # (with source)
|
||||||
deferred_message_update = 6 # for components
|
deferred_message_update = 6 # for components
|
||||||
message_update = 7 # for components
|
message_update = 7 # for components
|
||||||
|
|
||||||
|
|
||||||
class VideoQualityMode(Enum):
|
class VideoQualityMode(Enum):
|
||||||
auto = 1
|
auto = 1
|
||||||
@ -473,6 +510,7 @@ class VideoQualityMode(Enum):
|
|||||||
def __int__(self):
|
def __int__(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
|
|
||||||
class ComponentType(Enum):
|
class ComponentType(Enum):
|
||||||
action_row = 1
|
action_row = 1
|
||||||
button = 2
|
button = 2
|
||||||
@ -481,6 +519,7 @@ class ComponentType(Enum):
|
|||||||
def __int__(self):
|
def __int__(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
|
|
||||||
class ButtonStyle(Enum):
|
class ButtonStyle(Enum):
|
||||||
primary = 1
|
primary = 1
|
||||||
secondary = 2
|
secondary = 2
|
||||||
@ -499,24 +538,29 @@ class ButtonStyle(Enum):
|
|||||||
def __int__(self):
|
def __int__(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
|
|
||||||
class StagePrivacyLevel(Enum):
|
class StagePrivacyLevel(Enum):
|
||||||
public = 1
|
public = 1
|
||||||
closed = 2
|
closed = 2
|
||||||
guild_only = 2
|
guild_only = 2
|
||||||
|
|
||||||
|
|
||||||
class NSFWLevel(Enum):
|
class NSFWLevel(Enum):
|
||||||
default = 0
|
default = 0
|
||||||
explicit = 1
|
explicit = 1
|
||||||
safe = 2
|
safe = 2
|
||||||
age_restricted = 3
|
age_restricted = 3
|
||||||
|
|
||||||
|
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
|
|
||||||
|
|
||||||
def create_unknown_value(cls: Type[T], val: Any) -> T:
|
def create_unknown_value(cls: Type[T], val: Any) -> T:
|
||||||
value_cls = cls._enum_value_cls_ # type: ignore
|
value_cls = cls._enum_value_cls_ # type: ignore
|
||||||
name = f'unknown_{val}'
|
name = f'unknown_{val}'
|
||||||
return value_cls(name=name, value=val)
|
return value_cls(name=name, value=val)
|
||||||
|
|
||||||
|
|
||||||
def try_enum(cls: Type[T], val: Any) -> T:
|
def try_enum(cls: Type[T], val: Any) -> T:
|
||||||
"""A function that tries to turn the value into enum ``cls``.
|
"""A function that tries to turn the value into enum ``cls``.
|
||||||
|
|
||||||
@ -524,6 +568,6 @@ def try_enum(cls: Type[T], val: Any) -> T:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return cls._enum_value_map_[val] # type: ignore
|
return cls._enum_value_map_[val] # type: ignore
|
||||||
except (KeyError, TypeError, AttributeError):
|
except (KeyError, TypeError, AttributeError):
|
||||||
return create_unknown_value(cls, val)
|
return create_unknown_value(cls, val)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user