Skip to content

Commit

Permalink
feat(guild, channel): implement new helper methods (#600)
Browse files Browse the repository at this point in the history
* fix!: fix EmbedImageStruct serialization

* fix!: Button emoji serialization

* fix!: message serialization in context

* feat: implement channel.get and channel.url

* feat: add InviteTargetType

* feat: add delete to headers

* fix!: allow @me for url property

* feat: get for guild

* Delete http.py

* Update channel.pyi

* Update guild.py

* Update guild.pyi
  • Loading branch information
EdVraz committed Mar 20, 2022
1 parent 29889fe commit e0cbb8a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
23 changes: 2 additions & 21 deletions interactions/api/models/channel.py
Expand Up @@ -857,29 +857,10 @@ async def create_thread(

return Channel(**res, _client=self._client)

@classmethod
async def get(
cls,
channel: Union[int, str],
client: "HTTPClient", # noqa
) -> "Channel":
"""
Gets a channel based of its URL or its id.
:param channel: The URL to the channel or the id of the channel
:type channel: Union[int, str]
:param client: The HTTPClient of your bot. Set as ``bot._http``
:type client: HTTPClient
"""

channel_id = channel if isinstance(channel, int) else int(channel.split(sep="/")[-1])

res = await client.get_channel(channel_id)
return cls(**res, _client=client)

@property
def url(self) -> str:
return f"https://discord.com/channels/{self.guild_id}/{self.id}" if self.guild_id else None
_guild_id = "@me" if not isinstance(self.guild_id, int) else self.guild_id
return f"https://discord.com/channels/{_guild_id}/{self.id}"

async def create_invite(
self,
Expand Down
13 changes: 13 additions & 0 deletions interactions/api/models/channel.pyi
Expand Up @@ -2,6 +2,7 @@ from datetime import datetime
from enum import IntEnum
from typing import List, Optional, Union, Callable

from .guild import Invite, InviteTargetType
from .message import Message, Embed, MessageInteraction
from ...models.component import ActionRow, Button, SelectMenu
from .misc import DictSerializerMixin, Overwrite, Snowflake, MISSING
Expand Down Expand Up @@ -190,5 +191,17 @@ class Channel(DictSerializerMixin):
message_id: Optional[int] = MISSING,
reason: Optional[str] = None,
) -> "Channel": ...
@property
def url(self) -> str: ...
async def create_invite(
self,
max_age: int = 86400,
max_uses: int = 0,
temporary: bool = False,
unique: bool = False,
target_type: InviteTargetType = MISSING,
target_user_id: int = MISSING,
target_application_id: int = MISSING,
) -> Invite: ...

class Thread(Channel): ...
15 changes: 15 additions & 0 deletions interactions/api/models/guild.py
Expand Up @@ -54,6 +54,13 @@ class EventStatus(IntEnum):
CANCELED = 4


class InviteTargetType(IntEnum):
"""An enumerable object representing the different invite target types"""

STREAM = 1
EMBEDDED_APPLICATION = 2


class WelcomeChannels(DictSerializerMixin):
"""
A class object representing a welcome channel on the welcome screen.
Expand Down Expand Up @@ -1755,6 +1762,14 @@ def __init__(self, **kwargs):
else None
)

async def delete(self) -> None:
"""Deletes the invite"""

if not self._client:
raise AttributeError("HTTPClient not found!")

await self._client.delete_invite(self.code)


class GuildTemplate(DictSerializerMixin):
"""
Expand Down
5 changes: 5 additions & 0 deletions interactions/api/models/guild.pyi
Expand Up @@ -38,6 +38,10 @@ class EventStatus(IntEnum):
COMPLETED: int
CANCELED: int

class InviteTargetType(IntEnum):
STREAM: int
EMBEDDED_APPLICATION: int

class WelcomeChannels(DictSerializerMixin):
_json: dict
channel_id: int
Expand Down Expand Up @@ -433,6 +437,7 @@ class Invite(DictSerializerMixin):
temporary: bool
created_at: datetime
def __init__(self, **kwargs): ...
async def delete(self) -> None: ...

class GuildTemplate(DictSerializerMixin):
_json: dict
Expand Down

0 comments on commit e0cbb8a

Please sign in to comment.