@@ -99,12 +99,13 @@ def __attrs_post_init__(self) -> None:
99
99
100
100
# region User cache
101
101
102
- async def fetch_user (self , user_id : "Snowflake_Type" ) -> User :
102
+ async def fetch_user (self , user_id : "Snowflake_Type" , * , force : bool = False ) -> User :
103
103
"""
104
104
Fetch a user by their ID.
105
105
106
106
Args:
107
107
user_id: The user's ID
108
+ force: If the cache should be ignored, and the user should be fetched from the API
108
109
109
110
Returns:
110
111
User object if found
@@ -113,9 +114,10 @@ async def fetch_user(self, user_id: "Snowflake_Type") -> User:
113
114
user_id = to_snowflake (user_id )
114
115
115
116
user = self .user_cache .get (user_id )
116
- if user is None :
117
+ if ( user is None or user . _fetched is False ) or force :
117
118
data = await self ._client .http .get_user (user_id )
118
119
user = self .place_user_data (data )
120
+ user ._fetched = True # the user object should set this to True, but we do it here just in case
119
121
return user
120
122
121
123
def get_user (self , user_id : Optional ["Snowflake_Type" ]) -> Optional [User ]:
@@ -164,13 +166,16 @@ def delete_user(self, user_id: "Snowflake_Type") -> None:
164
166
165
167
# region Member cache
166
168
167
- async def fetch_member (self , guild_id : "Snowflake_Type" , user_id : "Snowflake_Type" ) -> Member :
169
+ async def fetch_member (
170
+ self , guild_id : "Snowflake_Type" , user_id : "Snowflake_Type" , * , force : bool = False
171
+ ) -> Member :
168
172
"""
169
173
Fetch a member by their guild and user IDs.
170
174
171
175
Args:
172
176
guild_id: The ID of the guild this user belongs to
173
177
user_id: The ID of the user
178
+ force: If the cache should be ignored, and the member should be fetched from the API
174
179
175
180
Returns:
176
181
Member object if found
@@ -179,7 +184,7 @@ async def fetch_member(self, guild_id: "Snowflake_Type", user_id: "Snowflake_Typ
179
184
guild_id = to_snowflake (guild_id )
180
185
user_id = to_snowflake (user_id )
181
186
member = self .member_cache .get ((guild_id , user_id ))
182
- if member is None :
187
+ if member is None or force :
183
188
data = await self ._client .http .get_member (guild_id , user_id )
184
189
member = self .place_member_data (guild_id , data )
185
190
return member
@@ -323,15 +328,13 @@ async def is_user_in_guild(
323
328
324
329
return False
325
330
326
- async def fetch_user_guild_ids (
327
- self ,
328
- user_id : "Snowflake_Type" ,
329
- ) -> List ["Snowflake_Type" ]:
331
+ async def fetch_user_guild_ids (self , user_id : "Snowflake_Type" ) -> List ["Snowflake_Type" ]:
330
332
"""
331
333
Fetch a list of IDs for the guilds a user has joined.
332
334
333
335
Args:
334
336
user_id: The ID of the user
337
+
335
338
Returns:
336
339
A list of snowflakes for the guilds the client can see the user is within
337
340
"""
@@ -361,16 +364,15 @@ def get_user_guild_ids(self, user_id: "Snowflake_Type") -> List["Snowflake_Type"
361
364
# region Message cache
362
365
363
366
async def fetch_message (
364
- self ,
365
- channel_id : "Snowflake_Type" ,
366
- message_id : "Snowflake_Type" ,
367
+ self , channel_id : "Snowflake_Type" , message_id : "Snowflake_Type" , * , force : bool = False
367
368
) -> Message :
368
369
"""
369
370
Fetch a message from a channel based on their IDs.
370
371
371
372
Args:
372
373
channel_id: The ID of the channel the message is in
373
374
message_id: The ID of the message
375
+ force: If the cache should be ignored, and the message should be fetched from the API
374
376
375
377
Returns:
376
378
The message if found
@@ -379,7 +381,7 @@ async def fetch_message(
379
381
message_id = to_snowflake (message_id )
380
382
message = self .message_cache .get ((channel_id , message_id ))
381
383
382
- if message is None :
384
+ if message is None or force :
383
385
data = await self ._client .http .get_message (channel_id , message_id )
384
386
message = self .place_message_data (data )
385
387
if message .channel is None :
@@ -437,22 +439,20 @@ def delete_message(self, channel_id: "Snowflake_Type", message_id: "Snowflake_Ty
437
439
# endregion Message cache
438
440
439
441
# region Channel cache
440
- async def fetch_channel (
441
- self ,
442
- channel_id : "Snowflake_Type" ,
443
- ) -> "TYPE_ALL_CHANNEL" :
442
+ async def fetch_channel (self , channel_id : "Snowflake_Type" , * , force : bool = False ) -> "TYPE_ALL_CHANNEL" :
444
443
"""
445
444
Get a channel based on its ID.
446
445
447
446
Args:
448
447
channel_id: The ID of the channel
448
+ force: If the cache should be ignored, and the channel should be fetched from the API
449
449
450
450
Returns:
451
451
The channel if found
452
452
"""
453
453
channel_id = to_snowflake (channel_id )
454
454
channel = self .channel_cache .get (channel_id )
455
- if channel is None :
455
+ if channel is None or force :
456
456
try :
457
457
data = await self ._client .http .get_channel (channel_id )
458
458
channel = self .place_channel_data (data )
@@ -518,31 +518,33 @@ def place_dm_channel_id(self, user_id: "Snowflake_Type", channel_id: "Snowflake_
518
518
"""
519
519
self .dm_channels [to_snowflake (user_id )] = to_snowflake (channel_id )
520
520
521
- async def fetch_dm_channel_id (self , user_id : "Snowflake_Type" ) -> "Snowflake_Type" :
521
+ async def fetch_dm_channel_id (self , user_id : "Snowflake_Type" , * , force : bool = False ) -> "Snowflake_Type" :
522
522
"""
523
523
Get the DM channel ID for a user.
524
524
525
525
Args:
526
526
user_id: The ID of the user
527
+ force: If the cache should be ignored, and the channel should be fetched from the API
527
528
"""
528
529
user_id = to_snowflake (user_id )
529
530
channel_id = self .dm_channels .get (user_id )
530
- if channel_id is None :
531
+ if channel_id is None or force :
531
532
data = await self ._client .http .create_dm (user_id )
532
533
channel = self .place_channel_data (data )
533
534
channel_id = channel .id
534
535
return channel_id
535
536
536
- async def fetch_dm_channel (self , user_id : "Snowflake_Type" ) -> "DM" :
537
+ async def fetch_dm_channel (self , user_id : "Snowflake_Type" , * , force : bool = False ) -> "DM" :
537
538
"""
538
539
Fetch the DM channel for a user.
539
540
540
541
Args:
541
542
user_id: The ID of the user
543
+ force: If the cache should be ignored, and the channel should be fetched from the API
542
544
"""
543
545
user_id = to_snowflake (user_id )
544
- channel_id = await self .fetch_dm_channel_id (user_id )
545
- return await self .fetch_channel (channel_id )
546
+ channel_id = await self .fetch_dm_channel_id (user_id , force = force )
547
+ return await self .fetch_channel (channel_id , force = force )
546
548
547
549
def get_dm_channel (self , user_id : Optional ["Snowflake_Type" ]) -> Optional ["DM" ]:
548
550
"""
@@ -575,19 +577,20 @@ def delete_channel(self, channel_id: "Snowflake_Type") -> None:
575
577
576
578
# region Guild cache
577
579
578
- async def fetch_guild (self , guild_id : "Snowflake_Type" ) -> Guild :
580
+ async def fetch_guild (self , guild_id : "Snowflake_Type" , * , force : bool = False ) -> Guild :
579
581
"""
580
582
Fetch a guild based on its ID.
581
583
582
584
Args:
583
585
guild_id: The ID of the guild
586
+ force: If the cache should be ignored, and the guild should be fetched from the API
584
587
585
588
Returns:
586
589
The guild if found
587
590
"""
588
591
guild_id = to_snowflake (guild_id )
589
592
guild = self .guild_cache .get (guild_id )
590
- if guild is None :
593
+ if guild is None or force :
591
594
data = await self ._client .http .get_guild (guild_id )
592
595
guild = self .place_guild_data (data )
593
596
return guild
@@ -648,21 +651,24 @@ async def fetch_role(
648
651
self ,
649
652
guild_id : "Snowflake_Type" ,
650
653
role_id : "Snowflake_Type" ,
654
+ * ,
655
+ force : bool = False ,
651
656
) -> Role :
652
657
"""
653
658
Fetch a role based on the guild and its own ID.
654
659
655
660
Args:
656
661
guild_id: The ID of the guild this role belongs to
657
662
role_id: The ID of the role
663
+ force: If the cache should be ignored, and the role should be fetched from the API
658
664
659
665
Returns:
660
666
The role if found
661
667
"""
662
668
guild_id = to_snowflake (guild_id )
663
669
role_id = to_snowflake (role_id )
664
670
role = self .role_cache .get (role_id )
665
- if role is None :
671
+ if role is None or force :
666
672
data = await self ._client .http .get_roles (guild_id )
667
673
role = self .place_role_data (guild_id , data ).get (role_id )
668
674
return role
@@ -830,9 +836,7 @@ def delete_bot_voice_state(self, guild_id: "Snowflake_Type") -> None:
830
836
# region Emoji cache
831
837
832
838
async def fetch_emoji (
833
- self ,
834
- guild_id : "Snowflake_Type" ,
835
- emoji_id : "Snowflake_Type" ,
839
+ self , guild_id : "Snowflake_Type" , emoji_id : "Snowflake_Type" , * , force : bool = False
836
840
) -> "CustomEmoji" :
837
841
"""
838
842
Fetch an emoji based on the guild and its own ID.
@@ -842,14 +846,15 @@ async def fetch_emoji(
842
846
Args:
843
847
guild_id: The ID of the guild this emoji belongs to
844
848
emoji_id: The ID of the emoji
849
+ force: If the cache should be ignored, and the emoji should be fetched from the API
845
850
846
851
Returns:
847
852
The Emoji if found
848
853
"""
849
854
guild_id = to_snowflake (guild_id )
850
855
emoji_id = to_snowflake (emoji_id )
851
856
emoji = self .emoji_cache .get (emoji_id ) if self .emoji_cache is not None else None
852
- if emoji is None :
857
+ if emoji is None or force :
853
858
data = await self ._client .http .get_guild_emoji (guild_id , emoji_id )
854
859
emoji = self .place_emoji_data (guild_id , data )
855
860
0 commit comments