Skip to content

Commit 03e1093

Browse files
panley01Damego
andauthored
feat: new IntFlag objects for User & Application flags (#459)
* Add user & bot flags to flags.py * changed some user flag names * Added flags.py to user.pyi * added flag properties to user object * removed unused appflags * Added flag checking before starting gateway * added AppFlags * added appflags * abstracted flags to application object * Accounted for LIMITED priv intent flags * Fix typo Co-authored-by: Damego <[email protected]> Co-authored-by: Damego <[email protected]>
1 parent 13c6043 commit 03e1093

File tree

6 files changed

+70
-13
lines changed

6 files changed

+70
-13
lines changed

interactions/api/models/flags.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,36 @@ class Permissions(IntFlag):
8585
SEND_MESSAGES_IN_THREADS = 1 << 38
8686
START_EMBEDDED_ACTIVITIES = 1 << 39
8787
MODERATE_MEMBERS = 1 << 40
88+
89+
90+
class UserFlags(IntFlag):
91+
"""An integer flag bitshift object representing the different user flags given by Discord."""
92+
93+
STAFF = 1
94+
PARTNER = 1 << 1
95+
HYPESQUAD = 1 << 2
96+
BUG_HUNTER_LEVEL_1 = 1 << 3
97+
HYPESQUAD_HOUSE_1 = 1 << 6
98+
HYPESQUAD_HOUSE_2 = 1 << 7
99+
HYPESQUAD_HOUSE_3 = 1 << 8
100+
PREMIUM_EARLY_SUPPORTER = 1 << 9
101+
TEAM_PSEUDO_USER = 1 << 10
102+
SYSTEM = 1 << 12
103+
BUG_HUNTER_LEVEL_2 = 1 << 14
104+
VERIFIED_BOT = 1 << 16
105+
VERIFIED_DEVELOPER = 1 << 17
106+
DISCORD_CERTIFIED_MODERATOR = 1 << 18
107+
BOT_HTTP_INTERACTIONS = 1 << 19
108+
109+
class AppFlags(IntFlag):
110+
"""An integer flag bitshift object representing the different application flags given by Discord."""
111+
112+
GATEWAY_PRESENCE = 1 << 12
113+
GATEWAY_PRESENCE_LIMITED = 1 << 13
114+
GATEWAY_GUILD_MEMBERS = 1 << 14
115+
GATEWAY_GUILD_MEMBERS_LIMITED = 1 << 15
116+
VERIFICATION_PENDING_GUILD_LIMIT = 1 << 16
117+
EMBEDDED = 1 << 17
118+
GATEWAY_MESSAGE_CONTENT = 1 << 18
119+
GATEWAY_MESSAGE_CONTENT_LIMITED = 1 << 19
120+

interactions/api/models/team.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .channel import ThreadMember
22
from .misc import DictSerializerMixin, Snowflake
33
from .user import User
4+
from .flags import AppFlags
45

56

67
class TeamMember(DictSerializerMixin):
@@ -75,7 +76,7 @@ class Application(DictSerializerMixin):
7576
:ivar Optional[int] primary_sku_id?: Game SKU ID, if this app is a game sold on Discord
7677
:ivar Optional[str] slug?: URL slug that links to the store page, if this app is a game sold on Discord
7778
:ivar Optional[str] cover_image?: The app's default rich presence invite cover image
78-
:ivar Optional[int] flags?: The application's public flags
79+
:ivar Optional[AppFlags] flags?: The application's public flags
7980
"""
8081

8182
__slots__ = (
@@ -115,3 +116,4 @@ def __init__(self, **kwargs):
115116
)
116117
self.owner = User(**self.owner) if self._json.get("owner") else None
117118
self.team = Team(**self.team) if self._json.get("team") else None
119+
self.flags = AppFlags(self.flags) if self._json.get("flags") else None

interactions/api/models/team.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ from typing import Any, List, Optional
22

33
from .misc import DictSerializerMixin, Snowflake
44
from .user import User
5+
from .flags import AppFlags
56

67
class TeamMember(DictSerializerMixin):
78
_json: dict
@@ -39,7 +40,7 @@ class Application(DictSerializerMixin):
3940
primary_sku_id: Optional[Snowflake]
4041
slug: Optional[str]
4142
cover_image: Optional[str]
42-
flags: Optional[int]
43+
flags: Optional[AppFlags]
4344
type: Optional[Any]
4445
hook: Optional[Any]
4546
def __init__(self, **kwargs): ...

interactions/api/models/user.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .misc import DictSerializerMixin, Snowflake
2+
from .flags import UserFlags
23

34

45
class User(DictSerializerMixin):
@@ -8,18 +9,18 @@ class User(DictSerializerMixin):
89
:ivar Snowflake id: The User ID
910
:ivar str username: The Username associated (not necessarily unique across the platform)
1011
:ivar str discriminator: The User's 4-digit discord-tag (i.e.: XXXX)
11-
:ivar Optional[str] avatar?: The user's avatar hash, if any.
12-
:ivar Optional[bool] bot?: A status denoting if the user is a bot.
13-
:ivar Optional[bool] system?: A status denoting if the user is an Official Discord System user.
14-
:ivar Optional[bool] mfa_enabled?: A status denoting if the user has 2fa on their account.
15-
:ivar Optional[str] banner?: The user's banner hash, if any.
12+
:ivar Optional[str] avatar?: The user's avatar hash, if any
13+
:ivar Optional[bool] bot?: A status denoting if the user is a bot
14+
:ivar Optional[bool] system?: A status denoting if the user is an Official Discord System user
15+
:ivar Optional[bool] mfa_enabled?: A status denoting if the user has 2fa on their account
16+
:ivar Optional[str] banner?: The user's banner hash, if any
1617
:ivar Optional[int] accent_color?: The user's banner color as an integer represented of hex color codes
1718
:ivar Optional[str] locale?: The user's chosen language option
1819
:ivar Optional[bool] verified?: Whether the email associated with this account has been verified
19-
:ivar Optional[str] email?: The user's email, if any.
20-
:ivar Optional[int] flags?: The user's flags
20+
:ivar Optional[str] email?: The user's email, if any
21+
:ivar Optional[UserFlags] flags?: The user's flags
2122
:ivar Optional[int] premium_type?: The type of Nitro subscription the user has
22-
:ivar Optional[int] public_flags?: The user's public flags
23+
:ivar Optional[UserFlags] public_flags?: The user's public flags
2324
"""
2425

2526
__slots__ = (
@@ -44,3 +45,15 @@ class User(DictSerializerMixin):
4445
def __init__(self, **kwargs):
4546
super().__init__(**kwargs)
4647
self.id = Snowflake(self.id) if self._json.get("id") else None
48+
49+
self.public_flags = (
50+
UserFlags(int(self._json.get("public_flags")))
51+
if self._json.get("public_flags")
52+
else None
53+
)
54+
55+
self.flags = (
56+
UserFlags(int(self._json.get("flags")))
57+
if self._json.get("flags")
58+
else None
59+
)

interactions/api/models/user.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional
22

33
from .misc import DictSerializerMixin, Snowflake
4+
from .flags import UserFlags, AppFlags
45

56
class User(DictSerializerMixin):
67
_json: dict
@@ -16,7 +17,7 @@ class User(DictSerializerMixin):
1617
locale: Optional[str]
1718
verified: Optional[bool]
1819
email: Optional[str]
19-
flags: Optional[int]
20+
flags: Optional[UserFlags]
2021
premium_type: Optional[int]
21-
public_flags: Optional[int]
22+
public_flags: Optional[UserFlags]
2223
def __init__(self, **kwargs): ...

interactions/client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ def __init__(
9191
)
9292
else:
9393
self._automate_sync = True
94-
94+
9595
data = self._loop.run_until_complete(self._http.get_current_bot_information())
9696
self.me = Application(**data)
97+
9798

9899
def start(self) -> None:
99100
"""Starts the client session."""
@@ -256,6 +257,12 @@ async def _ready(self) -> None:
256257
ready: bool = False
257258

258259
try:
260+
if self._intents.GUILD_PRESENCES and not (self.me.flags.GATEWAY_PRESENCE or self.me.flags.GATEWAY_PRESENCE_LIMITED):
261+
raise RuntimeError("Client not authorised for GUILD_PRESENCES intent")
262+
if self._intents.GUILD_MEMBERS and not (self.me.flags.GATEWAY_GUILD_MEMBERS or self.me.flags.GATEWAY_GUILD_MEMBERS_LIMITED):
263+
raise RuntimeError("Client not authorised for GUILD_MEMBERS intent")
264+
if self._intents.GUILD_MESSAGES and not (self.me.flags.GATEWAY_MESSAGE_CONTENT or self.me.flags.GATEWAY_MESSAGE_CONTENT_LIMITED):
265+
log.critical("Client not authorised for MESSAGE_CONTENT intent")
259266
self.__register_events()
260267
if self._automate_sync:
261268
await self._synchronize()

0 commit comments

Comments
 (0)