Browse Source

Add chunk_guilds_at_startup and deprecate fetch_offline_members

pull/5852/head
Rapptz 5 years ago
parent
commit
005a80303f
  1. 20
      discord/client.py
  2. 9
      discord/shard.py
  3. 21
      discord/state.py

20
discord/client.py

@ -153,10 +153,14 @@ class Client:
.. versionadded:: 1.5
fetch_offline_members: :class:`bool`
Indicates if :func:`.on_ready` should be delayed to fetch all offline
members from the guilds the client belongs to. If this is ``False``\, then
no offline members are received and :meth:`request_offline_members`
must be used to fetch the offline members of the guild.
A deprecated alias of ``chunk_guilds_at_startup``.
chunk_guilds_at_startup: :class:`bool`
Indicates if :func:`.on_ready` should be delayed to chunk all guilds
at start-up if necessary. This operation is incredibly slow for large
amounts of guilds. The default is ``True`` if :attr:`Intents.members`
is ``True``.
.. versionadded:: 1.5
status: Optional[:class:`.Status`]
A status to start your presence with upon logging on to Discord.
activity: Optional[:class:`.BaseActivity`]
@ -243,9 +247,7 @@ class Client:
'before_identify': self._call_before_identify_hook
}
self._connection = ConnectionState(dispatch=self.dispatch, handlers=self._handlers,
hooks=self._hooks, syncer=self._syncer, http=self.http, loop=self.loop, **options)
self._connection = self._get_state(**options)
self._connection.shard_count = self.shard_count
self._closed = False
self._ready = asyncio.Event()
@ -261,6 +263,10 @@ class Client:
def _get_websocket(self, guild_id=None, *, shard_id=None):
return self.ws
def _get_state(self, **options):
return ConnectionState(dispatch=self.dispatch, handlers=self._handlers,
hooks=self._hooks, syncer=self._syncer, http=self.http, loop=self.loop, **options)
async def _syncer(self, guilds):
await self.ws.request_sync(guilds)

9
discord/shard.py

@ -295,10 +295,6 @@ class AutoShardedClient(Client):
elif not isinstance(self.shard_ids, (list, tuple)):
raise ClientException('shard_ids parameter must be a list or a tuple.')
self._connection = AutoShardedConnectionState(dispatch=self.dispatch,
handlers=self._handlers, syncer=self._syncer,
hooks=self._hooks, http=self.http, loop=self.loop, **kwargs)
# instead of a single websocket, we have multiple
# the key is the shard_id
self.__shards = {}
@ -311,6 +307,11 @@ class AutoShardedClient(Client):
shard_id = (guild_id >> 22) % self.shard_count
return self.__shards[shard_id].ws
def _get_state(self, **options):
return AutoShardedConnectionState(dispatch=self.dispatch,
handlers=self._handlers, syncer=self._syncer,
hooks=self._hooks, http=self.http, loop=self.loop, **options)
@property
def latency(self):
""":class:`float`: Measures latency between a HEARTBEAT and a HEARTBEAT_ACK in seconds.

21
discord/state.py

@ -32,6 +32,7 @@ import itertools
import logging
import math
import weakref
import warnings
import inspect
import gc
@ -103,7 +104,6 @@ class ConnectionState:
self.hooks = hooks
self.shard_count = None
self._ready_task = None
self._fetch_offline = options.get('fetch_offline_members', True)
self.heartbeat_timeout = options.get('heartbeat_timeout', 60.0)
self.guild_ready_timeout = options.get('guild_ready_timeout', 2.0)
if self.guild_ready_timeout < 0:
@ -136,12 +136,23 @@ class ConnectionState:
if intents is not None:
if not isinstance(intents, Intents):
raise TypeError('intents parameter must be Intent not %r' % type(intents))
if not intents.members and self._fetch_offline:
raise ValueError('Intents.members has be enabled to fetch offline members.')
else:
intents = Intents()
try:
chunk_guilds = options['fetch_offline_members']
except KeyError:
chunk_guilds = options.get('chunk_guilds_at_startup', intents.members)
else:
msg = 'fetch_offline_members is deprecated, use chunk_guilds_at_startup instead'
warnings.warn(msg, DeprecationWarning, stacklevel=4)
self._chunk_guilds = chunk_guilds
# Ensure these two are set properly
if not intents.members and self._chunk_guilds:
raise ValueError('Intents.members has be enabled to chunk guilds at startup.')
cache_flags = options.get('member_cache_flags', None)
if cache_flags is None:
cache_flags = MemberCacheFlags.from_intents(intents)
@ -333,7 +344,7 @@ class ConnectionState:
def _guild_needs_chunking(self, guild):
# If presences are enabled then we get back the old guild.large behaviour
return self._fetch_offline and not guild.chunked and not (self._intents.presences and not guild.large)
return self._chunk_guilds and not guild.chunked and not (self._intents.presences and not guild.large)
def _get_guild_channel(self, data):
channel_id = int(data['channel_id'])

Loading…
Cancel
Save