diff options
author | 2022-09-18 22:03:34 +0100 | |
---|---|---|
committer | 2022-09-18 22:03:34 +0100 | |
commit | 46d1e8ddb217f1bb5e07179b32db50b6a04b6de8 (patch) | |
tree | c3f4db63c2751c51acfee97d016551b8f677e29b /bot/utils | |
parent | Remove unnecessary hasattr check (diff) | |
parent | Fix Poetry 1.2 Support (#1099) (diff) |
Merge branch 'main' into fix-whitelist-inheritance
Diffstat (limited to 'bot/utils')
-rw-r--r-- | bot/utils/checks.py | 2 | ||||
-rw-r--r-- | bot/utils/commands.py | 11 | ||||
-rw-r--r-- | bot/utils/decorators.py | 4 | ||||
-rw-r--r-- | bot/utils/messages.py | 72 |
4 files changed, 85 insertions, 4 deletions
diff --git a/bot/utils/checks.py b/bot/utils/checks.py index 8c426ed7..5433f436 100644 --- a/bot/utils/checks.py +++ b/bot/utils/checks.py @@ -33,7 +33,7 @@ def in_whitelist_check( channels: Container[int] = (), categories: Container[int] = (), roles: Container[int] = (), - redirect: Optional[int] = constants.Channels.community_bot_commands, + redirect: Optional[int] = constants.Channels.sir_lancebot_playground, fail_silently: bool = False, ) -> bool: """ diff --git a/bot/utils/commands.py b/bot/utils/commands.py new file mode 100644 index 00000000..7c04a25a --- /dev/null +++ b/bot/utils/commands.py @@ -0,0 +1,11 @@ +from typing import Optional + +from rapidfuzz import process + + +def get_command_suggestions( + all_commands: list[str], query: str, *, cutoff: int = 60, limit: int = 3 +) -> Optional[list]: + """Get similar command names.""" + results = process.extract(query, all_commands, score_cutoff=cutoff, limit=limit) + return [result[0] for result in results] diff --git a/bot/utils/decorators.py b/bot/utils/decorators.py index 0061abd9..442eb841 100644 --- a/bot/utils/decorators.py +++ b/bot/utils/decorators.py @@ -272,10 +272,10 @@ def whitelist_check(**default_kwargs: Container[int]) -> Callable[[Context], boo channels = set(kwargs.get("channels") or {}) categories = kwargs.get("categories") - # Only output override channels + community_bot_commands + # Only output override channels + sir_lancebot_playground if channels: default_whitelist_channels = set(WHITELISTED_CHANNELS) - default_whitelist_channels.discard(Channels.community_bot_commands) + default_whitelist_channels.discard(Channels.sir_lancebot_playground) channels.difference_update(default_whitelist_channels) # Add all whitelisted category channels, but skip if we're in DMs diff --git a/bot/utils/messages.py b/bot/utils/messages.py index a6c035f9..b0c95583 100644 --- a/bot/utils/messages.py +++ b/bot/utils/messages.py @@ -1,5 +1,12 @@ +import logging import re -from typing import Optional +from typing import Callable, Optional, Union + +from discord import Embed, Message +from discord.ext import commands +from discord.ext.commands import Context, MessageConverter + +log = logging.getLogger(__name__) def sub_clyde(username: Optional[str]) -> Optional[str]: @@ -17,3 +24,66 @@ def sub_clyde(username: Optional[str]) -> Optional[str]: return re.sub(r"(clyd)(e)", replace_e, username, flags=re.I) else: return username # Empty string or None + + +async def get_discord_message(ctx: Context, text: str) -> Union[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: + 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]]: + """ + Attempts to extract the text and embed from a possible link to a discord Message. + + Does not retrieve the text and embed from the Message if it is in a channel the user does + not have read permissions in. + + Returns a tuple of: + 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 + + msg = await get_discord_message(ctx, text) + # Ensure the user has read permissions for the channel the message is in + if isinstance(msg, Message): + permissions = msg.channel.permissions_for(ctx.author) + if permissions.read_messages: + text = msg.clean_content + # Take first embed because we can't send multiple embeds + if msg.embeds: + embed = msg.embeds[0] + + return text, embed + + +def convert_embed(func: Callable[[str, ], str], embed: Embed) -> Embed: + """ + Converts the text in an embed using a given conversion function, then return the embed. + + Only modifies the following fields: title, description, footer, fields + """ + embed_dict = embed.to_dict() + + embed_dict["title"] = func(embed_dict.get("title", "")) + embed_dict["description"] = func(embed_dict.get("description", "")) + + if "footer" in embed_dict: + embed_dict["footer"]["text"] = func(embed_dict["footer"].get("text", "")) + + if "fields" in embed_dict: + for field in embed_dict["fields"]: + field["name"] = func(field.get("name", "")) + field["value"] = func(field.get("value", "")) + + return Embed.from_dict(embed_dict) |