From 65e3743888d5b1744cdba031d160b525585e56e2 Mon Sep 17 00:00:00 2001 From: japandotorg Date: Tue, 25 Jun 2024 11:16:35 +0530 Subject: [PATCH] [Tags] bypass bot owner from mention check and accept the usage of name and ID --- tags/blocks/allowed_mentions.py | 30 +++++++++------------------ tags/mixins/processor.py | 36 +++++++++++++++++++++------------ 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/tags/blocks/allowed_mentions.py b/tags/blocks/allowed_mentions.py index 6fd44a1..0d2857b 100644 --- a/tags/blocks/allowed_mentions.py +++ b/tags/blocks/allowed_mentions.py @@ -25,8 +25,6 @@ from typing import ClassVar, Dict, List, Optional, Tuple, Union -import discord - from TagScriptEngine import Block, Context @@ -34,11 +32,12 @@ class AllowedMentionsBlock(Block): """ The "allowed mentions" block attempts to enable mentioning of roles. Passing no parameter enables mentioning of all roles within the message - content. However passing a role ID to the block parameter allows mentioning - of that specific role only. Multiple role IDs can be included, separated by - a comma ",". By default, mentioning is only triggered if the execution - author has "manage server" permissions. However, using the "override" - keyword as a payload allows mentioning to be triggered by anyone. + content. However passing a role name or ID to the block parameter allows + mentioning of that specific role only. Multiple role name or IDs can be + included, separated by a comma ",". By default, mentioning is only + triggered if the execution author has "manage server" permissions. However, + using the "override" keyword as a payload allows mentioning to be triggered + by anyone. **Usage:** ``{allowedmentions():["override", None]}`` @@ -46,12 +45,13 @@ class AllowedMentionsBlock(Block): **Payload:** "override", None - **Parameter:** role IDs, None + **Parameter:** role, None **Examples:** :: {allowedmentions} {allowedmentions:override} + {allowedmentions(@Admin, Moderator):override} {allowedmentions(763522431151112265, 812949167190048769)} {mentions(763522431151112265, 812949167190048769):override} """ @@ -73,22 +73,12 @@ def process(self, ctx: Context) -> Optional[str]: return None if not (param := ctx.verb.parameter): ctx.response.actions["allowed_mentions"] = { - "mentions": { - "everyone": False, - "users": True, - "roles": True, - "replied_user": True, - }, + "mentions": True, "override": True if ctx.verb.payload else False, } return "" ctx.response.actions["allowed_mentions"] = { - "mentions": { - "everyone": False, - "users": True, - "roles": [discord.Object(id=r.strip()) for r in param.split(",")], - "replied_user": True, - }, + "mentions": [r.strip() for r in param.split(",")], "override": True if ctx.verb.payload else False, } return "" diff --git a/tags/mixins/processor.py b/tags/mixins/processor.py index ddd5e23..71c54fb 100644 --- a/tags/mixins/processor.py +++ b/tags/mixins/processor.py @@ -252,20 +252,18 @@ async def send_tag_response( if allowed_mentions := actions.get("allowed_mentions"): if isinstance(ctx.author, discord.Member): - if ctx.author.guild_permissions.manage_guild or allowed_mentions.get( - "override", False + if ( + self.bot.is_owner(ctx.author) + or ctx.author.guild_permissions.manage_guild + or allowed_mentions.get("override", False) ): - kwargs["allowed_mentions"] = discord.AllowedMentions( - **allowed_mentions.get( - "mentions", - { - "everyone": False, - "users": True, - "roles": False, - "replied_user": True, - }, - ) - ) + mentions = allowed_mentions.get("mentions", True) + if isinstance(mentions, list): + roles: List[discord.Role] = await self._get_roles(ctx, mentions) + if roles: + kwargs["allowed_mentions"] = discord.AllowedMentions(roles=roles) + else: + kwargs["allowed_mentions"] = discord.AllowedMentions(roles=mentions) if replying: ref = ctx.message.to_reference(fail_if_not_exists=False) @@ -332,6 +330,18 @@ def handle_overrides( overriden_command.all_commands = all_commands return overriden_command + async def _get_roles(self, ctx: commands.Context, mentions: List[str]) -> List[discord.Role]: + tasks = [await asyncio.to_thread(self._convert_roles_silently, ctx, argument) for argument in mentions] + converted = await asyncio.gather(*tasks) + return [role for role in converted if role is not None] + + async def _convert_roles_silently( + self, ctx: commands.Context, argument: str + ) -> Optional[discord.Role]: + with contextlib.suppress(commands.RoleNotFound): + return await self.role_converter.convert(ctx, argument.strip("@")) + return None + async def validate_checks(self, ctx: commands.Context, actions: Dict[str, Any]) -> None: to_gather = [] if requires := actions.get("requires"):