Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions botstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ def __getitem__(self, item: str):
sys.exit(-1)


class BotstrapError(Exception):
"""Raised when an error occurs during the botstrap process."""


class DiscordClient(Client):
"""An HTTP client to communicate with Discord's APIs."""

Expand Down Expand Up @@ -167,17 +163,22 @@ def upgrade_server_to_community_if_necessary(
announcements_channel_id: int | str,
) -> bool:
"""Fetches server info & upgrades to COMMUNITY if necessary."""
payload = self.get_guild_info()

if COMMUNITY_FEATURE not in payload["features"]:
log.info("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
self._guild_info = self.patch(f"/guilds/{self.guild_id}", json=payload).json()
log.info("Server %s has been successfully updated to a community.", self.guild_id)
return True
return False
guild_info = self.get_guild_info()

if COMMUNITY_FEATURE in guild_info["features"]:
return False

log.info("This server is currently not a community, upgrading.")

payload = {
"features": guild_info["features"] + [COMMUNITY_FEATURE],
"rules_channel_id": rules_channel_id,
"public_updates_channel_id": announcements_channel_id,
}
self._guild_info = self.patch(f"/guilds/{self.guild_id}", json=payload).json()

log.info("Server %s has been successfully updated to a community.", self.guild_id)
return True

def get_all_roles(self) -> dict[str, int]:
"""Fetches all the roles in a guild."""
Expand Down Expand Up @@ -330,20 +331,32 @@ def check_guild_membership(self) -> None:
"""Check the bot is in the required guild."""
if not self.client.check_if_in_guild():
log.error("The bot is not a member of the configured guild with ID %s.", self.guild_id)
log.warning(
log.info(
"Please invite with the following URL and rerun this script: "
"https://discord.com/oauth2/authorize?client_id=%s&guild_id=%s&scope=bot+applications.commands&permissions=8",
self.client_id,
self.guild_id,
)
raise BotstrapError("Bot is not a member of the configured guild.")
sys.exit(1)

def upgrade_guild(self, announcements_channel_id: str, rules_channel_id: str) -> bool:
"""Upgrade the guild to a community if necessary."""
return self.client.upgrade_server_to_community_if_necessary(
rules_channel_id=rules_channel_id,
announcements_channel_id=announcements_channel_id,
)
try:
return self.client.upgrade_server_to_community_if_necessary(
rules_channel_id=rules_channel_id,
announcements_channel_id=announcements_channel_id,
)
except HTTPStatusError as e:
if e.response.status_code == 403:
log.error("Unable to upgrade to community guild as he bot does not have Administrator permissions")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.error("Unable to upgrade to community guild as he bot does not have Administrator permissions")
log.error("Unable to upgrade to community guild as the bot does not have Administrator permissions.")

log.info(
"Please re-invite with the following URL and rerun this script: "

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Please re-invite with the following URL and rerun this script: "
"Please re-invite with the following URL and run this script again: "

This wording might be a bit more understandable to non-native English speakers.

"https://discord.com/oauth2/authorize?client_id=%s&guild_id=%s&scope=bot+applications.commands&permissions=8",
self.client_id,
self.guild_id,
)
sys.exit(1)
raise

def get_roles(self) -> dict[str, Any]:
"""Get a config map of all of the roles in the guild."""
Expand Down