From 3f8683de31d6c6235049ff1f4aac0ba8101a0520 Mon Sep 17 00:00:00 2001 From: Wasi Master <63045920+wasi-master@users.noreply.github.com> Date: Sun, 29 Aug 2021 23:04:12 +0600 Subject: [PATCH] Add support for voice channel parties Basically the same thing as https://www.npmjs.com/package/discord-together and https://pypi.org/project/discord-together/ --- discord/channel.py | 56 +++++++++++++++++++++++++++++++++++++++++++++- discord/enums.py | 8 +++++++ discord/http.py | 11 +++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/discord/channel.py b/discord/channel.py index 25e06dc2..5f3f4f44 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -27,7 +27,7 @@ import asyncio import discord.abc from .permissions import Permissions -from .enums import ChannelType, try_enum, VoiceRegion +from .enums import ChannelType, try_enum, VoiceRegion, PartyType from .mixins import Hashable from . import utils from .asset import Asset @@ -734,6 +734,37 @@ class VoiceChannel(VocalGuildChannel): await self._edit(options, reason=reason) + async def create_party(self, application_id: PartyType, max_age: int = 86400 , max_uses: int = 0): + """|coro| + Creates a party in this voice channel. + + .. versionadded:: 1.7.3.8 + + Parameters + ---------- + application_id : PartyType + The id of the application the party belongs to. currently any of + ``PartyType.youtube``, ``PartyType.poker``, ``PartyType.betrayal``, ``PartyType.fishing``, ``PartyType.chess``. + max_age : int, optional + Duration in seconds after which the invite expires, by default 1 day. + max_uses : int, optional + maximum number of times this invite can be used, by default Unlimited. + + Raises + ------- + Forbidden + You do not have permissions to create a party. + HTTPException + Party creation failed. + + Returns + -------- + :class:`Party` + The created party. + """ + return Party(await self._state.http.create_party(self.id, application_id.value, max_age=max_age, max_uses=max_uses)) + + class StageChannel(VocalGuildChannel): """Represents a Discord guild stage channel. @@ -1461,6 +1492,29 @@ class GroupChannel(discord.abc.Messageable, Hashable): await self._state.http.leave_group(self.id) +class Party: + """Represents a party in a voice channel.""" + + __slots__ = ('code', 'uses', 'max_uses', 'max_age', 'temporary', 'created_at') + def __init__(self, data): + self.code = data['code'] + self.uses = data['uses'] + self.max_uses = data['max_uses'] + self.max_age = data['max_age'] + self.temporary = data['temporary'] + self.created_at = utils.parse_time(data.get('created_at')) + # TODO: add more fields here Such as guild, raw data: https://mystb.in/AdvertisersExperiencesMothers.json + + def __repr__(self): + return f'' + + def __str__(self): + return f'https://discord.gg/{self.code}' + + def __eq__(self, other): + return isinstance(other, Party) and self.code == other.code + + def _channel_factory(channel_type): value = try_enum(ChannelType, channel_type) if value is ChannelType.text: diff --git a/discord/enums.py b/discord/enums.py index 06727d8a..261a4361 100644 --- a/discord/enums.py +++ b/discord/enums.py @@ -30,6 +30,7 @@ __all__ = ( 'Enum', 'ChannelType', 'MessageType', + 'PartyType', 'VoiceRegion', 'SpeakingState', 'VerificationLevel', @@ -179,6 +180,13 @@ class MessageType(Enum): guild_discovery_grace_period_initial_warning = 16 guild_discovery_grace_period_final_warning = 17 +class PartyType(Enum): + youtube = 755600276941176913 + poker = 755827207812677713 + betrayal = 773336526917861400 + fishing = 814288819477020702 + chess = 832012774040141894 + class VoiceRegion(Enum): us_west = 'us-west' us_east = 'us-east' diff --git a/discord/http.py b/discord/http.py index 353410e4..5dddd357 100644 --- a/discord/http.py +++ b/discord/http.py @@ -701,6 +701,17 @@ class HTTPClient: def delete_channel(self, channel_id, *, reason=None): return self.request(Route('DELETE', '/channels/{channel_id}', channel_id=channel_id), reason=reason) + def create_party(self, channel_id, application_id, max_age: int, max_uses: int): + payload = { + 'max_age': max_age, + 'max_uses': max_uses, + 'target_application_id': application_id, + 'target_type': 2, + 'temporary': False, + 'validate': None + } + return self.request(Route("POST", "/channels/{channel_id}/invites", channel_id=channel_id), json=payload) + # Webhook management def create_webhook(self, channel_id, *, name, avatar=None, reason=None):