|
3 | 3 | import re |
4 | 4 | import sys |
5 | 5 | from pathlib import Path |
6 | | -from typing import Any |
| 6 | +from typing import Any, cast |
7 | 7 |
|
8 | 8 | from dotenv import load_dotenv |
9 | 9 | from httpx import Client, HTTPStatusError, Response |
@@ -83,11 +83,37 @@ def __init__(self, guild_id: int | str): |
83 | 83 | event_hooks={"response": [self._raise_for_status]}, |
84 | 84 | ) |
85 | 85 | self.guild_id = guild_id |
| 86 | + self._app_info: dict[str, Any] | None = None |
86 | 87 |
|
87 | 88 | @staticmethod |
88 | 89 | def _raise_for_status(response: Response) -> None: |
89 | 90 | response.raise_for_status() |
90 | 91 |
|
| 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 | + |
91 | 117 | def upgrade_server_to_community_if_necessary( |
92 | 118 | self, |
93 | 119 | rules_channel_id_: int | str, |
@@ -181,6 +207,9 @@ def create_webhook(self, name: str, channel_id_: int) -> str: |
181 | 207 |
|
182 | 208 |
|
183 | 209 | 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 | + |
184 | 213 | config_str = "#Roles\n" |
185 | 214 |
|
186 | 215 | all_roles = discord_client.get_all_roles() |
|
0 commit comments