Skip to content

Commit 774b934

Browse files
authored
Add support for guest invites
1 parent 59546a4 commit 774b934

File tree

9 files changed

+111
-7
lines changed

9 files changed

+111
-7
lines changed

discord/abc.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
from .voice_client import VoiceClient, VoiceProtocol
6161
from .sticker import GuildSticker, StickerItem
6262
from . import utils
63+
from .flags import InviteFlags
6364

6465
__all__ = (
6566
'Snowflake',
@@ -1257,6 +1258,7 @@ async def create_invite(
12571258
target_type: Optional[InviteTarget] = None,
12581259
target_user: Optional[User] = None,
12591260
target_application_id: Optional[int] = None,
1261+
guest: bool = False,
12601262
) -> Invite:
12611263
"""|coro|
12621264
@@ -1295,6 +1297,10 @@ async def create_invite(
12951297
The id of the embedded application for the invite, required if ``target_type`` is :attr:`.InviteTarget.embedded_application`.
12961298
12971299
.. versionadded:: 2.0
1300+
guest: :class:`bool`
1301+
Whether the invite is a guest invite.
1302+
1303+
.. versionadded:: 2.6
12981304
12991305
Raises
13001306
-------
@@ -1312,6 +1318,11 @@ async def create_invite(
13121318
if target_type is InviteTarget.unknown:
13131319
raise ValueError('Cannot create invite with an unknown target type')
13141320

1321+
flags: Optional[InviteFlags] = None
1322+
if guest:
1323+
flags = InviteFlags._from_value(0)
1324+
flags.guest = True
1325+
13151326
data = await self._state.http.create_invite(
13161327
self.id,
13171328
reason=reason,
@@ -1322,6 +1333,7 @@ async def create_invite(
13221333
target_type=target_type.value if target_type else None,
13231334
target_user_id=target_user.id if target_user else None,
13241335
target_application_id=target_application_id,
1336+
flags=flags.value if flags else None,
13251337
)
13261338
return Invite.from_incomplete(data=data, state=self._state)
13271339

discord/audit_logs.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ def _transform_applied_forum_tags(entry: AuditLogEntry, data: List[Snowflake]) -
145145
return [Object(id=tag_id, type=ForumTag) for tag_id in data]
146146

147147

148-
def _transform_overloaded_flags(entry: AuditLogEntry, data: int) -> Union[int, flags.ChannelFlags]:
149-
# The `flags` key is definitely overloaded. Right now it's for channels and threads but
148+
def _transform_overloaded_flags(entry: AuditLogEntry, data: int) -> Union[int, flags.ChannelFlags, flags.InviteFlags]:
149+
# The `flags` key is definitely overloaded. Right now it's for channels, threads and invites but
150150
# I am aware of `member.flags` and `user.flags` existing. However, this does not impact audit logs
151151
# at the moment but better safe than sorry.
152152
channel_audit_log_types = (
@@ -157,9 +157,16 @@ def _transform_overloaded_flags(entry: AuditLogEntry, data: int) -> Union[int, f
157157
enums.AuditLogAction.thread_update,
158158
enums.AuditLogAction.thread_delete,
159159
)
160+
invite_audit_log_types = (
161+
enums.AuditLogAction.invite_create,
162+
enums.AuditLogAction.invite_update,
163+
enums.AuditLogAction.invite_delete,
164+
)
160165

161166
if entry.action in channel_audit_log_types:
162167
return flags.ChannelFlags._from_value(data)
168+
elif entry.action in invite_audit_log_types:
169+
return flags.InviteFlags._from_value(data)
163170
return data
164171

165172

discord/flags.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
'AppInstallationType',
6565
'SKUFlags',
6666
'EmbedFlags',
67+
'InviteFlags',
6768
)
6869

6970
BF = TypeVar('BF', bound='BaseFlags')
@@ -2397,3 +2398,59 @@ def content_inventory_entry(self):
23972398
longer displayed.
23982399
"""
23992400
return 1 << 5
2401+
2402+
2403+
class InviteFlags(BaseFlags):
2404+
r"""Wraps up the Discord Invite flags
2405+
2406+
.. versionadded:: 2.6
2407+
2408+
.. container:: operations
2409+
2410+
.. describe:: x == y
2411+
2412+
Checks if two InviteFlags are equal.
2413+
2414+
.. describe:: x != y
2415+
2416+
Checks if two InviteFlags are not equal.
2417+
2418+
.. describe:: x | y, x |= y
2419+
2420+
Returns a InviteFlags instance with all enabled flags from
2421+
both x and y.
2422+
2423+
.. describe:: x ^ y, x ^= y
2424+
2425+
Returns a InviteFlags instance with only flags enabled on
2426+
only one of x or y, not on both.
2427+
2428+
.. describe:: ~x
2429+
2430+
Returns a InviteFlags instance with all flags inverted from x.
2431+
2432+
.. describe:: hash(x)
2433+
2434+
Returns the flag's hash.
2435+
2436+
.. describe:: iter(x)
2437+
2438+
Returns an iterator of ``(name, value)`` pairs. This allows it
2439+
to be, for example, constructed as a dict or a list of pairs.
2440+
Note that aliases are not shown.
2441+
2442+
.. describe:: bool(b)
2443+
2444+
Returns whether any flag is set to ``True``.
2445+
2446+
Attributes
2447+
----------
2448+
value: :class:`int`
2449+
The raw value. You should query flags via the properties
2450+
rather than using this raw value.
2451+
"""
2452+
2453+
@flag_value
2454+
def guest(self):
2455+
""":class:`bool`: Returns ``True`` if this is a guest invite for a voice channel."""
2456+
return 1 << 0

discord/http.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,7 @@ def create_invite(
18341834
target_type: Optional[invite.InviteTargetType] = None,
18351835
target_user_id: Optional[Snowflake] = None,
18361836
target_application_id: Optional[Snowflake] = None,
1837+
flags: Optional[int] = None,
18371838
) -> Response[invite.Invite]:
18381839
r = Route('POST', '/channels/{channel_id}/invites', channel_id=channel_id)
18391840
payload = {
@@ -1852,6 +1853,9 @@ def create_invite(
18521853
if target_application_id:
18531854
payload['target_application_id'] = str(target_application_id)
18541855

1856+
if flags:
1857+
payload['flags'] = flags
1858+
18551859
return self.request(r, reason=reason, json=payload)
18561860

18571861
def get_invite(

discord/invite.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from .enums import ChannelType, NSFWLevel, VerificationLevel, InviteTarget, InviteType, try_enum
3333
from .appinfo import PartialAppInfo
3434
from .scheduled_event import ScheduledEvent
35+
from .flags import InviteFlags
3536

3637
__all__ = (
3738
'PartialInviteChannel',
@@ -379,6 +380,7 @@ class Invite(Hashable):
379380
'scheduled_event',
380381
'scheduled_event_id',
381382
'type',
383+
'_flags',
382384
)
383385

384386
BASE = 'https://discord.gg'
@@ -432,6 +434,7 @@ def __init__(
432434
else None
433435
)
434436
self.scheduled_event_id: Optional[int] = self.scheduled_event.id if self.scheduled_event else None
437+
self._flags: int = data.get('flags', 0)
435438

436439
@classmethod
437440
def from_incomplete(cls, *, state: ConnectionState, data: InvitePayload) -> Self:
@@ -523,6 +526,14 @@ def url(self) -> str:
523526
url += '?event=' + str(self.scheduled_event_id)
524527
return url
525528

529+
@property
530+
def flags(self) -> InviteFlags:
531+
""":class:`InviteFlags`: Returns the flags for this invite.
532+
533+
.. versionadded:: 2.6
534+
"""
535+
return InviteFlags._from_value(self._flags)
536+
526537
def set_scheduled_event(self, scheduled_event: Snowflake, /) -> Self:
527538
"""Sets the scheduled event for this invite.
528539

discord/member.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ class Member(discord.abc.Messageable, _UserTag):
238238
----------
239239
joined_at: Optional[:class:`datetime.datetime`]
240240
An aware datetime object that specifies the date and time in UTC that the member joined the guild.
241-
If the member left and rejoined the guild, this will be the latest date. In certain cases, this can be ``None``.
241+
If the member left and rejoined the guild, this will be the latest date.
242+
This can be ``None``, such as when the member is a guest.
242243
activities: Tuple[Union[:class:`BaseActivity`, :class:`Spotify`]]
243244
The activities that the user is currently doing.
244245

discord/types/invite.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Invite(IncompleteInvite, total=False):
6565
target_application: PartialAppInfo
6666
guild_scheduled_event: GuildScheduledEvent
6767
type: InviteType
68+
flags: NotRequired[int]
6869

6970

7071
class InviteWithCounts(Invite, _GuildPreviewUnique):
@@ -84,6 +85,7 @@ class GatewayInviteCreate(TypedDict):
8485
target_type: NotRequired[InviteTargetType]
8586
target_user: NotRequired[PartialUser]
8687
target_application: NotRequired[PartialAppInfo]
88+
flags: NotRequired[int]
8789

8890

8991
class GatewayInviteDelete(TypedDict):

discord/types/member.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Nickname(TypedDict):
3434

3535
class PartialMember(TypedDict):
3636
roles: SnowflakeList
37-
joined_at: str
37+
joined_at: Optional[str] # null if guest
3838
deaf: bool
3939
mute: bool
4040
flags: int

docs/api.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,6 +2517,7 @@ of :class:`enum.Enum`.
25172517
- :attr:`~AuditLogDiff.channel`
25182518
- :attr:`~AuditLogDiff.uses`
25192519
- :attr:`~AuditLogDiff.max_uses`
2520+
- :attr:`~AuditLogDiff.flags`
25202521

25212522
.. attribute:: invite_update
25222523

@@ -2541,6 +2542,7 @@ of :class:`enum.Enum`.
25412542
- :attr:`~AuditLogDiff.channel`
25422543
- :attr:`~AuditLogDiff.uses`
25432544
- :attr:`~AuditLogDiff.max_uses`
2545+
- :attr:`~AuditLogDiff.flags`
25442546

25452547
.. attribute:: webhook_create
25462548

@@ -4552,11 +4554,11 @@ AuditLogDiff
45524554

45534555
.. attribute:: flags
45544556

4555-
The channel flags associated with this thread or forum post.
4557+
The flags associated with this thread, forum post or invite.
45564558

4557-
See also :attr:`ForumChannel.flags` and :attr:`Thread.flags`
4559+
See also :attr:`ForumChannel.flags`, :attr:`Thread.flags` and :attr:`Invite.flags`
45584560

4559-
:type: :class:`ChannelFlags`
4561+
:type: Union[:class:`ChannelFlags`, :class:`InviteFlags`]
45604562

45614563
.. attribute:: default_thread_slowmode_delay
45624564

@@ -5734,6 +5736,14 @@ EmbedFlags
57345736
.. autoclass:: EmbedFlags()
57355737
:members:
57365738

5739+
InviteFlags
5740+
~~~~~~~~~~~~~~~~
5741+
5742+
.. attributetable:: InviteFlags
5743+
5744+
.. autoclass:: InviteFlags()
5745+
:members:
5746+
57375747
ForumTag
57385748
~~~~~~~~~
57395749

0 commit comments

Comments
 (0)