diff options
Diffstat (limited to 'botstrap.py')
-rw-r--r-- | botstrap.py | 74 |
1 files changed, 73 insertions, 1 deletions
diff --git a/botstrap.py b/botstrap.py index 90c2c2fbc..b77b98f1b 100644 --- a/botstrap.py +++ b/botstrap.py @@ -15,6 +15,12 @@ env_file_path = Path(".env.server") BOT_TOKEN = os.getenv("BOT_TOKEN", None) GUILD_ID = os.getenv("GUILD_ID", None) +COMMUNITY_FEATURE = "COMMUNITY" +PYTHON_HELP_CHANNEL_NAME = "python_help" +ANNOUNCEMENTS_CHANNEL_NAME = "announcements" +RULES_CHANNEL_NAME = "rules" +GUILD_FORUM_TYPE = 15 + if not BOT_TOKEN: message = ( @@ -40,7 +46,7 @@ class DiscordClient(Client): super().__init__( base_url="https://discord.com/api/v10", headers={"Authorization": f"Bot {BOT_TOKEN}"}, - event_hooks={"response": [self._raise_for_status]} + event_hooks={"response": [self._raise_for_status]}, ) @staticmethod @@ -48,6 +54,53 @@ class DiscordClient(Client): response.raise_for_status() +def upgrade_server_to_community_if_necessary( + guild_id: int | str, + rules_channel_id_: int | str, + announcements_channel_id_: int | str, + client: DiscordClient) -> None: + """Fetches server info & upgrades to COMMUNITY if necessary.""" + response = client.get(f"/guilds/{guild_id}") + payload = response.json() + + if COMMUNITY_FEATURE not in payload["features"]: + log.warning("This server is currently not a community, upgrading.") + payload["features"].append(COMMUNITY_FEATURE) + payload["rules_channel_id"] = rules_channel_id_ + payload["public_updates_channel_id"] = announcements_channel_id_ + client.patch(f"/guilds/{guild_id}", json=payload) + log.info(f"Server {guild_id} has been successfully updated to a community.") + + +def create_forum_channel( + channel_name_: str, + guild_id: str, + client: DiscordClient, + category_id_: int | None = None +) -> int: + """Creates a new forum channel.""" + payload = {"name": channel_name_, "type": GUILD_FORUM_TYPE} + if category_id_: + payload["parent_id"] = category_id_ + + response = client.post(f"/guilds/{guild_id}/channels", json=payload) + forum_channel_id = response.json()["id"] + log.info(f"New forum channel: {channel_name_} has been successfully created.") + return forum_channel_id + + +def is_forum_channel(channel_id_: str, client: DiscordClient) -> bool: + """A boolean that indicates if a channel is of type GUILD_FORUM.""" + response = client.get(f"/channels/{channel_id_}") + return response.json()["type"] == GUILD_FORUM_TYPE + + +def delete_channel(channel_id_: id, client: DiscordClient) -> None: + """Upgrades a channel to a channel of type GUILD FORUM.""" + log.info(f"Channel python-help: {channel_id_} is not a forum channel and will be replaced with one.") + client.delete(f"/channels/{channel_id_}") + + def get_all_roles(guild_id: int | str, client: DiscordClient) -> dict: """Fetches all the roles in a guild.""" result = {} @@ -126,6 +179,24 @@ with DiscordClient() as discord_client: config_str += "\n#Channels\n" + rules_channel_id = all_channels[RULES_CHANNEL_NAME] + announcements_channel_id = all_channels[ANNOUNCEMENTS_CHANNEL_NAME] + + upgrade_server_to_community_if_necessary(GUILD_ID, rules_channel_id, announcements_channel_id, discord_client) + + create_help_channel = True + + if PYTHON_HELP_CHANNEL_NAME in all_channels: + python_help_channel_id = all_channels[PYTHON_HELP_CHANNEL_NAME] + if not is_forum_channel(python_help_channel_id, discord_client): + delete_channel(python_help_channel_id, discord_client) + else: + create_help_channel = False + + if create_help_channel: + python_help_channel_name = PYTHON_HELP_CHANNEL_NAME.replace('_', '-') + python_help_channel_id = create_forum_channel(python_help_channel_name, GUILD_ID, discord_client) + for channel_name in _Channels.__fields__: channel_id = all_channels.get(channel_name, None) if not channel_id: @@ -135,6 +206,7 @@ with DiscordClient() as discord_client: continue config_str += f"channels_{channel_name}={channel_id}\n" + config_str += f"channels_{PYTHON_HELP_CHANNEL_NAME}={python_help_channel_id}\n" config_str += "\n#Categories\n" |