diff options
| author | 2023-05-17 12:23:29 +0530 | |
|---|---|---|
| committer | 2023-05-17 12:23:29 +0530 | |
| commit | 7876744fedab9fbb22a160c8950ee22262570270 (patch) | |
| tree | f7189b5f3e9c0947d71f86461f151e50092f6e2c /bot/utils/messages.py | |
| parent | nit (diff) | |
| parent | Bump sentry-sdk from 1.22.2 to 1.23.0 (#1277) (diff) | |
Merge branch 'main' into undeprecate-bookmark
Diffstat (limited to '')
| -rw-r--r-- | bot/utils/messages.py | 25 | 
1 files changed, 11 insertions, 14 deletions
diff --git a/bot/utils/messages.py b/bot/utils/messages.py index b0c95583..4fb0b39b 100644 --- a/bot/utils/messages.py +++ b/bot/utils/messages.py @@ -1,6 +1,7 @@ +import contextlib  import logging  import re -from typing import Callable, Optional, Union +from collections.abc import Callable  from discord import Embed, Message  from discord.ext import commands @@ -9,39 +10,35 @@ from discord.ext.commands import Context, MessageConverter  log = logging.getLogger(__name__) -def sub_clyde(username: Optional[str]) -> Optional[str]: +def sub_clyde(username: str | None) -> str | None:      """ -    Replace "e"/"E" in any "clyde" in `username` with a Cyrillic "е"/"E" and return the new string. +    Replace "e"/"E" in any "clyde" in `username` with a Cyrillic "е"/"Е" and return the new string.      Discord disallows "clyde" anywhere in the username for webhooks. It will return a 400.      Return None only if `username` is None. -    """ +    """  # noqa: RUF002      def replace_e(match: re.Match) -> str: -        char = "е" if match[2] == "e" else "Е" +        char = "е" if match[2] == "e" else "Е"  # noqa: RUF001          return match[1] + char      if username:          return re.sub(r"(clyd)(e)", replace_e, username, flags=re.I) -    else: -        return username  # Empty string or None +    return username  # Empty string or None -async def get_discord_message(ctx: Context, text: str) -> Union[Message, str]: +async def get_discord_message(ctx: Context, text: str) -> Message | str:      """      Attempts to convert a given `text` to a discord Message object and return it.      Conversion will succeed if given a discord Message ID or link.      Returns `text` if the conversion fails.      """ -    try: +    with contextlib.suppress(commands.BadArgument):          text = await MessageConverter().convert(ctx, text) -    except commands.BadArgument: -        pass -      return text -async def get_text_and_embed(ctx: Context, text: str) -> tuple[str, Optional[Embed]]: +async def get_text_and_embed(ctx: Context, text: str) -> tuple[str, Embed | None]:      """      Attempts to extract the text and embed from a possible link to a discord Message. @@ -52,7 +49,7 @@ async def get_text_and_embed(ctx: Context, text: str) -> tuple[str, Optional[Emb          str: If `text` is a valid discord Message, the contents of the message, else `text`.          Optional[Embed]: The embed if found in the valid Message, else None      """ -    embed: Optional[Embed] = None +    embed: Embed | None = None      msg = await get_discord_message(ctx, text)      # Ensure the user has read permissions for the channel the message is in  |