Skip to content

Commit 333625c

Browse files
upgrade application flags if intents weren't enabled
1 parent 150333d commit 333625c

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

botstrap.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import re
44
import sys
55
from pathlib import Path
6-
from typing import Any
6+
from typing import Any, cast
77

88
from dotenv import load_dotenv
99
from httpx import Client, HTTPStatusError, Response
@@ -83,11 +83,37 @@ def __init__(self, guild_id: int | str):
8383
event_hooks={"response": [self._raise_for_status]},
8484
)
8585
self.guild_id = guild_id
86+
self._app_info: dict[str, Any] | None = None
8687

8788
@staticmethod
8889
def _raise_for_status(response: Response) -> None:
8990
response.raise_for_status()
9091

92+
@property
93+
def app_info(self) -> dict[str, Any]:
94+
"""Fetches the application's information."""
95+
if self._app_info is None:
96+
response = self.get("/applications/@me")
97+
self._app_info = cast("dict[str, Any]", response.json())
98+
return self._app_info
99+
100+
def upgrade_application_flags_if_necessary(self) -> bool:
101+
"""
102+
Set the app's flags to allow the intents that we need.
103+
104+
Returns a boolean defining whether changes were made.
105+
"""
106+
# Fetch first in the event the user has already set some flags
107+
current_flags = self.app_info.get("flags", 0)
108+
new_flags = current_flags | 1 << 15 | 1 << 19
109+
110+
if new_flags != current_flags:
111+
resp = self.patch("/applications/@me", json={"flags": new_flags})
112+
self._app_info = cast("dict[str, Any]", resp.json())
113+
return True
114+
115+
return False
116+
91117
def upgrade_server_to_community_if_necessary(
92118
self,
93119
rules_channel_id_: int | str,
@@ -181,6 +207,9 @@ def create_webhook(self, name: str, channel_id_: int) -> str:
181207

182208

183209
with DiscordClient(guild_id=GUILD_ID) as discord_client:
210+
if discord_client.upgrade_application_flags_if_necessary():
211+
log.info("Application flags upgraded successfully, and necessary intents are now enabled.")
212+
184213
config_str = "#Roles\n"
185214

186215
all_roles = discord_client.get_all_roles()

0 commit comments

Comments
 (0)