diff options
Diffstat (limited to 'bot/exts/utilities')
-rw-r--r-- | bot/exts/utilities/challenges.py | 2 | ||||
-rw-r--r-- | bot/exts/utilities/colour.py | 2 | ||||
-rw-r--r-- | bot/exts/utilities/emoji.py | 2 | ||||
-rw-r--r-- | bot/exts/utilities/reddit.py | 31 | ||||
-rw-r--r-- | bot/exts/utilities/wolfram.py | 4 |
5 files changed, 20 insertions, 21 deletions
diff --git a/bot/exts/utilities/challenges.py b/bot/exts/utilities/challenges.py index 2f9ac73e..6d1813bb 100644 --- a/bot/exts/utilities/challenges.py +++ b/bot/exts/utilities/challenges.py @@ -258,7 +258,7 @@ class Challenges(commands.Cog): @commands.command(aliases=["kata"]) @commands.cooldown(1, 5, commands.BucketType.user) - async def challenge(self, ctx: commands.Context, language: str = "python", *, query: str = None) -> None: + async def challenge(self, ctx: commands.Context, language: str = "python", *, query: str | None = None) -> None: """ The challenge command pulls a random kata (challenge) from codewars.com. diff --git a/bot/exts/utilities/colour.py b/bot/exts/utilities/colour.py index 95f9ac22..b0ff8747 100644 --- a/bot/exts/utilities/colour.py +++ b/bot/exts/utilities/colour.py @@ -241,7 +241,7 @@ class Colour(commands.Cog): choices=self.colour_mapping.values(), score_cutoff=80 ) - colour_name = [name for name, hex_code in self.colour_mapping.items() if hex_code == match][0] + colour_name = next(name for name, hex_code in self.colour_mapping.items() if hex_code == match) except TypeError: colour_name = None return colour_name diff --git a/bot/exts/utilities/emoji.py b/bot/exts/utilities/emoji.py index ce352fe2..bbaf6d25 100644 --- a/bot/exts/utilities/emoji.py +++ b/bot/exts/utilities/emoji.py @@ -78,7 +78,7 @@ class Emojis(commands.Cog): await self.bot.invoke_help_command(ctx) @emoji_group.command(name="count", aliases=("c",)) - async def count_command(self, ctx: commands.Context, *, category_query: str = None) -> None: + async def count_command(self, ctx: commands.Context, *, category_query: str | None = None) -> None: """Returns embed with emoji category and info given by the user.""" emoji_dict = defaultdict(list) diff --git a/bot/exts/utilities/reddit.py b/bot/exts/utilities/reddit.py index f8e358de..5dd4a377 100644 --- a/bot/exts/utilities/reddit.py +++ b/bot/exts/utilities/reddit.py @@ -20,16 +20,15 @@ from bot.utils.pagination import ImagePaginator, LinePaginator log = logging.getLogger(__name__) AccessToken = namedtuple("AccessToken", ["token", "expires_at"]) +HEADERS = {"User-Agent": "python3:python-discord/bot:1.0.0 (by /u/PythonDiscord)"} +URL = "https://www.reddit.com" +OAUTH_URL = "https://oauth.reddit.com" +MAX_RETRIES = 3 class Reddit(Cog): """Track subreddit posts and show detailed statistics about them.""" - HEADERS = {"User-Agent": "python3:python-discord/bot:1.0.0 (by /u/PythonDiscord)"} - URL = "https://www.reddit.com" - OAUTH_URL = "https://oauth.reddit.com" - MAX_RETRIES = 3 - def __init__(self, bot: Bot): self.bot = bot @@ -68,7 +67,7 @@ class Reddit(Cog): # Normal brackets interfere with Markdown. title = escape_markdown(title).replace("[", "⦋").replace("]", "⦌") - link = self.URL + data["permalink"] + link = URL + data["permalink"] first_page += f"**[{title.replace('*', '')}]({link})**\n" @@ -121,10 +120,10 @@ class Reddit(Cog): A token is valid for 1 hour. There will be MAX_RETRIES to get a token, after which the cog will be unloaded and a ClientError raised if retrieval was still unsuccessful. """ - for i in range(1, self.MAX_RETRIES + 1): + for i in range(1, MAX_RETRIES + 1): response = await self.bot.http_session.post( - url=f"{self.URL}/api/v1/access_token", - headers=self.HEADERS, + url=f"{URL}/api/v1/access_token", + headers=HEADERS, auth=self.client_auth, data={ "grant_type": "client_credentials", @@ -144,7 +143,7 @@ class Reddit(Cog): return log.debug( f"Failed to get an access token: status {response.status} & content type {response.content_type}; " - f"retrying ({i}/{self.MAX_RETRIES})" + f"retrying ({i}/{MAX_RETRIES})" ) await asyncio.sleep(3) @@ -159,8 +158,8 @@ class Reddit(Cog): For security reasons, it's good practice to revoke the token when it's no longer being used. """ response = await self.bot.http_session.post( - url=f"{self.URL}/api/v1/revoke_token", - headers=self.HEADERS, + url=f"{URL}/api/v1/revoke_token", + headers=HEADERS, auth=self.client_auth, data={ "token": self.access_token.token, @@ -173,7 +172,7 @@ class Reddit(Cog): else: log.warning(f"Unable to revoke access token: status {response.status}.") - async def fetch_posts(self, route: str, *, amount: int = 25, params: dict = None) -> list[dict]: + async def fetch_posts(self, route: str, *, amount: int = 25, params: dict | None = None) -> list[dict]: """A helper method to fetch a certain amount of Reddit posts at a given route.""" # Reddit's JSON responses only provide 25 posts at most. if not 25 >= amount > 0: @@ -183,11 +182,11 @@ class Reddit(Cog): if not self.access_token or self.access_token.expires_at < datetime.now(tz=UTC): await self.get_access_token() - url = f"{self.OAUTH_URL}/{route}" - for _ in range(self.MAX_RETRIES): + url = f"{OAUTH_URL}/{route}" + for _ in range(MAX_RETRIES): response = await self.bot.http_session.get( url=url, - headers={**self.HEADERS, "Authorization": f"bearer {self.access_token.token}"}, + headers=HEADERS | {"Authorization": f"bearer {self.access_token.token}"}, params=params ) if response.status == 200 and response.content_type == "application/json": diff --git a/bot/exts/utilities/wolfram.py b/bot/exts/utilities/wolfram.py index d5669c6b..e77573a7 100644 --- a/bot/exts/utilities/wolfram.py +++ b/bot/exts/utilities/wolfram.py @@ -33,8 +33,8 @@ async def send_embed( ctx: Context, message_txt: str, colour: int = Colours.soft_red, - footer: str = None, - img_url: str = None, + footer: str | None = None, + img_url: str | None = None, f: discord.File = None ) -> None: """Generate & send a response embed with Wolfram as the author.""" |