diff options
| author | Rapptz <[email protected]> | 2019-08-27 04:49:23 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2019-08-27 04:50:50 -0400 |
| commit | cf79816c5dc5aa3a797137092644c7b6f858d2fe (patch) | |
| tree | 74fc4e30e582f6997bd12d0ca7956104dd8a7391 | |
| parent | Add Message.is_system to more intuitively filter system messages. (diff) | |
| download | discord.py-cf79816c5dc5aa3a797137092644c7b6f858d2fe.tar.xz discord.py-cf79816c5dc5aa3a797137092644c7b6f858d2fe.zip | |
Allow disabling the message cache with max_messages=None
This also changes the default to 1000 instead of 5000 since it can
take some time for small bots to cycle through the default and they
make up the majority of it.
| -rw-r--r-- | discord/client.py | 8 | ||||
| -rw-r--r-- | discord/state.py | 22 |
2 files changed, 20 insertions, 10 deletions
diff --git a/discord/client.py b/discord/client.py index 333db804..e5712454 100644 --- a/discord/client.py +++ b/discord/client.py @@ -122,8 +122,10 @@ class Client: ----------- max_messages: Optional[:class:`int`] The maximum number of messages to store in the internal message cache. - This defaults to 5000. Passing in ``None`` or a value less than 100 - will use the default instead of the passed in value. + This defaults to 1000. Passing in ``None`` disables the message cache. + + .. versionchanged:: 1.3 + Allow disabling the message cache and change the default size to 1000. loop: Optional[:class:`asyncio.AbstractEventLoop`] The :class:`asyncio.AbstractEventLoop` to use for asynchronous operations. Defaults to ``None``, in which case the default event loop is used via @@ -274,7 +276,7 @@ class Client: .. versionadded:: 1.1.0 """ - return utils.SequenceProxy(self._connection._messages) + return utils.SequenceProxy(self._connection._messages or []) @property def private_channels(self): diff --git a/discord/state.py b/discord/state.py index fc7ea7fa..a24d1b0b 100644 --- a/discord/state.py +++ b/discord/state.py @@ -61,7 +61,10 @@ class ConnectionState: def __init__(self, *, dispatch, chunker, handlers, syncer, http, loop, **options): self.loop = loop self.http = http - self.max_messages = max(options.get('max_messages', 5000), 100) + self.max_messages = options.get('max_messages', 1000) + if self.max_messages is not None and self.max_messages <= 0: + self.max_messages = 1000 + self.dispatch = dispatch self.chunker = chunker self.syncer = syncer @@ -112,7 +115,7 @@ class ConnectionState: self._private_channels = OrderedDict() # extra dict to look up private channels by user id self._private_channels_by_user = {} - self._messages = deque(maxlen=self.max_messages) + self._messages = self.max_messages and deque(maxlen=self.max_messages) def process_listeners(self, listener_type, argument, result): removed = [] @@ -253,7 +256,7 @@ class ConnectionState: self._private_channels_by_user.pop(channel.recipient.id, None) def _get_message(self, msg_id): - return utils.find(lambda m: m.id == msg_id, reversed(self._messages)) + return self._messages and utils.find(lambda m: m.id == msg_id, reversed(self._messages)) def _add_guild_from_data(self, guild): guild = Guild(data=guild, state=self) @@ -398,7 +401,8 @@ class ConnectionState: channel, _ = self._get_guild_channel(data) message = Message(channel=channel, data=data, state=self) self.dispatch('message', message) - self._messages.append(message) + if self._messages: + self._messages.append(message) if channel and channel.__class__ is TextChannel: channel.last_message_id = message.id @@ -407,13 +411,16 @@ class ConnectionState: found = self._get_message(raw.message_id) raw.cached_message = found self.dispatch('raw_message_delete', raw) - if found is not None: + if self._messages and found is not None: self.dispatch('message_delete', found) self._messages.remove(found) def parse_message_delete_bulk(self, data): raw = RawBulkMessageDeleteEvent(data) - found_messages = [message for message in self._messages if message.id in raw.message_ids] + if self._messages: + found_messages = [message for message in self._messages if message.id in raw.message_ids] + else: + found_messages = [] raw.cached_messages = found_messages self.dispatch('raw_bulk_message_delete', raw) if found_messages: @@ -751,7 +758,8 @@ class ConnectionState: return # do a cleanup of the messages cache - self._messages = deque((msg for msg in self._messages if msg.guild != guild), maxlen=self.max_messages) + if self._messages is not None: + self._messages = deque((msg for msg in self._messages if msg.guild != guild), maxlen=self.max_messages) self._remove_guild(guild) self.dispatch('guild_remove', guild) |