From 7ff7b0fb9c373c2145788408e60ac18e24c9401a Mon Sep 17 00:00:00 2001 From: Rapptz Date: Mon, 21 Nov 2016 03:08:03 -0500 Subject: [PATCH] Add Permissions.update and PermissionOverwrite.update for bulk edits. This should satisfy those that have a one-line obsession and make things a little bit easier if you have a dict. --- discord/permissions.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/discord/permissions.py b/discord/permissions.py index e6a1df586..74be8a434 100644 --- a/discord/permissions.py +++ b/discord/permissions.py @@ -162,6 +162,26 @@ class Permissions: "Voice" permissions from the official Discord UI set to True.""" return cls(0b00000011111100000000000000000000) + def update(self, **kwargs): + """Bulk updates this permission object. + + Allows you to set multiple attributes by using keyword + arguments. The names must be equivalent to the properties + listed. Extraneous key/value pairs will be silently ignored. + + Parameters + ------------ + \*\*kwargs + A list of key/value pairs to bulk update permissions with. + """ + for key, value in kwargs.items(): + try: + is_property = isinstance(getattr(self.__class__, key), property) + except AttributeError: + continue + + if is_property: + setattr(self, key, value) def _bit(self, index): return bool((self.value >> index) & 1) @@ -537,6 +557,24 @@ class PermissionOverwrite: return ret + def update(self, **kwargs): + """Bulk updates this permission overwrite object. + + Allows you to set multiple attributes by using keyword + arguments. The names must be equivalent to the properties + listed. Extraneous key/value pairs will be silently ignored. + + Parameters + ------------ + \*\*kwargs + A list of key/value pairs to bulk update with. + """ + for key, value in kwargs.items(): + if key not in self.VALID_NAMES: + continue + + setattr(self, key, value) + def __iter__(self): for key in self.VALID_NAMES: yield key, self._values.get(key)