From 1339d834b4de81d5757ce18532a563040fc579dc Mon Sep 17 00:00:00 2001 From: Lemon Rose Date: Mon, 23 Oct 2023 13:06:55 +0530 Subject: [PATCH] [Tags] fixes --- tags/abc.py | 23 +++++++++++++---------- tags/core.py | 34 +++++++++++++++++----------------- tags/utils.py | 4 +++- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/tags/abc.py b/tags/abc.py index ecff7561..a2cec0e7 100644 --- a/tags/abc.py +++ b/tags/abc.py @@ -25,7 +25,8 @@ import asyncio from abc import ABC, abstractmethod -from typing import Any, Coroutine, Dict, List, Optional +from collections import defaultdict +from typing import Any, Coroutine, Dict, List, Optional, Union import discord from aiohttp import ClientSession @@ -33,6 +34,7 @@ from redbot.core.bot import Red from .objects import Tag +from .utils import RequesterType class MixinMeta(ABC): @@ -48,26 +50,27 @@ class MixinMeta(ABC): session: ClientSession def __init__(self, *_args: Any) -> None: - super().__init__() + super().__init__(*_args) + self.guild_tag_cache: defaultdict[int, Dict[str, Tag]] + self.global_tag_cache: Dict[str, Tag] + self.dot_parameter: Optional[bool] + self.async_enabled: Optional[bool] + self.docs: Union[List[str], Dict[str, str]] @abstractmethod - def create_task(self, coroutine: Coroutine, *, name: Optional[str] = None) -> asyncio.Task: + async def cog_unload(self) -> None: raise NotImplementedError() @abstractmethod - async def cog_unload(self): - raise NotImplementedError() - - @abstractmethod - async def initialize(self) -> None: + def create_task(self, coroutine: Coroutine, *, name: Optional[str] = None) -> asyncio.Task: raise NotImplementedError() @abstractmethod - async def format_help_for_context(self, ctx: commands.Context) -> str: + def format_help_for_context(self, ctx: commands.Context) -> str: raise NotImplementedError() @abstractmethod - async def red_delete_data_for_user(self, *, requester: str, user_id: int) -> None: + async def red_delete_data_for_user(self, *, requester: RequesterType, user_id: int) -> None: raise NotImplementedError() @abstractmethod diff --git a/tags/core.py b/tags/core.py index 82ff305a..5f22ebe4 100644 --- a/tags/core.py +++ b/tags/core.py @@ -29,7 +29,7 @@ import re from collections import defaultdict from operator import itemgetter -from typing import Any, Coroutine, Dict, Final, List, Optional, Tuple, Union +from typing import Any, Coroutine, Dict, Final, List, Optional, Union import aiohttp import discord @@ -45,6 +45,7 @@ from .errors import MissingTagPermissions, TagCharacterLimitReached from .mixins import Commands, OwnerCommands, Processor from .objects import Tag +from .utils import RequesterType log: logging.Logger = logging.getLogger("red.seina.tags") @@ -65,19 +66,7 @@ class Tags( """ __version__: Final[str] = "2.5.2" - __author__: Final[Tuple[str, ...]] = ("inthedark.org", "PhenoM4n4n", "sravan", "npc203") - - def format_help_for_context(self, ctx: commands.Context) -> str: - pre_processed = super().format_help_for_context(ctx) - n = "\n" if "\n\n" not in pre_processed else "" - authors = [f"**{name}**" for name in self.__author__] - text = [ - f"{pre_processed}{n}", - f"Cog Version: **{self.__version__}**", - f"AdvancedTagScriptEngine Version: **{tse_version}**", - f"Author: {humanize_list(authors)}", - ] - return "\n".join(text) + __author__: Final[List[str]] = ["inthedark.org", "PhenoM4n4n", "sravan", "npc203"] def __init__(self, bot: Red) -> None: self.bot: Red = bot @@ -108,7 +97,7 @@ def __init__(self, bot: Red) -> None: self.initialize_task: asyncio.Task = self.create_task(self.initialize()) self.session: aiohttp.ClientSession = aiohttp.ClientSession() - self.docs: List = [] + self.docs: Union[List[str], Dict[str, str]] = [] if bot._cli_flags.logging_level == logging.DEBUG: logging.getLogger("TagScriptEngine").setLevel(logging.DEBUG) @@ -118,7 +107,19 @@ def __init__(self, bot: Red) -> None: super().__init__() + def format_help_for_context(self, ctx: commands.Context) -> str: + pre_processed = super().format_help_for_context(ctx) + n = "\n" if "\n\n" not in str(pre_processed) else "" + text = [ + f"{pre_processed}{n}", + f"Cog Version: **{self.__version__}**", + f"AdvancedTagScriptEngine Version: **{tse_version}**", + f"Author: {humanize_list(self.__author__)}", + ] + return "\n".join(text) + async def cog_unload(self) -> None: + await super().cog_unload() try: await self.__unload() except Exception as e: @@ -129,9 +130,8 @@ async def __unload(self) -> None: if self.initialize_task: self.initialize_task.cancel() await self.session.close() - await super().cog_unload() - async def red_delete_data_for_user(self, *, requester: str, user_id: int) -> None: + async def red_delete_data_for_user(self, *, requester: RequesterType, user_id: int) -> None: if requester not in ("discord_deleted_user", "user"): return guilds_data = await self.config.all_guilds() diff --git a/tags/utils.py b/tags/utils.py index 50308c80..0e17c2ab 100644 --- a/tags/utils.py +++ b/tags/utils.py @@ -24,7 +24,7 @@ """ from importlib import reload -from typing import Final, Generator, List, Sequence, Union +from typing import Final, Generator, List, Sequence, TypeAlias, Union, Literal import discord from redbot.core import commands @@ -35,6 +35,8 @@ PIP: Final[str] = "pip3" +RequesterType: TypeAlias = Literal["discord_deleted_user", "owner", "user", "user_strict"] + async def menu(ctx: commands.Context, pages: List[Union[str, discord.Embed]]) -> None: view = PaginatedView(PageSource(pages))