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