From b1fae0861a6011fb4c37859db2d78f7ceb4a7b13 Mon Sep 17 00:00:00 2001 From: khazhyk Date: Sun, 14 Apr 2019 18:50:28 -0700 Subject: add read-only cached_messages property to Client For those of us who want access to this sweet trove of zero hop messages --- discord/client.py | 8 ++++++++ discord/utils.py | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) (limited to 'discord') diff --git a/discord/client.py b/discord/client.py index e0d7900b..2f70082b 100644 --- a/discord/client.py +++ b/discord/client.py @@ -227,6 +227,14 @@ class Client: """List[:class:`.Emoji`]: The emojis that the connected client has.""" return self._connection.emojis + @property + def cached_messages(self): + """Sequence[:class:`~discord.Message`]: Read-only list of messages the connected client has cached. + + .. versionadded:: 1.1.0 + """ + return utils.SequenceProxy(self._connection._messages) + @property def private_channels(self): """List[:class:`abc.PrivateChannel`]: The private channels that the connected client is participating on. diff --git a/discord/utils.py b/discord/utils.py index 4dfc7a0c..8303916c 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -26,6 +26,7 @@ DEALINGS IN THE SOFTWARE. import array import asyncio +import collections.abc import unicodedata from base64 import b64encode from bisect import bisect_left @@ -78,6 +79,32 @@ def cached_slot_property(name): return CachedSlotProperty(name, func) return decorator +class SequenceProxy(collections.abc.Sequence): + """Read-only proxy of a Sequence.""" + def __init__(self, proxied): + self.__proxied = proxied + + def __getitem__(self, idx): + return self.__proxied[idx] + + def __len__(self): + return len(self.__proxied) + + def __contains__(self, item): + return item in self.__proxied + + def __iter__(self): + return iter(self.__proxied) + + def __reversed__(self): + return reversed(self.__proxied) + + def index(self, value, *args, **kwargs): + return self.__proxied.index(value, *args, **kwargs) + + def count(self, value): + return self.__proxied.count(value) + def parse_time(timestamp): if timestamp: return datetime.datetime(*map(int, re.split(r'[^\d]', timestamp.replace('+00:00', '')))) -- cgit v1.2.3