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

feat: application role connections #1791

Merged
merged 41 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f52b491
feat: start of application role connection
plun1331 Nov 21, 2022
9554237
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 21, 2022
d4f6cb1
Merge branch 'master' into role-connections
Lulalaby Nov 22, 2022
b3bb0a6
Apply suggestions from code review
Lulalaby Nov 22, 2022
3626180
Merge branch 'master' into role-connections
plun1331 Nov 23, 2022
8459bf9
Merge branch 'master' into role-connections
plun1331 Nov 23, 2022
ba791d1
fix: type imports
plun1331 Nov 23, 2022
b6573dd
Merge branch 'master' into role-connections
Lulalaby Nov 30, 2022
bafba6c
Merge branch 'master' into role-connections
plun1331 Dec 1, 2022
929048f
Update http.py
plun1331 Dec 9, 2022
7ff181e
Update application_role_connection.py
plun1331 Dec 9, 2022
36e131f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 9, 2022
ed91252
Update application_role_connection.py
plun1331 Dec 9, 2022
2ac88db
Update client.py
plun1331 Dec 9, 2022
02b63ea
Merge branch 'master' into role-connections
plun1331 Dec 9, 2022
36b13f0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 9, 2022
f1dcb85
Update CHANGELOG.md
plun1331 Dec 9, 2022
91993cc
Update CHANGELOG.md
plun1331 Dec 9, 2022
d77cf7e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 9, 2022
c15acd0
Update application_role_connection.py
plun1331 Dec 9, 2022
7f29c79
Update data_classes.rst
plun1331 Dec 9, 2022
5388ac5
Update enums.py
plun1331 Dec 9, 2022
aaddc94
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 9, 2022
04c626d
Update enums.rst
plun1331 Dec 9, 2022
3f64daa
Update application_role_connection.py
plun1331 Dec 9, 2022
0b3da5f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 9, 2022
df20619
Update client.py
plun1331 Dec 9, 2022
f03eade
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 9, 2022
40666dc
Update __init__.py
plun1331 Dec 9, 2022
e61d06d
Update __init__.py
plun1331 Dec 9, 2022
52bfcea
Update application_role_connection.py
plun1331 Dec 9, 2022
0274845
Merge branch 'master' into role-connections
Lulalaby Dec 12, 2022
2fcc9a0
Merge branch 'master' into role-connections
Lulalaby Dec 20, 2022
aca3f76
Merge branch 'master' into role-connections
Lulalaby Jan 5, 2023
bc6adc1
Update CHANGELOG.md
Lulalaby Jan 5, 2023
808c262
Merge branch 'master' into role-connections
Lulalaby Jan 5, 2023
0446ceb
Merge branch 'master' into role-connections
Lulalaby Jan 17, 2023
efb2ddb
Merge branch 'master' into role-connections
Lulalaby Jan 26, 2023
8d84668
Merge branch 'master' into role-connections
plun1331 Jan 30, 2023
fd391da
feat: add __repr__ and __str__
plun1331 Jan 30, 2023
69fbaf2
Merge branch 'master' into role-connections
plun1331 Feb 7, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ These changes are available on the `master` branch, but have not yet been releas
([#1809](https://github.com/Pycord-Development/pycord/pull/1809))
- Added missing `image` parameter to `Guild.create_scheduled_event()` method.
([#1831](https://github.com/Pycord-Development/pycord/pull/1831))
- New `ApplicationRoleConnectionMetadata` class for application role connection
metadata, along with the `fetch_role_connection_metadata_records` and
`update_role_connection_metadata_records` methods in `Client`.
([#1791](https://github.com/Pycord-Development/pycord/pull/1791))
- Added new message types, `interaction_premium_upsell`, `stage_start`, `stage_end`,
`stage_speaker`, `stage_raise_hand`, `stage_topic`, and
`guild_application_premium_subscription`.
Expand Down
1 change: 1 addition & 0 deletions discord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from . import abc, opus, sinks, ui, utils
from .activity import *
from .appinfo import *
from .application_role_connection import *
from .asset import *
from .audit_logs import *
from .automod import *
Expand Down
128 changes: 128 additions & 0 deletions discord/application_role_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
"""
The MIT License (MIT)

Copyright (c) 2021-present Pycord Development

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
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 __future__ import annotations

from typing import TYPE_CHECKING

from .enums import ApplicationRoleConnectionMetadataType, try_enum
from .utils import MISSING

if TYPE_CHECKING:
from .types.application_role_connection import (
ApplicationRoleConnectionMetadata as ApplicationRoleConnectionMetadataPayload,
)

__all__ = ("ApplicationRoleConnectionMetadata",)


class ApplicationRoleConnectionMetadata:
r"""Represents role connection metadata for a Discord application.

.. versionadded:: 2.4

Parameters
----------
type: :class:`ApplicationRoleConnectionMetadataType`
The type of metadata value.
key: :class:`str`
The key for this metadata field.
May only be the ``a-z``, ``0-9``, or ``_`` characters, with a maximum of 50 characters.
name: :class:`str`
The name for this metadata field. Maximum 100 characters.
description: :class:`str`
The description for this metadata field. Maximum 200 characters.
name_localizations: Optional[Dict[:class:`str`, :class:`str`]]
The name localizations for this metadata field. The values of this should be ``"locale": "name"``.
See `here <https://discord.com/developers/docs/reference#locales>`_ for a list of valid locales.
description_localizations: Optional[Dict[:class:`str`, :class:`str`]]
The description localizations for this metadata field. The values of this should be ``"locale": "name"``.
See `here <https://discord.com/developers/docs/reference#locales>`_ for a list of valid locales.
"""

__slots__ = (
"type",
"key",
"name",
"description",
"name_localizations",
"description_localizations",
)

def __init__(
self,
*,
type: ApplicationRoleConnectionMetadataType,
key: str,
name: str,
description: str,
name_localizations: dict[str, str] = MISSING,
description_localizations: dict[str, str] = MISSING,
):
self.type: ApplicationRoleConnectionMetadataType = type
self.key: str = key
self.name: str = name
self.name_localizations: dict[str, str] = name_localizations
self.description: str = description
self.description_localizations: dict[str, str] = description_localizations

def __repr__(self):
return (
f"<ApplicationRoleConnectionMetadata "
f"type={self.type!r} "
f"key={self.key!r} "
f"name={self.name!r} "
f"description={self.description!r} "
f"name_localizations={self.name_localizations!r} "
f"description_localizations={self.description_localizations!r}>"
)

def __str__(self):
return self.name

@classmethod
def from_dict(
cls, data: ApplicationRoleConnectionMetadataPayload
) -> ApplicationRoleConnectionMetadata:
return cls(
type=try_enum(ApplicationRoleConnectionMetadataType, data["type"]),
key=data["key"],
name=data["name"],
description=data["description"],
name_localizations=data.get("name_localizations"),
description_localizations=data.get("description_localizations"),
)

def to_dict(self) -> ApplicationRoleConnectionMetadataPayload:
data = {
"type": self.type.value,
"key": self.key,
"name": self.name,
"description": self.description,
}
if self.name_localizations is not MISSING:
data["name_localizations"] = self.name_localizations
if self.description_localizations is not MISSING:
data["description_localizations"] = self.description_localizations
return data
45 changes: 45 additions & 0 deletions discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from . import utils
from .activity import ActivityTypes, BaseActivity, create_activity
from .appinfo import AppInfo, PartialAppInfo
from .application_role_connection import ApplicationRoleConnectionMetadata
from .backoff import ExponentialBackoff
from .channel import PartialMessageable, _threaded_channel_factory
from .emoji import Emoji
Expand Down Expand Up @@ -1782,3 +1783,47 @@ def persistent_views(self) -> Sequence[View]:
.. versionadded:: 2.0
"""
return self._connection.persistent_views

async def fetch_role_connection_metadata_records(
self,
) -> list[ApplicationRoleConnectionMetadata]:
"""|coro|

Fetches the bot's role connection metadata records.

.. versionadded:: 2.4

Returns
-------
List[:class:`.ApplicationRoleConnectionMetadata`]
The bot's role connection metadata records.
"""
data = await self._connection.http.get_application_role_connection_metadata_records(
self.application_id
)
return [ApplicationRoleConnectionMetadata.from_dict(r) for r in data]

async def update_role_connection_metadata_records(
self, *role_connection_metadata
) -> list[ApplicationRoleConnectionMetadata]:
"""|coro|

Updates the bot's role connection metadata records.

.. versionadded:: 2.4

Parameters
----------
*role_connection_metadata: :class:`ApplicationRoleConnectionMetadata`
The new metadata records to send to Discord.

Returns
-------
List[:class:`.ApplicationRoleConnectionMetadata`]
The updated role connection metadata records.
"""
payload = [r.to_dict() for r in role_connection_metadata]
data = await self._connection.http.update_application_role_connection_metadata_records(
self.application_id, payload
)
return [ApplicationRoleConnectionMetadata.from_dict(r) for r in data]
14 changes: 14 additions & 0 deletions discord/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"AutoModEventType",
"AutoModActionType",
"AutoModKeywordPresetType",
"ApplicationRoleConnectionMetadataType",
)


Expand Down Expand Up @@ -930,6 +931,19 @@ class AutoModKeywordPresetType(Enum):
slurs = 3


class ApplicationRoleConnectionMetadataType(Enum):
"""Application role connection metadata type"""

integer_less_than_or_equal = 1
integer_greater_than_or_equal = 2
integer_equal = 3
integer_not_equal = 4
datetime_less_than_or_equal = 5
datetime_greater_than_or_equal = 6
boolean_equal = 7
boolean_not_equal = 8


T = TypeVar("T")


Expand Down
26 changes: 26 additions & 0 deletions discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
from .file import File
from .types import (
appinfo,
application_role_connection,
audit_log,
automod,
channel,
Expand Down Expand Up @@ -2818,6 +2819,31 @@ def bulk_edit_guild_application_command_permissions(
)
return self.request(r, json=payload)

# Application Role Connections

def get_application_role_connection_metadata_records(
self,
application_id: Snowflake,
) -> Response[list[application_role_connection.ApplicationRoleConnectionMetadata]]:
r = Route(
"GET",
"/applications/{application_id}/role-connections/metadata",
application_id=application_id,
)
return self.request(r)

def update_application_role_connection_metadata_records(
self,
application_id: Snowflake,
payload: list[application_role_connection.ApplicationRoleConnectionMetadata],
) -> Response[list[application_role_connection.ApplicationRoleConnectionMetadata]]:
r = Route(
"PUT",
"/applications/{application_id}/role-connections/metadata",
application_id=application_id,
)
return self.request(r, json=payload)

# Misc

def application_info(self) -> Response[appinfo.AppInfo]:
Expand Down
40 changes: 40 additions & 0 deletions discord/types/application_role_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
The MIT License (MIT)

Copyright (c) 2021-present Pycord Development

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
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 __future__ import annotations

from typing import Literal

from .._typed_dict import NotRequired, TypedDict

ApplicationRoleConnectionMetadataType = Literal[1, 2, 3, 4, 5, 6, 7, 8]


class ApplicationRoleConnectionMetadata(TypedDict):
type: ApplicationRoleConnectionMetadataType
key: str
name: str
name_localizations: NotRequired[dict[str, str]]
description: str
description_localizations: NotRequired[dict[str, str]]
8 changes: 8 additions & 0 deletions docs/api/data_classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,11 @@ Permissions

.. autoclass:: PermissionOverwrite
:members:

Application Role Connections
-----------------------------

.. attributetable:: ApplicationRoleConnectionMetadata

.. autoclass:: ApplicationRoleConnectionMetadata
:members:
45 changes: 45 additions & 0 deletions docs/api/enums.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,51 @@ of :class:`enum.Enum`.

Represents a scheduled event that is only available to members inside the guild.

.. class:: ApplicationRoleConnectionMetadataType

Represents an application role connection metadata type.

Each metadata type offers a comparison operation that allows guilds to
configure role requirements based on metadata values stored by the bot.
Bots specify a ``metadata value`` for each user and guilds specify the
required ``guild's configured value`` within the guild role settings.

.. versionadded:: 2.4

.. attribute:: integer_less_than_or_equal

The metadata value (``integer``) is less than or equal to the guild's configured value (``integer``).

.. attribute:: integer_greater_than_or_equal

The metadata value (``integer``) is greater than or equal to the guild's configured value (``integer``).

.. attribute:: integer_equal

The metadata value (``integer``) is equal to the guild's configured value (``integer``).

.. attribute:: integer_not_equal

The metadata value (``integer``) is not equal to the guild's configured value (``integer``).

.. attribute:: datetime_less_than_or_equal

The metadata value (``datetime``) is less than or equal to the guild's configured value
(``integer``; the number of days before the current date).

.. attribute:: datetime_greater_than_or_equal

The metadata value (``datetime``) is greater than or equal to the guild's configured value
(``integer``; the number of days before the current date).

.. attribute:: boolean_equal

The metadata value (``integer``) is equal to the guild's configured value (``integer``; 1).

.. attribute:: boolean_not_equal

The metadata value (``integer``) is not equal to the guild's configured value (``integer``; 1).

.. class:: AutoModTriggerType

Represents an AutoMod trigger type.
Expand Down