Repository: hikari-py/hikari
Pull request #651 (2021-06-08 13:34:15)

Add thread permissions + all perms method
### Summary
Add thread permissions + all perms method
* Add note for permissions which can be gated behind 2fa

### Checklist
<!-- Make sure to tick all the following boxes by putting an `x` in between (like this `[x]`) -->
- [x] I have run `nox` and all the pipelines have passed.
- [x] I have made unittests according to the code I have added/modified/deleted.

### Related issues
<!--
To mention an issue use `#issue-id` and to mention a merge request use `!merge-request-id`
To close/fix an issue use `Close #issue-id` or `Fix #issue-id` (depending on the merge request)
-->



Patch:
diff --git a/hikari/impl/shard.py b/hikari/impl/shard.py
index ba73e7da..ae314152 100644
--- a/hikari/impl/shard.py
+++ b/hikari/impl/shard.py
@@ -28,7 +28,6 @@
 
 import asyncio
 import contextlib
-import json
 import logging
 import platform
 import sys
@@ -156,7 +155,7 @@ async def send_close(self, *, code: int = 1000, message: bytes = b"") -> bool:
     async def receive_json(
         self,
         *,
-        loads: aiohttp.typedefs.JSONDecoder = json.loads,
+        loads: aiohttp.typedefs.JSONDecoder = data_binding.load_json,
         timeout: typing.Optional[float] = None,
     ) -> typing.Any:
         pl = await self._receive_and_check(timeout)
@@ -170,7 +169,7 @@ async def send_json(
         data: data_binding.JSONObject,
         compress: typing.Optional[int] = None,
         *,
-        dumps: aiohttp.typedefs.JSONEncoder = json.dumps,
+        dumps: aiohttp.typedefs.JSONEncoder = data_binding.dump_json,
     ) -> None:
         pl = dumps(data)
         if self.logger.isEnabledFor(ux.TRACE):
@@ -534,7 +533,7 @@ async def _send_json(
         data: data_binding.JSONObject,
         compress: typing.Optional[int] = None,
         *,
-        dumps: aiohttp.typedefs.JSONEncoder = json.dumps,
+        dumps: aiohttp.typedefs.JSONEncoder = data_binding.dump_json,
     ) -> None:
         await self._total_rate_limit.acquire()
 
diff --git a/hikari/intents.py b/hikari/intents.py
index e593154f..978ccc26 100644
--- a/hikari/intents.py
+++ b/hikari/intents.py
@@ -370,4 +370,4 @@ def is_privileged(self) -> bool:
         this intent on the Discord Developer Portal before you can utilise it
         in your application.
         """
-        return bool(self & (self.GUILD_MEMBERS | self.GUILD_PRESENCES))
+        return bool(self & self.ALL_PRIVILEGED)
diff --git a/hikari/permissions.py b/hikari/permissions.py
index c9470908..22079c29 100644
--- a/hikari/permissions.py
+++ b/hikari/permissions.py
@@ -26,6 +26,8 @@
 
 __all__: typing.List[str] = ["Permissions"]
 
+import functools
+import operator
 import typing
 
 from hikari.internal import enums
@@ -94,19 +96,49 @@ class Permissions(enums.Flag):
     """Allows creation of instant invites."""
 
     KICK_MEMBERS = 1 << 1
-    """Allows kicking members"""
+    """Allows kicking members.
+
+    !!! note
+        In guilds with server-wide 2FA enabled this permission can only be used
+        by users who have two-factor authentication enabled on their account
+        (or their owner's account in the case of bot users) and the guild owner.
+    """
 
     BAN_MEMBERS = 1 << 2
-    """Allows banning members."""
+    """Allows banning members.
+
+    !!! note
+        In guilds with server-wide 2FA enabled this permission can only be used
+        by users who have two-factor authentication enabled on their account
+        (or their owner's account in the case of bot users) and the guild owner.
+    """
 
     ADMINISTRATOR = 1 << 3
-    """Allows all permissions and bypasses channel permission overwrites."""
+    """Allows all permissions and bypasses channel permission overwrites.
+
+    !!! note
+        In guilds with server-wide 2FA enabled this permission can only be used
+        by users who have two-factor authentication enabled on their account
+        (or their owner's account in the case of bot users) and the guild owner.
+    """
 
     MANAGE_CHANNELS = 1 << 4
-    """Allows management and editing of channels."""
+    """Allows management and editing of channels.
+
+    !!! note
+        In guilds with server-wide 2FA enabled this permission can only be used
+        by users who have two-factor authentication enabled on their account
+        (or their owner's account in the case of bot users) and the guild owner.
+    """
 
     MANAGE_GUILD = 1 << 5
-    """Allows management and editing of the guild."""
+    """Allows management and editing of the guild.
+
+    !!! note
+        In guilds with server-wide 2FA enabled this permission can only be used
+        by users who have two-factor authentication enabled on their account
+        (or their owner's account in the case of bot users) and the guild owner.
+    """
 
     ADD_REACTIONS = 1 << 6
     """Allows for the addition of reactions to messages."""
@@ -130,7 +162,13 @@ class Permissions(enums.Flag):
     """Allows for sending of `/tts` messages."""
 
     MANAGE_MESSAGES = 1 << 13
-    """Allows for deletion of other users messages."""
+    """Allows for deletion of other users messages.
+
+    !!! note
+        In guilds with server-wide 2FA enabled this permission can only be used
+        by users who have two-factor authentication enabled on their account
+        (or their owner's account in the case of bot users) and the guild owner.
+    """
 
     EMBED_LINKS = 1 << 14
     """Links sent by users with this permission will be auto-embedded."""
@@ -179,13 +217,31 @@ class Permissions(enums.Flag):
     """Allows for modification of other users nicknames."""
 
     MANAGE_ROLES = 1 << 28
-    """Allows management and editing of roles."""
+    """Allows management and editing of roles.
+
+    !!! note
+        In guilds with server-wide 2FA enabled this permission can only be used
+        by users who have two-factor authentication enabled on their account
+        (or their owner's account in the case of bot users) and the guild owner.
+    """
 
     MANAGE_WEBHOOKS = 1 << 29
-    """Allows management and editing of webhooks."""
+    """Allows management and editing of webhooks.
+
+    !!! note
+        In guilds with server-wide 2FA enabled this permission can only be used
+        by users who have two-factor authentication enabled on their account
+        (or their owner's account in the case of bot users) and the guild owner.
+    """
 
     MANAGE_EMOJIS = 1 << 30
-    """Allows management and editing of emojis."""
+    """Allows management and editing of emojis.
+
+    !!! note
+        In guilds with server-wide 2FA enabled this permission can only be used
+        by users who have two-factor authentication enabled on their account
+        (or their owner's account in the case of bot users) and the guild owner.
+    """
 
     USE_APPLICATION_COMMANDS = 1 << 31
     """Allows for using the application commands of guild integrations within a text channel."""
@@ -198,3 +254,29 @@ class Permissions(enums.Flag):
         development" by Discord meaning that "it may be changed or removed"
         without warning.
     """
+
+    MANAGE_THREADS = 1 << 34
+    """Allows for deleting and archiving threads, and viewing all private threads.
+
+     !!! note
+        In guilds with server-wide 2FA enabled this permission can only be used
+        by users who have two-factor authentication enabled on their account
+        (or their owner's account in the case of bot users) and the guild owner.
+    """
+
+    USE_PUBLIC_THREADS = 1 << 35
+    """Allows for creating and participating in threads."""
+
+    USE_PRIVATE_THREADS = 1 << 36
+    """Allows for creating and participating in private threads."""
+
+    @classmethod
+    def all_permissions(cls) -> Permissions:
+        """Get an instance of `Permissions` with all the known permissions.
+
+        Returns
+        -------
+        Permissions
+            A permissions instance with all the known permissions.
+        """
+        return functools.reduce(operator.ior, Permissions)


Test patch:
diff --git a/tests/hikari/test_permissions.py b/tests/hikari/test_permissions.py
index ef09b994..409a1b61 100644
--- a/tests/hikari/test_permissions.py
+++ b/tests/hikari/test_permissions.py
@@ -19,3 +19,12 @@
 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 # SOFTWARE.
+from hikari import permissions
+
+
+class TestPermissions:
+    def test_all_permissions(self):
+        all_perms = permissions.Permissions.all_permissions()
+
+        assert isinstance(all_perms, permissions.Permissions)
+        assert all_perms == 128849018879