Skip to content

Commit b4154b8

Browse files
authored
refactor!: Change type of permissions attrs to Permissions (#1209)
* refactor!: Mark permissions attrs as `Permissions` class * fix: thanks, git
1 parent a6f832d commit b4154b8

File tree

6 files changed

+42
-27
lines changed

6 files changed

+42
-27
lines changed

interactions/api/models/channel.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from ...utils.attrs_utils import (
2323
ClientSerializerMixin,
2424
DictSerializerMixin,
25+
convert_int,
2526
convert_list,
2627
define,
2728
field,
@@ -433,7 +434,7 @@ class Channel(ClientSerializerMixin, IDMixin):
433434
:ivar Optional[ThreadMetadata] thread_metadata: The thread metadata of the channel.
434435
:ivar Optional[ThreadMember] member: The member of the thread in the channel.
435436
:ivar Optional[int] default_auto_archive_duration: The set auto-archive time for all threads to naturally follow in the channel.
436-
:ivar Optional[str] permissions: The permissions of the channel.
437+
:ivar Optional[Permissions] permissions: The permissions of the channel.
437438
:ivar Optional[int] flags: The flags of the channel.
438439
:ivar Optional[int] total_message_sent: Number of messages ever sent in a thread.
439440
:ivar Optional[int] default_thread_slowmode_delay: The default slowmode delay in seconds for threads, if this channel is a forum.
@@ -484,7 +485,9 @@ class Channel(ClientSerializerMixin, IDMixin):
484485
converter=ThreadMember, default=None, add_client=True, repr=False
485486
)
486487
default_auto_archive_duration: Optional[int] = field(default=None)
487-
permissions: Optional[str] = field(default=None, repr=False)
488+
permissions: Optional[Permissions] = field(
489+
converter=convert_int(Permissions), default=None, repr=False
490+
)
488491
flags: Optional[int] = field(default=None, repr=False)
489492
total_message_sent: Optional[int] = field(default=None, repr=False)
490493
default_thread_slowmode_delay: Optional[int] = field(default=None, repr=False)
@@ -2070,7 +2073,7 @@ async def add_permission_overwrite(
20702073
_id = int(id)
20712074
_type = type
20722075

2073-
if not _type:
2076+
if _type is MISSING:
20742077
raise LibraryException(12, "Please set the type of the overwrite!")
20752078

20762079
overwrites.append(Overwrite(id=_id, type=_type, allow=allow, deny=deny))

interactions/api/models/guild.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from ...utils.attrs_utils import (
2121
ClientSerializerMixin,
2222
DictSerializerMixin,
23+
convert_int,
2324
convert_list,
2425
define,
2526
field,
@@ -29,6 +30,7 @@
2930
from .audit_log import AuditLogEvents, AuditLogs
3031
from .channel import Channel, ChannelType, Thread, ThreadMember
3132
from .emoji import Emoji
33+
from .flags import Permissions
3234
from .member import Member
3335
from .message import Sticker, StickerPack
3436
from .misc import (
@@ -349,7 +351,7 @@ class Guild(ClientSerializerMixin, IDMixin):
349351
:ivar Optional[str] discovery_splash: The discovery splash banner of the guild.
350352
:ivar Optional[bool] owner: Whether the guild is owned.
351353
:ivar Snowflake owner_id: The ID of the owner of the guild.
352-
:ivar Optional[str] permissions: The permissions of the guild.
354+
:ivar Optional[Permissions] permissions: The permissions of the guild.
353355
:ivar Optional[str] region: The geographical region of the guild.
354356
:ivar Optional[Snowflake] afk_channel_id: The AFK voice channel of the guild.
355357
:ivar int afk_timeout: The timeout of the AFK voice channel of the guild.
@@ -400,7 +402,9 @@ class Guild(ClientSerializerMixin, IDMixin):
400402
discovery_splash: Optional[str] = field(default=None, repr=False)
401403
owner: Optional[bool] = field(default=None)
402404
owner_id: Snowflake = field(converter=Snowflake, default=None)
403-
permissions: Optional[str] = field(default=None, repr=False)
405+
permissions: Optional[Permissions] = field(
406+
converter=convert_int(Permissions), default=None, repr=False
407+
)
404408
region: Optional[str] = field(default=None, repr=False) # None, we don't do Voices.
405409
afk_channel_id: Optional[Snowflake] = field(converter=Snowflake, default=None)
406410
afk_timeout: Optional[int] = field(default=None)
@@ -703,7 +707,7 @@ async def remove_member_role(
703707
async def create_role(
704708
self,
705709
name: str,
706-
permissions: Optional[int] = MISSING,
710+
permissions: Optional[Union[Permissions, int]] = MISSING,
707711
color: Optional[int] = 0,
708712
hoist: Optional[bool] = False,
709713
icon: Optional[Image] = MISSING,
@@ -718,7 +722,7 @@ async def create_role(
718722
719723
:param str name: The name of the role
720724
:param Optional[int] color: RGB color value as integer, default ``0``
721-
:param Optional[int] permissions: Bitwise value of the enabled/disabled permissions
725+
:param Optional[Union[Permissions, int]] permissions: Bitwise value of the enabled/disabled permissions
722726
:param Optional[bool] hoist: Whether the role should be displayed separately in the sidebar, default ``False``
723727
:param Optional[Image] icon: The role's icon image (if the guild has the ROLE_ICONS feature)
724728
:param Optional[str] unicode_emoji: The role's unicode emoji as a standard emoji (if the guild has the ROLE_ICONS feature)
@@ -729,7 +733,8 @@ async def create_role(
729733
"""
730734
if not self._client:
731735
raise LibraryException(code=13)
732-
_permissions = permissions if permissions is not MISSING else None
736+
737+
_permissions = int(permissions) if permissions is not MISSING else None
733738
_icon = icon if icon is not MISSING else None
734739
_unicode_emoji = unicode_emoji if unicode_emoji is not MISSING else None
735740
payload = dict(
@@ -839,7 +844,7 @@ async def modify_role(
839844
self,
840845
role_id: Union[int, Snowflake, Role],
841846
name: Optional[str] = MISSING,
842-
permissions: Optional[int] = MISSING,
847+
permissions: Optional[Union[Permissions, int]] = MISSING,
843848
color: Optional[int] = MISSING,
844849
hoist: Optional[bool] = MISSING,
845850
icon: Optional[Image] = MISSING,
@@ -855,7 +860,7 @@ async def modify_role(
855860
:param Union[int, Snowflake, Role] role_id: The id of the role to edit
856861
:param Optional[str] name: The name of the role, defaults to the current value of the role
857862
:param Optional[int] color: RGB color value as integer, defaults to the current value of the role
858-
:param Optional[int] permissions: Bitwise value of the enabled/disabled permissions, defaults to the current value of the role
863+
:param Optional[Union[Permissions, int]] permissions: Bitwise value of the enabled/disabled permissions, defaults to the current value of the role
859864
:param Optional[bool] hoist: Whether the role should be displayed separately in the sidebar, defaults to the current value of the role
860865
:param Optional[Image] icon: The role's icon image (if the guild has the ROLE_ICONS feature), defaults to the current value of the role
861866
:param Optional[str] unicode_emoji: The role's unicode emoji as a standard emoji (if the guild has the ROLE_ICONS feature), defaults to the current value of the role
@@ -876,7 +881,7 @@ async def modify_role(
876881
_color = role.color if color is MISSING else color
877882
_hoist = role.hoist if hoist is MISSING else hoist
878883
_mentionable = role.mentionable if mentionable is MISSING else mentionable
879-
_permissions = role.permissions if permissions is MISSING else permissions
884+
_permissions = int(role.permissions if permissions is MISSING else permissions)
880885
_icon = role.icon if icon is MISSING else icon
881886
_unicode_emoji = role.unicode_emoji if unicode_emoji is MISSING else unicode_emoji
882887

interactions/api/models/message.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,9 @@ class Message(ClientSerializerMixin, IDMixin):
766766
position: Optional[int] = field(default=None, repr=False)
767767

768768
def __attrs_post_init__(self):
769+
if self.referenced_message is not None:
770+
self.referenced_message = Message(**self.referenced_message, _client=self._client)
771+
769772
if self.member and self.guild_id:
770773
self.member._extras["guild_id"] = self.guild_id
771774

@@ -790,9 +793,6 @@ def created_at(self) -> datetime:
790793
"""
791794
return self.id.timestamp
792795

793-
if self.referenced_message is not None:
794-
self.referenced_message = Message(**self.referenced_message, _client=self._client)
795-
796796
async def get_channel(self) -> Channel:
797797
"""
798798
.. versionadded:: 4.0.2

interactions/api/models/misc.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from typing import List, Optional, Union
1616

1717
from ...base import get_logger
18-
from ...utils.attrs_utils import DictSerializerMixin, convert_list, define, field
18+
from ...utils.attrs_utils import DictSerializerMixin, convert_int, convert_list, define, field
1919
from ...utils.missing import MISSING
2020
from ..error import LibraryException
2121
from .flags import Permissions
@@ -48,14 +48,14 @@ class Overwrite(DictSerializerMixin):
4848
4949
:ivar str id: Role or User ID
5050
:ivar int type: Type that corresponds ot the ID; 0 for role and 1 for member.
51-
:ivar Union[Permissions, int, str] allow: Permission bit set.
52-
:ivar Union[Permissions, int, str] deny: Permission bit set.
51+
:ivar Permissions allow: Permission bit set.
52+
:ivar Permissions deny: Permission bit set.
5353
"""
5454

5555
id: int = field()
5656
type: int = field()
57-
allow: Union[Permissions, int, str] = field()
58-
deny: Union[Permissions, int, str] = field()
57+
allow: Permissions = field(converter=convert_int(Permissions))
58+
deny: Permissions = field(converter=convert_int(Permissions))
5959

6060

6161
@define()

interactions/api/models/role.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
from datetime import datetime
22
from typing import TYPE_CHECKING, List, Optional, Union
33

4-
from ...utils.attrs_utils import ClientSerializerMixin, DictSerializerMixin, define, field
4+
from ...utils.attrs_utils import (
5+
ClientSerializerMixin,
6+
DictSerializerMixin,
7+
convert_int,
8+
define,
9+
field,
10+
)
511
from ...utils.missing import MISSING
612
from ..error import LibraryException
13+
from .flags import Permissions
714
from .misc import IDMixin, Image, Snowflake
815

916
if TYPE_CHECKING:
@@ -52,7 +59,7 @@ class Role(ClientSerializerMixin, IDMixin):
5259
:ivar Optional[str] icon: Role icon hash, if any.
5360
:ivar Optional[str] unicode_emoji: Role unicode emoji
5461
:ivar int position: Role position
55-
:ivar str permissions: Role permissions as a bit set
62+
:ivar Permissions permissions: Role permissions as a bit set
5663
:ivar bool managed: A status denoting if this role is managed by an integration
5764
:ivar bool mentionable: A status denoting if this role is mentionable
5865
:ivar Optional[RoleTags] tags: The tags this role has
@@ -65,7 +72,7 @@ class Role(ClientSerializerMixin, IDMixin):
6572
icon: Optional[str] = field(default=None, repr=False)
6673
unicode_emoji: Optional[str] = field(default=None)
6774
position: int = field()
68-
permissions: str = field()
75+
permissions: Permissions = field(converter=convert_int(Permissions))
6976
managed: bool = field()
7077
mentionable: bool = field()
7178
tags: Optional[RoleTags] = field(converter=RoleTags, default=None)
@@ -117,7 +124,7 @@ async def modify(
117124
self,
118125
guild_id: Union[int, Snowflake, "Guild"],
119126
name: Optional[str] = MISSING,
120-
permissions: Optional[int] = MISSING,
127+
permissions: Optional[Union[Permissions, int]] = MISSING,
121128
color: Optional[int] = MISSING,
122129
hoist: Optional[bool] = MISSING,
123130
icon: Optional[Image] = MISSING,
@@ -133,7 +140,7 @@ async def modify(
133140
:param int guild_id: The id of the guild to edit the role on
134141
:param Optional[str] name: The name of the role, defaults to the current value of the role
135142
:param Optional[int] color: RGB color value as integer, defaults to the current value of the role
136-
:param Optional[int] permissions: Bitwise value of the enabled/disabled permissions, defaults to the current value of the role
143+
:param Optional[Union[Permissions, int]] permissions: Bitwise value of the enabled/disabled permissions, defaults to the current value of the role
137144
:param Optional[bool] hoist: Whether the role should be displayed separately in the sidebar, defaults to the current value of the role
138145
:param Optional[Image] icon: The role's icon image (if the guild has the ROLE_ICONS feature), defaults to the current value of the role
139146
:param Optional[str] unicode_emoji: The role's unicode emoji as a standard emoji (if the guild has the ROLE_ICONS feature), defaults to the current value of the role
@@ -148,7 +155,7 @@ async def modify(
148155
_color = self.color if color is MISSING else color
149156
_hoist = self.hoist if hoist is MISSING else hoist
150157
_mentionable = self.mentionable if mentionable is MISSING else mentionable
151-
_permissions = self.permissions if permissions is MISSING else permissions
158+
_permissions = int(self.permissions if permissions is MISSING else permissions)
152159
_icon = self.icon if icon is MISSING else icon
153160
_unicode_emoji = self.unicode_emoji if unicode_emoji is MISSING else unicode_emoji
154161
_guild_id = int(guild_id) if isinstance(guild_id, (int, Snowflake)) else int(guild_id.id)

interactions/client/context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ class CommandContext(_Context):
434434
:ivar bool deferred: Whether the response was deferred or not.
435435
:ivar Optional[Locale] locale: The selected language of the user invoking the interaction.
436436
:ivar Optional[Locale] guild_locale: The guild's preferred language, if invoked in a guild.
437-
:ivar str app_permissions: Bitwise set of permissions the bot has within the channel the interaction was sent from.
437+
:ivar Permissions app_permissions: Bitwise set of permissions the bot has within the channel the interaction was sent from.
438438
:ivar Client client:
439439
.. versionadded:: 4.3.0
440440
@@ -696,7 +696,7 @@ class ComponentContext(_Context):
696696
:ivar bool deferred: Whether the response was deferred or not.
697697
:ivar Optional[Locale] locale: The selected language of the user invoking the interaction.
698698
:ivar Optional[Locale] guild_locale: The guild's preferred language, if invoked in a guild.
699-
:ivar str app_permissions: Bitwise set of permissions the bot has within the channel the interaction was sent from.
699+
:ivar Permissions app_permissions: Bitwise set of permissions the bot has within the channel the interaction was sent from.
700700
"""
701701

702702
async def edit(self, content: Optional[str] = MISSING, **kwargs) -> Message:

0 commit comments

Comments
 (0)