Skip to content

Commit

Permalink
[Tags] bypass bot owner from mention check and accept the usage of na…
Browse files Browse the repository at this point in the history
…me and ID
  • Loading branch information
japandotorg committed Jun 25, 2024
1 parent 56d9d7d commit 65e3743
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
30 changes: 10 additions & 20 deletions tags/blocks/allowed_mentions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,33 @@

from typing import ClassVar, Dict, List, Optional, Tuple, Union

import discord

from TagScriptEngine import Block, Context


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(<role, None>):["override", None]}``
**Aliases:** ``mentions``
**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}
"""
Expand All @@ -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 ""
36 changes: 23 additions & 13 deletions tags/mixins/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"):
Expand Down

0 comments on commit 65e3743

Please sign in to comment.