From ce2f844586e05056231cca2d017fc1e48ded30cf Mon Sep 17 00:00:00 2001 From: Lemon Rose Date: Sun, 22 Oct 2023 14:48:25 +0530 Subject: [PATCH] [Tags] make global & guild tags limit custom --- tags/core.py | 8 ++++++-- tags/mixins/commands.py | 25 ++++++++++++------------- tags/mixins/owner.py | 32 ++++++++++++++++++++++++++++++++ tags/mixins/processor.py | 12 ++++++------ 4 files changed, 56 insertions(+), 21 deletions(-) diff --git a/tags/core.py b/tags/core.py index 43a31766..e485bb4b 100644 --- a/tags/core.py +++ b/tags/core.py @@ -86,12 +86,16 @@ def __init__(self, bot: Red) -> None: identifier=567234895692346562369, force_registration=True, ) - default_guild: Dict[str, Dict[str, Any]] = {"tags": {}} - default_global: Dict[str, Union[Dict[str, Dict[str, Any]], Dict[str, str], bool]] = { + default_guild: Dict[str, Union[Dict[str, Any], int]] = { + "tags": {}, + "max_tags_limit": 250, + } + default_global: Dict[str, Union[Dict[str, Dict[str, Any]], Dict[str, str], bool, int]] = { "tags": {}, "blocks": {}, "async_enabled": False, "dot_parameter": False, + "max_tags_limit": 250, } self.config.register_guild(**default_guild) self.config.register_global(**default_global) diff --git a/tags/mixins/commands.py b/tags/mixins/commands.py index 42abfeeb..34962e4a 100644 --- a/tags/mixins/commands.py +++ b/tags/mixins/commands.py @@ -28,7 +28,7 @@ import time import types from collections import Counter -from typing import Dict, List, Optional, Union +from typing import Dict, List, Optional, Union, Final, Pattern from urllib.parse import quote_plus import discord @@ -52,14 +52,11 @@ from ..utils import chunks, menu from ..views import ConfirmationView -TAG_GUILD_LIMIT = 250 -TAG_GLOBAL_LIMIT = 250 +TAG_RE: Pattern[str] = re.compile(r"(?i)(\[p\])?\btag'?s?\b") -TAG_RE = re.compile(r"(?i)(\[p\])?\btag'?s?\b") +DOCS_URL: Final[str] = "https://seina-cogs.readthedocs.io/en/latest" -DOCS_URL = "https://seina-cogs.readthedocs.io/en/latest" - -log = logging.getLogger("red.seina.tags.commands") +log: logging.Logger = logging.getLogger("red.seina.tags.commands") def _sub(match: re.Match) -> str: @@ -186,16 +183,18 @@ async def tags(self, ctx: commands.Context): embeds.append(e) await menu(ctx, embeds) - def validate_tag_count(self, guild: discord.Guild) -> None: + async def validate_tag_count(self, guild: discord.Guild) -> None: + global_max_limit: int = await self.config.max_tags_limit() + guild_max_limit: int = await self.config.guild(guild).max_tags_limit() tag_count = len(self.get_unique_tags(guild)) if guild: - if tag_count >= TAG_GUILD_LIMIT: + if tag_count >= guild_max_limit: raise TagFeedbackError( - f"This server has reached the limit of **{TAG_GUILD_LIMIT}** tags." + f"This server has reached the limit of **{guild_max_limit}** tags." ) - elif tag_count >= TAG_GLOBAL_LIMIT: + elif tag_count >= global_max_limit: raise TagFeedbackError( - f"You have reached the limit of **{TAG_GLOBAL_LIMIT}** global tags." + f"You have reached the limit of **{global_max_limit}** global tags." ) async def create_tag( @@ -210,7 +209,7 @@ async def create_tag( guild = ctx.guild tag = self.get_tag(guild, tag_name, check_global=False) kwargs["guild_id"] = guild.id - self.validate_tag_count(guild) + await self.validate_tag_count(guild) if tag: tag_prefix = tag.name_prefix diff --git a/tags/mixins/owner.py b/tags/mixins/owner.py index 894486a4..891792eb 100644 --- a/tags/mixins/owner.py +++ b/tags/mixins/owner.py @@ -244,6 +244,38 @@ async def tagsettings_dotparam(self, ctx: commands.Context, true_or_false: bool "Blocks will be parsed like this: `{declaration%s:payload}`." % parameter ) + @tagsettings.group("limit") + async def tagsettings_limit(self, ctx: commands.Context): + """ + Change the global and guild limit for tags. + """ + + @commands.guild_only() + @tagsettings_limit.command("guild") + async def tagsettings_limit_guild( + self, + ctx: commands.Context, + amount: commands.Range[int, 150, 500], + guild: Optional[discord.Guild] = None, + ): + """ + Change the guild limit for tags. + """ + if guild is None: + guild: discord.Guild = ctx.guild + await self.config.guild(guild).max_tags_limit.set(amount) + await ctx.send(f"Changed the guild limit to {amount}.") + + @tagsettings_limit.command("global") + async def tagsettings_limit_global( + self, ctx: commands.Context, amount: commands.Range[int, 150, 500] + ): + """ + Change the global limit for tags. + """ + await self.config.max_tags_limit.set(amount) + await ctx.send(f"Changed the global limit to {amount}.") + @commands.is_owner() @commands.command() async def migratealias(self, ctx: commands.Context): diff --git a/tags/mixins/processor.py b/tags/mixins/processor.py index 635b0dcf..b87e6550 100644 --- a/tags/mixins/processor.py +++ b/tags/mixins/processor.py @@ -109,7 +109,7 @@ async def initialize_interpreter(self, data: Optional[Dict[str, Any]] = None) -> @commands.Cog.listener() async def on_command_error( - self, ctx: commands.Context, error: commands.CommandError, unhandled_by_cog=False + self, ctx: commands.Context, error: commands.CommandError, unhandled_by_cog: bool = False ): if not isinstance(error, commands.CommandNotFound): return @@ -159,7 +159,7 @@ async def process_tag( tag: Tag, *, seed_variables: Optional[Dict[str, Any]] = None, - **kwargs, + **kwargs: Any, ) -> Optional[str]: seed_variables = {} if seed_variables is None else seed_variables seed = self.get_seed_from_context(ctx) @@ -213,7 +213,7 @@ async def process_tag( @staticmethod async def send_quietly( - destination: discord.abc.Messageable, content: Optional[str] = None, **kwargs + destination: discord.abc.Messageable, content: Optional[str] = None, **kwargs: Any ) -> Optional[discord.Message]: try: return await destination.send(content, **kwargs) @@ -225,7 +225,7 @@ async def send_tag_response( ctx: commands.Context, actions: Dict[str, Any], content: Optional[str] = None, - **kwargs, + **kwargs: Any, ) -> Optional[discord.Message]: destination = ctx.channel embed = actions.get("embed") @@ -256,7 +256,7 @@ async def send_tag_response( return await self.send_quietly(destination, content, **kwargs) async def process_commands( - self, messages: List[discord.Message], silent: bool, overrides: dict + self, messages: List[discord.Message], silent: bool, overrides: Dict ) -> None: command_tasks = [] for message in messages: @@ -277,7 +277,7 @@ async def process_command( await self.bot.invoke(ctx) @classmethod - def handle_overrides(cls, command: commands.Command, overrides: dict) -> commands.Command: + def handle_overrides(cls, command: commands.Command, overrides: Dict) -> commands.Command: overriden_command = copy(command) # overriden_command = command.copy() # does not work as it makes ctx a regular argument # overriden_command.cog = command.cog