25
25
"""
26
26
from __future__ import annotations
27
27
28
- import re
29
- from .asset import Asset
30
- from . import utils
31
-
32
28
from typing import (
33
29
Optional ,
34
30
TYPE_CHECKING
35
31
)
32
+ from typing_extensions import Literal
33
+
34
+ import re
35
+ from .asset import Asset
36
+ from . import utils
36
37
37
38
if TYPE_CHECKING :
39
+ from .state import ConnectionState
38
40
from datetime import datetime
41
+ from .types .emoji import PartialEmoji as PartialEmojiPayload
39
42
40
43
41
44
class _EmojiTag :
@@ -82,14 +85,14 @@ class PartialEmoji(_EmojiTag):
82
85
83
86
__slots__ = ('animated' , 'name' , 'id' , '_state' )
84
87
85
- def __init__ (self , * , name , animated = False , id = None ):
86
- self .animated = animated
87
- self .name = name
88
- self .id = id
89
- self ._state = None
88
+ def __init__ (self , * , name : str , animated : bool = False , id : Optional [ int ] = None ) -> None :
89
+ self .animated : bool = animated
90
+ self .name : str = name
91
+ self .id : Optional [ int ] = id
92
+ self ._state : Optional [ ConnectionState ] = None
90
93
91
94
@classmethod
92
- def from_string (cls , string : str ):
95
+ def from_string (cls , string : str ) -> PartialEmoji :
93
96
"""Converts a (custom) emoji as they are in a message into a partial emoji object.
94
97
95
98
.. note::
@@ -98,6 +101,11 @@ def from_string(cls, string: str):
98
101
99
102
.. warning::
100
103
This does **not** support the :emoji: format for (standard) emojis
104
+
105
+ Returns
106
+ --------
107
+ :class:`PartialEmoji`
108
+ The emoji object if the string was valid.
101
109
"""
102
110
103
111
match = re .match ('^<(a?):([\-\w]+):(\d+)>$' , string )
@@ -106,14 +114,14 @@ def from_string(cls, string: str):
106
114
return cls (name = string )
107
115
108
116
@classmethod
109
- def from_dict (cls , data ) :
117
+ def from_dict (cls , data : PartialEmojiPayload ) -> PartialEmoji :
110
118
return cls (
111
119
animated = data .get ('animated' , False ),
112
120
id = utils ._get_as_snowflake (data , 'id' ),
113
121
name = data .get ('name' ),
114
122
)
115
123
116
- def to_dict (self ):
124
+ def to_dict (self ) -> PartialEmojiPayload :
117
125
o = {'name' : self .name }
118
126
if self .id :
119
127
o ['id' ] = self .id
@@ -122,36 +130,43 @@ def to_dict(self):
122
130
return o
123
131
124
132
@classmethod
125
- def with_state (cls , state , * , name , animated = False , id = None ):
133
+ def with_state (
134
+ cls ,
135
+ state : ConnectionState ,
136
+ * ,
137
+ name : str ,
138
+ animated : bool = False ,
139
+ id : Optional [int ] = None
140
+ ) -> PartialEmoji :
126
141
self = cls (name = name , animated = animated , id = id )
127
142
self ._state = state
128
143
return self
129
144
130
- def __str__ (self ):
145
+ def __str__ (self ) -> str :
131
146
if self .id is None :
132
147
return self .name
133
148
if self .animated :
134
149
return '<a:%s:%s>' % (self .name , self .id )
135
150
return '<:%s:%s>' % (self .name , self .id )
136
151
137
- def __len__ (self ):
152
+ def __len__ (self ) -> int :
138
153
return len (str (self ))
139
154
140
- def __repr__ (self ):
155
+ def __repr__ (self ) -> str :
141
156
return '<{0.__class__.__name__} animated={0.animated} name={0.name!r} id={0.id}>' .format (self )
142
157
143
158
def __eq__ (self , other ) -> bool :
144
159
if self .is_unicode_emoji ():
145
160
return isinstance (other , PartialEmoji ) and self .name == other .name
146
161
147
162
if isinstance (other , _EmojiTag ):
148
- return self .id == other .id
163
+ return self .id == other .id # type: ignore
149
164
return False
150
-
165
+
151
166
def __ne__ (self , other ) -> bool :
152
167
return not self .__eq__ (other )
153
168
154
- def __hash__ (self ):
169
+ def __hash__ (self ) -> int :
155
170
return hash ((self .id , self .name ))
156
171
157
172
def is_custom_emoji (self ) -> bool :
@@ -187,7 +202,12 @@ def url(self) -> Asset:
187
202
"""
188
203
return self .url_as (format = None )
189
204
190
- def url_as (self , * , format : Optional [str ] = None , static_format = "png" ) -> Asset :
205
+ def url_as (
206
+ self ,
207
+ * ,
208
+ format : Optional [Literal ['jpeg' , 'jpg' , 'webp' , 'png' , 'gif' ]] = None ,
209
+ static_format : Literal ['jpeg' , 'jpg' , 'webp' , 'png' , ] = 'png'
210
+ ) -> Asset :
191
211
"""Returns an :class:`Asset` for the emoji's url, if it is custom.
192
212
193
213
The format must be one of 'webp', 'jpeg', 'jpg', 'png' or 'gif'.
0 commit comments