Skip to content

Commit 150333d

Browse files
style: format with ruff, fix typehints, save magic values as constants
1 parent 00c597e commit 150333d

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

botstrap.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@
33
import re
44
import sys
55
from pathlib import Path
6+
from typing import Any
67

78
from dotenv import load_dotenv
89
from httpx import Client, HTTPStatusError, Response
910

1011
# Filter out the send typing monkeypatch logs from bot core when we import to get constants
1112
logging.getLogger("pydis_core").setLevel(logging.WARNING)
1213

13-
from bot.constants import Webhooks, _Categories, _Channels, _Roles # noqa: E402
14+
from bot.constants import ( # noqa: E402
15+
Webhooks,
16+
_Categories, # pyright: ignore[reportPrivateUsage]
17+
_Channels, # pyright: ignore[reportPrivateUsage]
18+
_Roles, # pyright: ignore[reportPrivateUsage]
19+
)
1420
from bot.log import get_logger # noqa: E402
1521

1622
load_dotenv()
@@ -27,6 +33,7 @@
2733
PYTHON_HELP_CATEGORY_NAME = "python_help_system"
2834
ANNOUNCEMENTS_CHANNEL_NAME = "announcements"
2935
RULES_CHANNEL_NAME = "rules"
36+
GUILD_CATEGORY_TYPE = 4
3037
GUILD_FORUM_TYPE = 15
3138

3239
if not BOT_TOKEN:
@@ -46,7 +53,7 @@
4653
raise ValueError(message)
4754

4855

49-
class SilencedDict(dict):
56+
class SilencedDict(dict[str, Any]):
5057
"""A dictionary that silences KeyError exceptions upon subscription to non existent items."""
5158

5259
def __init__(self, name: str):
@@ -98,13 +105,9 @@ def upgrade_server_to_community_if_necessary(
98105
self.patch(f"/guilds/{self.guild_id}", json=payload)
99106
log.info(f"Server {self.guild_id} has been successfully updated to a community.")
100107

101-
def create_forum_channel(
102-
self,
103-
channel_name_: str,
104-
category_id_: int | str | None = None
105-
) -> int:
108+
def create_forum_channel(self, channel_name_: str, category_id_: int | str | None = None) -> str:
106109
"""Creates a new forum channel."""
107-
payload = {"name": channel_name_, "type": GUILD_FORUM_TYPE}
110+
payload: dict[str, Any] = {"name": channel_name_, "type": GUILD_FORUM_TYPE}
108111
if category_id_:
109112
payload["parent_id"] = category_id_
110113

@@ -118,12 +121,12 @@ def is_forum_channel(self, channel_id_: str) -> bool:
118121
response = self.get(f"/channels/{channel_id_}")
119122
return response.json()["type"] == GUILD_FORUM_TYPE
120123

121-
def delete_channel(self, channel_id_: id) -> None:
124+
def delete_channel(self, channel_id_: str | int) -> None:
122125
"""Delete a channel."""
123126
log.info(f"Channel python-help: {channel_id_} is not a forum channel and will be replaced with one.")
124127
self.delete(f"/channels/{channel_id_}")
125128

126-
def get_all_roles(self) -> dict:
129+
def get_all_roles(self) -> dict[str, int]:
127130
"""Fetches all the roles in a guild."""
128131
result = SilencedDict(name="Roles dictionary")
129132

@@ -153,7 +156,7 @@ def get_all_channels_and_categories(self) -> tuple[dict[str, str], dict[str, str
153156
name = f"off_topic_{off_topic_count}"
154157
off_topic_count += 1
155158

156-
if channel_type == 4:
159+
if channel_type == GUILD_CATEGORY_TYPE:
157160
categories[name] = channel["id"]
158161
else:
159162
channels[name] = channel["id"]
@@ -163,7 +166,7 @@ def get_all_channels_and_categories(self) -> tuple[dict[str, str], dict[str, str
163166
def webhook_exists(self, webhook_id_: int) -> bool:
164167
"""A predicate that indicates whether a webhook exists already or not."""
165168
try:
166-
self.get(f"webhooks/{webhook_id_}")
169+
self.get(f"/webhooks/{webhook_id_}")
167170
return True
168171
except HTTPStatusError:
169172
return False
@@ -172,7 +175,7 @@ def create_webhook(self, name: str, channel_id_: int) -> str:
172175
"""Creates a new webhook for a particular channel."""
173176
payload = {"name": name}
174177

175-
response = self.post(f"channels/{channel_id_}/webhooks", json=payload)
178+
response = self.post(f"/channels/{channel_id_}/webhooks", json=payload)
176179
new_webhook = response.json()
177180
return new_webhook["id"]
178181

@@ -183,7 +186,6 @@ def create_webhook(self, name: str, channel_id_: int) -> str:
183186
all_roles = discord_client.get_all_roles()
184187

185188
for role_name in _Roles.model_fields:
186-
187189
role_id = all_roles.get(role_name, None)
188190
if not role_id:
189191
log.warning(f"Couldn't find the role {role_name} in the guild, PyDis' default values will be used.")
@@ -200,16 +202,12 @@ def create_webhook(self, name: str, channel_id_: int) -> str:
200202

201203
discord_client.upgrade_server_to_community_if_necessary(rules_channel_id, announcements_channel_id)
202204

203-
create_help_channel = True
204-
205-
if PYTHON_HELP_CHANNEL_NAME in all_channels:
206-
python_help_channel_id = all_channels[PYTHON_HELP_CHANNEL_NAME]
205+
if python_help_channel_id := all_channels.get(PYTHON_HELP_CHANNEL_NAME):
207206
if not discord_client.is_forum_channel(python_help_channel_id):
208207
discord_client.delete_channel(python_help_channel_id)
209-
else:
210-
create_help_channel = False
208+
python_help_channel_id = None
211209

212-
if create_help_channel:
210+
if not python_help_channel_id:
213211
python_help_channel_name = PYTHON_HELP_CHANNEL_NAME.replace("_", "-")
214212
python_help_category_id = all_categories[PYTHON_HELP_CATEGORY_NAME]
215213
python_help_channel_id = discord_client.create_forum_channel(python_help_channel_name, python_help_category_id)
@@ -218,9 +216,7 @@ def create_webhook(self, name: str, channel_id_: int) -> str:
218216
for channel_name in _Channels.model_fields:
219217
channel_id = all_channels.get(channel_name, None)
220218
if not channel_id:
221-
log.warning(
222-
f"Couldn't find the channel {channel_name} in the guild, PyDis' default values will be used."
223-
)
219+
log.warning(f"Couldn't find the channel {channel_name} in the guild, PyDis' default values will be used.")
224220
continue
225221

226222
config_str += f"channels_{channel_name}={channel_id}\n"
@@ -231,9 +227,7 @@ def create_webhook(self, name: str, channel_id_: int) -> str:
231227
for category_name in _Categories.model_fields:
232228
category_id = all_categories.get(category_name, None)
233229
if not category_id:
234-
log.warning(
235-
f"Couldn't find the category {category_name} in the guild, PyDis' default values will be used."
236-
)
230+
log.warning(f"Couldn't find the category {category_name} in the guild, PyDis' default values will be used.")
237231
continue
238232

239233
config_str += f"categories_{category_name}={category_id}\n"

0 commit comments

Comments
 (0)