Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: Change type of permissions attrs to Permissions #1209

Merged
merged 2 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions interactions/api/models/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from ...utils.attrs_utils import (
ClientSerializerMixin,
DictSerializerMixin,
convert_int,
convert_list,
define,
field,
Expand Down Expand Up @@ -433,7 +434,7 @@ class Channel(ClientSerializerMixin, IDMixin):
:ivar Optional[ThreadMetadata] thread_metadata: The thread metadata of the channel.
:ivar Optional[ThreadMember] member: The member of the thread in the channel.
:ivar Optional[int] default_auto_archive_duration: The set auto-archive time for all threads to naturally follow in the channel.
:ivar Optional[str] permissions: The permissions of the channel.
:ivar Optional[Permissions] permissions: The permissions of the channel.
:ivar Optional[int] flags: The flags of the channel.
:ivar Optional[int] total_message_sent: Number of messages ever sent in a thread.
:ivar Optional[int] default_thread_slowmode_delay: The default slowmode delay in seconds for threads, if this channel is a forum.
Expand Down Expand Up @@ -484,7 +485,9 @@ class Channel(ClientSerializerMixin, IDMixin):
converter=ThreadMember, default=None, add_client=True, repr=False
)
default_auto_archive_duration: Optional[int] = field(default=None)
permissions: Optional[str] = field(default=None, repr=False)
permissions: Optional[Permissions] = field(
converter=convert_int(Permissions), default=None, repr=False
)
flags: Optional[int] = field(default=None, repr=False)
total_message_sent: Optional[int] = field(default=None, repr=False)
default_thread_slowmode_delay: Optional[int] = field(default=None, repr=False)
Expand Down Expand Up @@ -2070,7 +2073,7 @@ async def add_permission_overwrite(
_id = int(id)
_type = type

if not _type:
if _type is MISSING:
raise LibraryException(12, "Please set the type of the overwrite!")

overwrites.append(Overwrite(id=_id, type=_type, allow=allow, deny=deny))
Expand Down
21 changes: 13 additions & 8 deletions interactions/api/models/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ...utils.attrs_utils import (
ClientSerializerMixin,
DictSerializerMixin,
convert_int,
convert_list,
define,
field,
Expand All @@ -29,6 +30,7 @@
from .audit_log import AuditLogEvents, AuditLogs
from .channel import Channel, ChannelType, Thread, ThreadMember
from .emoji import Emoji
from .flags import Permissions
from .member import Member
from .message import Sticker, StickerPack
from .misc import (
Expand Down Expand Up @@ -349,7 +351,7 @@ class Guild(ClientSerializerMixin, IDMixin):
:ivar Optional[str] discovery_splash: The discovery splash banner of the guild.
:ivar Optional[bool] owner: Whether the guild is owned.
:ivar Snowflake owner_id: The ID of the owner of the guild.
:ivar Optional[str] permissions: The permissions of the guild.
:ivar Optional[Permissions] permissions: The permissions of the guild.
:ivar Optional[str] region: The geographical region of the guild.
:ivar Optional[Snowflake] afk_channel_id: The AFK voice channel of the guild.
:ivar int afk_timeout: The timeout of the AFK voice channel of the guild.
Expand Down Expand Up @@ -400,7 +402,9 @@ class Guild(ClientSerializerMixin, IDMixin):
discovery_splash: Optional[str] = field(default=None, repr=False)
owner: Optional[bool] = field(default=None)
owner_id: Snowflake = field(converter=Snowflake, default=None)
permissions: Optional[str] = field(default=None, repr=False)
permissions: Optional[Permissions] = field(
converter=convert_int(Permissions), default=None, repr=False
)
region: Optional[str] = field(default=None, repr=False) # None, we don't do Voices.
afk_channel_id: Optional[Snowflake] = field(converter=Snowflake, default=None)
afk_timeout: Optional[int] = field(default=None)
Expand Down Expand Up @@ -703,7 +707,7 @@ async def remove_member_role(
async def create_role(
self,
name: str,
permissions: Optional[int] = MISSING,
permissions: Optional[Union[Permissions, int]] = MISSING,
color: Optional[int] = 0,
hoist: Optional[bool] = False,
icon: Optional[Image] = MISSING,
Expand All @@ -718,7 +722,7 @@ async def create_role(

:param str name: The name of the role
:param Optional[int] color: RGB color value as integer, default ``0``
:param Optional[int] permissions: Bitwise value of the enabled/disabled permissions
:param Optional[Union[Permissions, int]] permissions: Bitwise value of the enabled/disabled permissions
:param Optional[bool] hoist: Whether the role should be displayed separately in the sidebar, default ``False``
:param Optional[Image] icon: The role's icon image (if the guild has the ROLE_ICONS feature)
:param Optional[str] unicode_emoji: The role's unicode emoji as a standard emoji (if the guild has the ROLE_ICONS feature)
Expand All @@ -729,7 +733,8 @@ async def create_role(
"""
if not self._client:
raise LibraryException(code=13)
_permissions = permissions if permissions is not MISSING else None

_permissions = int(permissions) if permissions is not MISSING else None
_icon = icon if icon is not MISSING else None
_unicode_emoji = unicode_emoji if unicode_emoji is not MISSING else None
payload = dict(
Expand Down Expand Up @@ -839,7 +844,7 @@ async def modify_role(
self,
role_id: Union[int, Snowflake, Role],
name: Optional[str] = MISSING,
permissions: Optional[int] = MISSING,
permissions: Optional[Union[Permissions, int]] = MISSING,
color: Optional[int] = MISSING,
hoist: Optional[bool] = MISSING,
icon: Optional[Image] = MISSING,
Expand All @@ -855,7 +860,7 @@ async def modify_role(
:param Union[int, Snowflake, Role] role_id: The id of the role to edit
:param Optional[str] name: The name of the role, defaults to the current value of the role
:param Optional[int] color: RGB color value as integer, defaults to the current value of the role
:param Optional[int] permissions: Bitwise value of the enabled/disabled permissions, defaults to the current value of the role
:param Optional[Union[Permissions, int]] permissions: Bitwise value of the enabled/disabled permissions, defaults to the current value of the role
:param Optional[bool] hoist: Whether the role should be displayed separately in the sidebar, defaults to the current value of the role
: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
: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
Expand All @@ -876,7 +881,7 @@ async def modify_role(
_color = role.color if color is MISSING else color
_hoist = role.hoist if hoist is MISSING else hoist
_mentionable = role.mentionable if mentionable is MISSING else mentionable
_permissions = role.permissions if permissions is MISSING else permissions
_permissions = int(role.permissions if permissions is MISSING else permissions)
_icon = role.icon if icon is MISSING else icon
_unicode_emoji = role.unicode_emoji if unicode_emoji is MISSING else unicode_emoji

Expand Down
6 changes: 3 additions & 3 deletions interactions/api/models/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,9 @@ class Message(ClientSerializerMixin, IDMixin):
position: Optional[int] = field(default=None, repr=False)

def __attrs_post_init__(self):
if self.referenced_message is not None:
self.referenced_message = Message(**self.referenced_message, _client=self._client)

if self.member and self.guild_id:
self.member._extras["guild_id"] = self.guild_id

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

if self.referenced_message is not None:
self.referenced_message = Message(**self.referenced_message, _client=self._client)

async def get_channel(self) -> Channel:
"""
.. versionadded:: 4.0.2
Expand Down
10 changes: 5 additions & 5 deletions interactions/api/models/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import List, Optional, Union

from ...base import get_logger
from ...utils.attrs_utils import DictSerializerMixin, convert_list, define, field
from ...utils.attrs_utils import DictSerializerMixin, convert_int, convert_list, define, field
from ...utils.missing import MISSING
from ..error import LibraryException
from .flags import Permissions
Expand Down Expand Up @@ -48,14 +48,14 @@ class Overwrite(DictSerializerMixin):

:ivar str id: Role or User ID
:ivar int type: Type that corresponds ot the ID; 0 for role and 1 for member.
:ivar Union[Permissions, int, str] allow: Permission bit set.
:ivar Union[Permissions, int, str] deny: Permission bit set.
:ivar Permissions allow: Permission bit set.
:ivar Permissions deny: Permission bit set.
"""

id: int = field()
type: int = field()
allow: Union[Permissions, int, str] = field()
deny: Union[Permissions, int, str] = field()
allow: Permissions = field(converter=convert_int(Permissions))
deny: Permissions = field(converter=convert_int(Permissions))


@define()
Expand Down
19 changes: 13 additions & 6 deletions interactions/api/models/role.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from datetime import datetime
from typing import TYPE_CHECKING, List, Optional, Union

from ...utils.attrs_utils import ClientSerializerMixin, DictSerializerMixin, define, field
from ...utils.attrs_utils import (
ClientSerializerMixin,
DictSerializerMixin,
convert_int,
define,
field,
)
from ...utils.missing import MISSING
from ..error import LibraryException
from .flags import Permissions
from .misc import IDMixin, Image, Snowflake

if TYPE_CHECKING:
Expand Down Expand Up @@ -52,7 +59,7 @@ class Role(ClientSerializerMixin, IDMixin):
:ivar Optional[str] icon: Role icon hash, if any.
:ivar Optional[str] unicode_emoji: Role unicode emoji
:ivar int position: Role position
:ivar str permissions: Role permissions as a bit set
:ivar Permissions permissions: Role permissions as a bit set
:ivar bool managed: A status denoting if this role is managed by an integration
:ivar bool mentionable: A status denoting if this role is mentionable
:ivar Optional[RoleTags] tags: The tags this role has
Expand All @@ -65,7 +72,7 @@ class Role(ClientSerializerMixin, IDMixin):
icon: Optional[str] = field(default=None, repr=False)
unicode_emoji: Optional[str] = field(default=None)
position: int = field()
permissions: str = field()
permissions: Permissions = field(converter=convert_int(Permissions))
managed: bool = field()
mentionable: bool = field()
tags: Optional[RoleTags] = field(converter=RoleTags, default=None)
Expand Down Expand Up @@ -117,7 +124,7 @@ async def modify(
self,
guild_id: Union[int, Snowflake, "Guild"],
name: Optional[str] = MISSING,
permissions: Optional[int] = MISSING,
permissions: Optional[Union[Permissions, int]] = MISSING,
color: Optional[int] = MISSING,
hoist: Optional[bool] = MISSING,
icon: Optional[Image] = MISSING,
Expand All @@ -133,7 +140,7 @@ async def modify(
:param int guild_id: The id of the guild to edit the role on
:param Optional[str] name: The name of the role, defaults to the current value of the role
:param Optional[int] color: RGB color value as integer, defaults to the current value of the role
:param Optional[int] permissions: Bitwise value of the enabled/disabled permissions, defaults to the current value of the role
:param Optional[Union[Permissions, int]] permissions: Bitwise value of the enabled/disabled permissions, defaults to the current value of the role
:param Optional[bool] hoist: Whether the role should be displayed separately in the sidebar, defaults to the current value of the role
: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
: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
Expand All @@ -148,7 +155,7 @@ async def modify(
_color = self.color if color is MISSING else color
_hoist = self.hoist if hoist is MISSING else hoist
_mentionable = self.mentionable if mentionable is MISSING else mentionable
_permissions = self.permissions if permissions is MISSING else permissions
_permissions = int(self.permissions if permissions is MISSING else permissions)
_icon = self.icon if icon is MISSING else icon
_unicode_emoji = self.unicode_emoji if unicode_emoji is MISSING else unicode_emoji
_guild_id = int(guild_id) if isinstance(guild_id, (int, Snowflake)) else int(guild_id.id)
Expand Down
4 changes: 2 additions & 2 deletions interactions/client/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ class CommandContext(_Context):
:ivar bool deferred: Whether the response was deferred or not.
:ivar Optional[Locale] locale: The selected language of the user invoking the interaction.
:ivar Optional[Locale] guild_locale: The guild's preferred language, if invoked in a guild.
:ivar str app_permissions: Bitwise set of permissions the bot has within the channel the interaction was sent from.
:ivar Permissions app_permissions: Bitwise set of permissions the bot has within the channel the interaction was sent from.
:ivar Client client:
.. versionadded:: 4.3.0

Expand Down Expand Up @@ -696,7 +696,7 @@ class ComponentContext(_Context):
:ivar bool deferred: Whether the response was deferred or not.
:ivar Optional[Locale] locale: The selected language of the user invoking the interaction.
:ivar Optional[Locale] guild_locale: The guild's preferred language, if invoked in a guild.
:ivar str app_permissions: Bitwise set of permissions the bot has within the channel the interaction was sent from.
:ivar Permissions app_permissions: Bitwise set of permissions the bot has within the channel the interaction was sent from.
"""

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