diff options
| author | Rapptz <[email protected]> | 2016-05-01 07:22:45 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2016-05-01 07:22:45 -0400 |
| commit | 1acf478fb720e47b23dea81e86e15876a8e0ef72 (patch) | |
| tree | e750e6662e48fb31578fd0f0196a35e602030728 | |
| parent | [commands] Fix when_mentioned when handling nicknames. (diff) | |
| download | discord.py-1acf478fb720e47b23dea81e86e15876a8e0ef72.tar.xz discord.py-1acf478fb720e47b23dea81e86e15876a8e0ef72.zip | |
Make Permissions an iterable class.
| -rw-r--r-- | discord/permissions.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/discord/permissions.py b/discord/permissions.py index bc74536c..f7e262d1 100644 --- a/discord/permissions.py +++ b/discord/permissions.py @@ -50,6 +50,11 @@ class Permissions: +-----------+------------------------------------------+ | hash(x) | Return the permission's hash. | +-----------+------------------------------------------+ + | iter(x) | Returns an iterator of (perm, value) | + | | pairs. This allows this class to be used | + | | as an iterable in e.g. set/list/dict | + | | constructions. | + +-----------+------------------------------------------+ The properties provided are two way. You can set and retrieve individual bits using the properties as if they were regular bools. This allows you to edit permissions. @@ -75,6 +80,16 @@ class Permissions: def __hash__(self): return hash(self.value) + def _perm_iterator(self): + for attr in dir(self): + # check if it's a property, because if so it's a permission + is_property = isinstance(getattr(self.__class__, attr), property) + if is_property: + yield (attr, getattr(self, attr)) + + def __iter__(self): + return self._perm_iterator() + def is_subset(self, other): """Returns True if other has the same or fewer permissions as self.""" if isinstance(other, Permissions): |