Skip to content

Commit

Permalink
feat: Add support for GET invite endpoint (#804)
Browse files Browse the repository at this point in the history
* feat: Add has_public_flag to User model

* uhh

* feat: Add GET Invite endpoint support (WIP)

* Remove stuff that shouldn't have been committed

* Update slotting

* Revert "Update slotting"

This reverts commit 66b0510.

* Small reformat and documenting

* Document new slots in Invite model

* Move delete_invite and add .pyi file for InviteRequest

* Fix typo and add InviteRequest to client.pyi

* Add url property to invite model

* Note: API is broken

* Update url property

* Remove accidental comment

* Remove small eyesore

* ci: correct from checks.

* Update interactions/api/http/invite.py

Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com>

* Update interactions/api/models/guild.py

Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com>

* ci: correct from checks.

* ci: correct from checks.

* Remove unused imports

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 1, 2022
1 parent 17a3d6d commit 9f7f310
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 11 deletions.
10 changes: 0 additions & 10 deletions interactions/api/http/channel.py
Expand Up @@ -180,16 +180,6 @@ async def create_channel_invite(
Route("POST", f"/channels/{channel_id}/invites"), json=payload, reason=reason
)

async def delete_invite(self, invite_code: str, reason: Optional[str] = None) -> dict:
"""
Delete an invite.
:param invite_code: The code of the invite to delete
:param reason: Reason to show in the audit log, if any.
:return: The deleted invite object
"""
return await self._req.request(Route("DELETE", f"/invites/{invite_code}"), reason=reason)

async def edit_channel_permission(
self,
channel_id: int,
Expand Down
1 change: 0 additions & 1 deletion interactions/api/http/channel.pyi
Expand Up @@ -39,7 +39,6 @@ class ChannelRequest:
async def create_channel_invite(
self, channel_id: int, payload: dict, reason: Optional[str] = None
) -> dict: ...
async def delete_invite(self, invite_code: str, reason: Optional[str] = None) -> dict: ...
async def edit_channel_permission(
self,
channel_id: int,
Expand Down
3 changes: 3 additions & 0 deletions interactions/api/http/client.py
Expand Up @@ -7,6 +7,7 @@
from .emoji import EmojiRequest
from .guild import GuildRequest
from .interaction import InteractionRequest
from .invite import InviteRequest
from .member import MemberRequest
from .message import MessageRequest
from .reaction import ReactionRequest
Expand All @@ -24,6 +25,7 @@ class HTTPClient(
EmojiRequest,
GuildRequest,
InteractionRequest,
InviteRequest,
MemberRequest,
MessageRequest,
ReactionRequest,
Expand Down Expand Up @@ -59,6 +61,7 @@ def __init__(self, token: str):
MessageRequest.__init__(self)
GuildRequest.__init__(self)
ChannelRequest.__init__(self)
InviteRequest.__init__(self)
ThreadRequest.__init__(self)
ReactionRequest.__init__(self)
StickerRequest.__init__(self)
Expand Down
2 changes: 2 additions & 0 deletions interactions/api/http/client.pyi
Expand Up @@ -5,6 +5,7 @@ from .channel import ChannelRequest
from .emoji import EmojiRequest
from .guild import GuildRequest
from .interaction import InteractionRequest
from .invite import InviteRequest
from .member import MemberRequest
from .message import MessageRequest
from .reaction import ReactionRequest
Expand All @@ -21,6 +22,7 @@ class HTTPClient(
EmojiRequest,
GuildRequest,
InteractionRequest,
InviteRequest,
MemberRequest,
MessageRequest,
ReactionRequest,
Expand Down
55 changes: 55 additions & 0 deletions interactions/api/http/invite.py
@@ -0,0 +1,55 @@
from typing import Optional

from ...api.cache import Cache
from .request import _Request
from .route import Route

__all__ = ["InviteRequest"]


class InviteRequest:
_req = _Request
cache: Cache

def __init__(self) -> None:
pass

async def get_invite(
self,
invite_code: str,
with_counts: bool = None,
with_expiration: bool = None,
guild_scheduled_event_id: int = None,
) -> dict:
"""
Gets an invite using its code.
.. note:: with_expiration is currently broken, the API will always return expiration_date.
:param invite_code: A string representing the invite code.
:param with_counts: Whether approximate_member_count and approximate_presence_count are returned.
:param with_expiration: Whether the invite's expiration is returned.
:param guild_scheduled_event_id: A guild scheduled event's ID.
"""
params_set = {
"with_counts=true" if with_counts else None,
"with_expiration=false" if not with_expiration else None,
f"guild_scheduled_event_id={guild_scheduled_event_id}"
if guild_scheduled_event_id
else None,
}
final = "&".join([item for item in params_set if item is not None])

return await self._req.request(
Route("GET", f"/invites/{invite_code}{'?' + final if final is not None else ''}")
)

async def delete_invite(self, invite_code: str, reason: Optional[str] = None) -> dict:
"""
Delete an invite.
:param invite_code: The code of the invite to delete
:param reason: Reason to show in the audit log, if any.
:return: The deleted invite object
"""
return await self._req.request(Route("DELETE", f"/invites/{invite_code}"), reason=reason)
18 changes: 18 additions & 0 deletions interactions/api/http/invite.pyi
@@ -0,0 +1,18 @@
from typing import Optional
from .request import _Request
from ...api.cache import Cache

class InviteRequest:

_req: _Request
cache: Cache

def __init__(self) -> None: ...
async def get_invite(
self,
invite_code: str,
with_counts: bool = None,
with_expiration: bool = None,
guild_scheduled_event_id: int = None
) -> dict: ...
async def delete_invite(self, invite_code: str, reason: Optional[str] = None) -> dict: ...
16 changes: 16 additions & 0 deletions interactions/api/models/guild.py
Expand Up @@ -2056,6 +2056,10 @@ class Invite(DictSerializerMixin):
:ivar Optional[int] target_type: The target type of this invite.
:ivar Optional[Guild] guild: The guild of this invite.
:ivar Optional[Channel] channel: The channel of this invite.
:ivar Optional[int] approximate_member_count: The approximate amount of total members in a guild.
:ivar Optional[int] approximate_presence_count: The aprpoximate amount of online members in a guild.
:ivar Optional[ScheduledEvents] guild_scheduled_event: A scheduled guild event object included in the invite.
"""

__slots__ = (
Expand All @@ -2077,6 +2081,9 @@ class Invite(DictSerializerMixin):
"target_type",
"guild",
"channel",
"approximate_member_count",
"approximate_presence_count",
"guild_scheduled_event",
)

def __init__(self, **kwargs):
Expand Down Expand Up @@ -2107,6 +2114,11 @@ def __init__(self, **kwargs):
if self._json.get("channel")
else None
)
self.guild_scheduled_event = (
ScheduledEvents(**self._json.get("guild_scheduled_event"))
if isinstance(self._json.get("guild_scheduled_event"), dict)
else self._json.get("guild_scheduled_event")
)

async def delete(self) -> None:
"""Deletes the invite"""
Expand All @@ -2116,6 +2128,10 @@ async def delete(self) -> None:

await self._client.delete_invite(self.code)

@property
def url(self):
return f"https://discord.gg/{self.code}" if self.code else None


class GuildTemplate(DictSerializerMixin):
"""
Expand Down

0 comments on commit 9f7f310

Please sign in to comment.