From 6dbe1184ad2d5c6eb8277362714b4051fb1423c8 Mon Sep 17 00:00:00 2001 From: Soheab_ <33902984+Soheab@users.noreply.github.com> Date: Sat, 17 May 2025 00:03:16 +0200 Subject: [PATCH] Client & Bot --- discord/client.py | 31 +++++++++++++++++++++++++++---- discord/ext/commands/bot.py | 14 ++++++++++---- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/discord/client.py b/discord/client.py index b997bd96f..8e160755c 100644 --- a/discord/client.py +++ b/discord/client.py @@ -42,6 +42,7 @@ from typing import ( Tuple, Type, TypeVar, + TypedDict, Union, overload, ) @@ -82,7 +83,7 @@ from .soundboard import SoundboardDefaultSound, SoundboardSound if TYPE_CHECKING: from types import TracebackType - from typing_extensions import Self + from typing_extensions import Self, NotRequired, Unpack, Required from .abc import Messageable, PrivateChannel, Snowflake, SnowflakeTime from .app_commands import Command, ContextMenu @@ -120,7 +121,29 @@ if TYPE_CHECKING: from .audit_logs import AuditLogEntry from .poll import PollAnswer from .subscription import Subscription - + from .flags import MemberCacheFlags + + class _ClientOptions(TypedDict, total=False): + intents: Required[Intents] + max_messages: NotRequired[Optional[int]] + proxy: NotRequired[Optional[str]] + proxy_auth: NotRequired[Optional[aiohttp.BasicAuth]] + shard_id: NotRequired[Optional[int]] + shard_count: NotRequired[Optional[int]] + application_id: NotRequired[int] + member_cache_flags: NotRequired[MemberCacheFlags] + chunk_guilds_at_startup: NotRequired[bool] + status: NotRequired[Optional[Status]] + activity: NotRequired[Optional[BaseActivity]] + allowed_mentions: NotRequired[Optional[AllowedMentions]] + heartbeat_timeout: NotRequired[float] + guild_ready_timeout: NotRequired[float] + assume_unsync_clock: NotRequired[bool] + enable_debug_events: NotRequired[bool] + enable_raw_presences: NotRequired[bool] + http_trace: NotRequired[Optional[aiohttp.TraceConfig]] + max_ratelimit_timeout: NotRequired[Optional[float]] + connector: NotRequired[Optional[aiohttp.BaseConnector]] # fmt: off __all__ = ( @@ -272,7 +295,7 @@ class Client: The websocket gateway the client is currently connected to. Could be ``None``. """ - def __init__(self, *, intents: Intents, **options: Any) -> None: + def __init__(self, **options: Unpack[_ClientOptions]) -> None: self.loop: asyncio.AbstractEventLoop = _loop # self.ws is set in the connect method self.ws: DiscordWebSocket = None # type: ignore @@ -305,7 +328,7 @@ class Client: } self._enable_debug_events: bool = options.pop('enable_debug_events', False) - self._connection: ConnectionState[Self] = self._get_state(intents=intents, **options) + self._connection: ConnectionState[Self] = self._get_state(**options) self._connection.shard_count = self.shard_count self._closing_task: Optional[asyncio.Task[None]] = None self._ready: asyncio.Event = MISSING diff --git a/discord/ext/commands/bot.py b/discord/ext/commands/bot.py index 8ce872f1a..858d4f10d 100644 --- a/discord/ext/commands/bot.py +++ b/discord/ext/commands/bot.py @@ -64,7 +64,7 @@ from .cog import Cog from .hybrid import hybrid_command, hybrid_group, HybridCommand, HybridGroup if TYPE_CHECKING: - from typing_extensions import Self + from typing_extensions import Self, Unpack, NotRequired import importlib.machinery @@ -81,11 +81,18 @@ if TYPE_CHECKING: ) from .core import Command from .hybrid import CommandCallback, ContextT, P + from discord.client import _ClientOptions _Prefix = Union[Iterable[str], str] _PrefixCallable = MaybeAwaitableFunc[[BotT, Message], _Prefix] PrefixType = Union[_Prefix, _PrefixCallable[BotT]] + class _BotOptions(_ClientOptions): + owner_id: NotRequired[Optional[int]] + owner_ids: NotRequired[Optional[Collection[int]]] + strip_after_prefix: NotRequired[bool] + + __all__ = ( 'when_mentioned', 'when_mentioned_or', @@ -168,10 +175,9 @@ class BotBase(GroupMixin[None]): description: Optional[str] = None, allowed_contexts: app_commands.AppCommandContext = MISSING, allowed_installs: app_commands.AppInstallationType = MISSING, - intents: discord.Intents, - **options: Any, + **options: Unpack[_BotOptions], ) -> None: - super().__init__(intents=intents, **options) + super().__init__(**options) self.command_prefix: PrefixType[BotT] = command_prefix # type: ignore self.extra_events: Dict[str, List[CoroFunc]] = {} # Self doesn't have the ClientT bound, but since this is a mixin it technically does