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 /discord/state.py | |
| 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.
Diffstat (limited to 'discord/state.py')
| -rw-r--r-- | discord/state.py | 22 |
1 files changed, 15 insertions, 7 deletions
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) |