Skip to content

Commit 6b3b3ac

Browse files
style: format with ruff, fix typehints, save magic values as constants
1 parent 41d23e2 commit 6b3b3ac

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

botstrap.py

Lines changed: 19 additions & 16 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,9 +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(self, channel_name_: str, category_id_: int | str | None = None) -> int:
108+
def create_forum_channel(self, channel_name_: str, category_id_: int | str | None = None) -> str:
102109
"""Creates a new forum channel."""
103-
payload = {"name": channel_name_, "type": GUILD_FORUM_TYPE}
110+
payload: dict[str, Any] = {"name": channel_name_, "type": GUILD_FORUM_TYPE}
104111
if category_id_:
105112
payload["parent_id"] = category_id_
106113

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

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

122-
def get_all_roles(self) -> dict:
129+
def get_all_roles(self) -> dict[str, int]:
123130
"""Fetches all the roles in a guild."""
124131
result = SilencedDict(name="Roles dictionary")
125132

@@ -149,7 +156,7 @@ def get_all_channels_and_categories(self) -> tuple[dict[str, str], dict[str, str
149156
name = f"off_topic_{off_topic_count}"
150157
off_topic_count += 1
151158

152-
if channel_type == 4:
159+
if channel_type == GUILD_CATEGORY_TYPE:
153160
categories[name] = channel["id"]
154161
else:
155162
channels[name] = channel["id"]
@@ -159,7 +166,7 @@ def get_all_channels_and_categories(self) -> tuple[dict[str, str], dict[str, str
159166
def webhook_exists(self, webhook_id_: int) -> bool:
160167
"""A predicate that indicates whether a webhook exists already or not."""
161168
try:
162-
self.get(f"webhooks/{webhook_id_}")
169+
self.get(f"/webhooks/{webhook_id_}")
163170
return True
164171
except HTTPStatusError:
165172
return False
@@ -168,7 +175,7 @@ def create_webhook(self, name: str, channel_id_: int) -> str:
168175
"""Creates a new webhook for a particular channel."""
169176
payload = {"name": name}
170177

171-
response = self.post(f"channels/{channel_id_}/webhooks", json=payload)
178+
response = self.post(f"/channels/{channel_id_}/webhooks", json=payload)
172179
new_webhook = response.json()
173180
return new_webhook["id"]
174181

@@ -195,16 +202,12 @@ def create_webhook(self, name: str, channel_id_: int) -> str:
195202

196203
discord_client.upgrade_server_to_community_if_necessary(rules_channel_id, announcements_channel_id)
197204

198-
create_help_channel = True
199-
200-
if PYTHON_HELP_CHANNEL_NAME in all_channels:
201-
python_help_channel_id = all_channels[PYTHON_HELP_CHANNEL_NAME]
205+
if python_help_channel_id := all_channels.get(PYTHON_HELP_CHANNEL_NAME):
202206
if not discord_client.is_forum_channel(python_help_channel_id):
203207
discord_client.delete_channel(python_help_channel_id)
204-
else:
205-
create_help_channel = False
208+
python_help_channel_id = None
206209

207-
if create_help_channel:
210+
if not python_help_channel_id:
208211
python_help_channel_name = PYTHON_HELP_CHANNEL_NAME.replace("_", "-")
209212
python_help_category_id = all_categories[PYTHON_HELP_CATEGORY_NAME]
210213
python_help_channel_id = discord_client.create_forum_channel(python_help_channel_name, python_help_category_id)

0 commit comments

Comments
 (0)