From 54f8fe5a56603f3ed45a5965029705107e397145 Mon Sep 17 00:00:00 2001 From: AlexFlipnote Date: Sun, 22 Dec 2024 16:52:58 +0100 Subject: [PATCH] More type safty and small fix --- discord_http/client.py | 2 +- discord_http/commands.py | 6 ++++-- discord_http/gateway/shard.py | 8 ++++---- discord_http/guild.py | 18 +++++++++--------- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/discord_http/client.py b/discord_http/client.py index 25a3418..344fbbb 100644 --- a/discord_http/client.py +++ b/discord_http/client.py @@ -412,7 +412,7 @@ async def sync_commands(self) -> None: int(gid) for gid in cmd.guild_ids ]) - guild_ids = list(set(guild_ids)) + guild_ids: list[int] = list(set(guild_ids)) for g in guild_ids: await self.state.update_commands( diff --git a/discord_http/commands.py b/discord_http/commands.py index 385c5d8..ddbb61d 100644 --- a/discord_http/commands.py +++ b/discord_http/commands.py @@ -255,7 +255,7 @@ def __init__( self._converters: dict[str, Type[Converter]] = {} self.__list_choices: list[str] = [] - self.__user_objects: dict[str, Type[Union[Member, User]]] = {} + self.__user_objects: dict[str, Type[Member | User]] = {} if self.type == ApplicationCommandType.chat_input: if self.description is None: @@ -337,7 +337,9 @@ def __init__( else: # Just a regular channel type option.update({ - "channel_types": [int(i) for i in channel_types[origin]] + "channel_types": [ + int(i) for i in channel_types[origin] + ] }) case x if x in [Attachment]: diff --git a/discord_http/gateway/shard.py b/discord_http/gateway/shard.py index 571f6b1..3a000ec 100644 --- a/discord_http/gateway/shard.py +++ b/discord_http/gateway/shard.py @@ -21,7 +21,7 @@ if TYPE_CHECKING: from ..member import Member from ..client import Client - from ..guild import Guild + from ..guild import Guild, PartialGuild DEFAULT_GATEWAY = yarl.URL("wss://gateway.discord.gg/") _log = logging.getLogger("discord_http") @@ -570,7 +570,7 @@ async def _socket_manager(self) -> None: self._reset_instance() _log.error(f"Shard {self.shard_id} crashed completly", exc_info=e) - def _guild_needs_chunking(self, guild: "Guild") -> bool: + def _guild_needs_chunking(self, guild: "Guild | PartialGuild") -> bool: return ( self.bot.chunk_guilds_on_startup and not guild.chunked and @@ -580,7 +580,7 @@ def _guild_needs_chunking(self, guild: "Guild") -> bool: ) ) - def _chunk_timeout(self, guild: "Guild") -> float: + def _chunk_timeout(self, guild: "Guild | PartialGuild") -> float: return max(5.0, (guild.member_count or 0) / 10_000) async def _delay_ready(self) -> None: @@ -589,7 +589,7 @@ async def _delay_ready(self) -> None: Then make shard ready when last GUILD_CREATE is received """ try: - states: list[tuple[Guild, asyncio.Future[list[Member]]]] = [] + states: list[tuple[Guild | PartialGuild, asyncio.Future[list[Member]]]] = [] while True: try: guild_data = await asyncio.wait_for( diff --git a/discord_http/guild.py b/discord_http/guild.py index 2319ba8..37f04d2 100644 --- a/discord_http/guild.py +++ b/discord_http/guild.py @@ -321,6 +321,15 @@ def __init__(self, *, state: "DiscordAPI", id: int): def __repr__(self) -> str: return f"" + @property + def large(self) -> bool: + """ `bool`: Whether the guild is considered large """ + if self._large is None: + if self.member_count is not None: + return self.member_count >= 250 + return len(self.members) >= 250 + return self.large + @property def chunked(self) -> bool: """ `bool`: Whether the guild is chunked or not """ @@ -2556,15 +2565,6 @@ def _update(self, data: dict) -> None: self.widget_channel_id: Optional[int] = utils.get_int(data, "widget_channel_id") self.widget_enabled: bool = data.get("widget_enabled", False) - @property - def large(self) -> bool: - """ `bool`: Whether the guild is considered large """ - if self._large is None: - if self.member_count is not None: - return self.member_count >= 250 - return len(self.members) >= 250 - return self.large - @property def emojis_limit(self) -> int: """ `int`: The maximum amount of emojis the guild can have """