-
Notifications
You must be signed in to change notification settings - Fork 33
✨ Add permission handler #378
Changes from 15 commits
e631007
a78c570
707ef31
93a97bf
97cb5b6
eeae339
b3b0064
d58c694
4633104
282a0dd
b47c2c0
d42f827
5f67b5f
0a0d2f2
80cd31b
7c3fe1e
cec9822
1a343f1
cbd4c96
250fdda
4e19416
ddd5286
441a117
3d3606e
4d7d0b4
629a185
2dd75d9
dd2a853
30e7887
9a73186
fba1cde
7bffe0c
88beab7
2ed13e9
5e79113
57734d7
ef49bfa
1171921
eb974ec
b9688c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,121 @@ | ||||||
| # Copyright Pincer 2021-Present | ||||||
| # Full MIT License can be found in `LICENSE` at the project root. | ||||||
|
|
||||||
| from __future__ import annotations | ||||||
|
|
||||||
| from enum import Enum | ||||||
| from typing import Tuple, Optional | ||||||
|
|
||||||
|
|
||||||
| class PermissionEnums(Enum): | ||||||
|
||||||
| class PermissionEnums(Enum): | |
| class Permissions(Enum): |
beastmatser marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
beastmatser marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I not sure there is any scenario where int can be equal to tuple.
| elif isinstance(object, tuple): | |
| return self.to_int() == object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function returns a tuple of ints. Maybe the function name should be changed because I was confused by this too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few ideas:
- to_ints
- to_tuple
- convert
I changed it to_tuple for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should do to_ints since it matches with from_ints
My other idea is to_permission_tuple but thats kinda long.
beastmatser marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
zunda-arrow marked this conversation as resolved.
Show resolved
Hide resolved
beastmatser marked this conversation as resolved.
Show resolved
Hide resolved
beastmatser marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # Copyright Pincer 2021-Present | ||
| # Full MIT License can be found in `LICENSE` at the project root. | ||
| import pytest | ||
| from pincer.objects.guild.permissions import Permission, PermissionEnums | ||
|
|
||
|
|
||
| class TestPermission: | ||
| @staticmethod | ||
| def test_invalid_permissions(): | ||
| with pytest.raises(ValueError): | ||
| Permission(this_permisison_does_not_exist=True) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| Permission( | ||
| manage_channels="True", | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def test_valid_permissions(): | ||
| valid_perms = ( | ||
| "create_instant_invite", | ||
| "kick_members", | ||
| "ban_members", | ||
| "administrator", | ||
| "manage_channels", | ||
| "manage_guild", | ||
| "add_reactions", | ||
| "view_audit_log", | ||
| "priority_speaker", | ||
| "stream", | ||
| "view_channel", | ||
| "send_messages", | ||
| "send_tts_messages", | ||
| "manage_messages", | ||
| "embed_links", | ||
| "attach_files", | ||
| "read_message_history", | ||
| "mention_everyone", | ||
| "use_external_emojis", | ||
| "view_guild_insights", | ||
| "connect", | ||
| "speak", | ||
| "mute_members", | ||
| "deafen_members", | ||
| "move_members", | ||
| "use_vad", | ||
| "change_nickname", | ||
| "manage_nicknames", | ||
| "manage_roles", | ||
| "manage_webhooks", | ||
| "manage_emojis_and_stickers", | ||
| "use_application_commands", | ||
| "request_to_speak", | ||
| "manage_events", | ||
| "manage_threads", | ||
| "create_public_threads", | ||
| "create_private_threads", | ||
| "use_external_stickers", | ||
| "send_messages_in_threads", | ||
| "start_embedded_activities", | ||
| "moderate_members", | ||
| ) | ||
|
|
||
| for perm in valid_perms: | ||
| assert hasattr(Permission(), perm) | ||
|
|
||
| @staticmethod | ||
| def test_from_int(): | ||
| assert Permission.from_int(1025, 268435472) == Permission( | ||
| view_channel=True, | ||
| manage_channels=False, | ||
| create_instant_invite=True, | ||
| manage_roles=False, | ||
| ) | ||
|
|
||
| assert Permission.from_int(0, 0) == Permission() | ||
|
|
||
| @staticmethod | ||
| def test_to_int(): | ||
| allow, deny = Permission.to_int(Permission()) | ||
| assert allow == 0 | ||
| assert deny == 0 | ||
|
|
||
| permission = Permission() | ||
| for enum in PermissionEnums: | ||
| if getattr(permission, enum.name.lower()): | ||
| allow |= enum.value | ||
| elif getattr(permission, enum.name.lower()) is False: | ||
| deny |= enum.value | ||
|
|
||
| assert Permission.to_int(Permission()) == (0, 0) | ||
|
|
||
| @staticmethod | ||
| def test_allow(): | ||
| permission = Permission( | ||
| view_channel=True, | ||
| manage_channels=False, | ||
| create_instant_invite=True, | ||
| manage_roles=False, | ||
| ) | ||
|
|
||
| assert permission.allow == 1025 | ||
| assert permission.deny == 268435472 |
Uh oh!
There was an error while loading. Please reload this page.