From bee7f613a32ab91e6fd14d6aa29f71d07d7c2426 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Tue, 26 Oct 2021 01:32:43 +0200 Subject: [PATCH 1/6] Update Application Flags Source: https://github.com/discord/discord-api-docs/pull/3999 Added gateway_message_content with value 1 << 18 Added gateway_message_content_limited with value 1 << 19 Source: https://github.com/Aiko-IT-Systems/DisCatSharp/blob/main/DisCatSharp/Enums/Application/ApplicationFlags.cs Added missing managed_emoji with value 1 << 2 Added missing group_dm_create with value 1 << 5 Added missing rpc_has_connected with value 1 << 11 --- discord/flags.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/discord/flags.py b/discord/flags.py index 222f5124d0..b0a2b5fe29 100644 --- a/discord/flags.py +++ b/discord/flags.py @@ -1090,6 +1090,24 @@ class ApplicationFlags(BaseFlags): rather than using this raw value. """ + @flag_value + def managed_emoji(self): + """:class:`bool`: Returns ``True`` if the application is a managed emoji. + """ + return 1 << 2 + + @flag_value + def group_dm_create(self): + """:class:`bool`: Returns ``True`` if the application can create group dms. + """ + return 1 << 5 + + @flag_value + def rpc_has_connected(self): + """:class:`bool`: Returns ``True`` if the application has connected to RPC. + """ + return 1 << 11 + @flag_value def gateway_presence(self): """:class:`bool`: Returns ``True`` if the application is verified and is allowed to @@ -1129,3 +1147,16 @@ def verification_pending_guild_limit(self): def embedded(self): """:class:`bool`: Returns ``True`` if the application is embedded within the Discord client.""" return 1 << 17 + + @flag_value + def gateway_message_content(self): + """:class:`bool`: Returns ``True`` if the application is allowed to read message contents in guilds. + """ + return 1 << 18 + + @flag_value + def gateway_message_content_limited(self): + """:class:`bool`: Returns ``True`` if the application is currently pending verification + and has hit the guild limit. + """ + return 1 << 19 From 054b7612f2fb9e06d2cc7b35543bb17a030ceea3 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Tue, 26 Oct 2021 01:48:55 +0200 Subject: [PATCH 2/6] Update discord/flags.py Co-authored-by: ToxicKidz <78174417+ToxicKidz@users.noreply.github.com> --- discord/flags.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/flags.py b/discord/flags.py index b0a2b5fe29..68c36a2daa 100644 --- a/discord/flags.py +++ b/discord/flags.py @@ -1098,7 +1098,7 @@ def managed_emoji(self): @flag_value def group_dm_create(self): - """:class:`bool`: Returns ``True`` if the application can create group dms. + """:class:`bool`: Returns ``True`` if the application can create group DMs. """ return 1 << 5 From 7f1d133ee6bf561a3f295dae60f28461810e3425 Mon Sep 17 00:00:00 2001 From: ToxicKidz Date: Tue, 26 Oct 2021 20:12:34 -0400 Subject: [PATCH 3/6] fix: Remove commented out section of Bot.on_application_command_error This also adds ApplicationContext.cog --- discord/bot.py | 12 +++++------- discord/commands/context.py | 14 +++++++++++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/discord/bot.py b/discord/bot.py index 9b16a63cb5..71a129637a 100644 --- a/discord/bot.py +++ b/discord/bot.py @@ -569,18 +569,16 @@ async def on_application_command_error( This only fires if you do not specify any listeners for command error. """ - # TODO - # if self.extra_events.get('on_application_command_error', None): - # return + if self.extra_events.get('on_application_command_error', None): + return command = context.command if command and command.has_error_handler(): return - # TODO - # cog = context.cog - # if cog and cog.has_error_handler(): - # return + cog = context.cog + if cog and cog.has_error_handler(): + return print(f"Ignoring exception in command {context.command}:", file=sys.stderr) traceback.print_exception( diff --git a/discord/commands/context.py b/discord/commands/context.py index 9956ff2420..857f5d0e4a 100644 --- a/discord/commands/context.py +++ b/discord/commands/context.py @@ -22,6 +22,7 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +from __future__ import annotations from typing import TYPE_CHECKING, Optional, Union @@ -31,6 +32,9 @@ import discord from discord.state import ConnectionState + from .commands import ApplicationCommand + from ..cog import Cog + from ..guild import Guild from ..interactions import Interaction, InteractionResponse from ..member import Member @@ -63,7 +67,7 @@ class ApplicationContext(discord.abc.Messageable): def __init__(self, bot: "discord.Bot", interaction: Interaction): self.bot = bot self.interaction = interaction - self.command = None + self.command: ApplicationCommand = None # type: ignore self._state: ConnectionState = self.interaction._state async def _get_channel(self) -> discord.abc.Messageable: @@ -130,3 +134,11 @@ async def delete(self): @property def edit(self): return self.interaction.edit_original_message + + @property + def cog(self) -> Optional[Cog]: + """Optional[:class:`.Cog`]: Returns the cog associated with this context's command. None if it does not exist.""" + if self.command is None: + return None + + return self.command.cog From 60dcc535e78fd5f0e8c28da43c6bd35b16d03f01 Mon Sep 17 00:00:00 2001 From: Makiyu-py <73825066+Makiyu-py@users.noreply.github.com> Date: Wed, 27 Oct 2021 10:08:21 +0800 Subject: [PATCH 4/6] implement Pycord-Development/pycord#321 --- discord/webhook/async_.py | 55 ++++++++++++++++++++++++++++++++++----- discord/webhook/sync.py | 55 ++++++++++++++++++++++++++++++++++----- 2 files changed, 98 insertions(+), 12 deletions(-) diff --git a/discord/webhook/async_.py b/discord/webhook/async_.py index a0d6392fe9..5c4045f71f 100644 --- a/discord/webhook/async_.py +++ b/discord/webhook/async_.py @@ -274,7 +274,13 @@ def get_webhook_message( message_id: int, *, session: aiohttp.ClientSession, + thread_id: Optional[int] = None, ) -> Response[MessagePayload]: + params = {} + + if thread_id: + params['thread_id'] = thread_id + route = Route( 'GET', '/webhooks/{webhook_id}/{webhook_token}/messages/{message_id}', @@ -282,7 +288,7 @@ def get_webhook_message( webhook_token=token, message_id=message_id, ) - return self.request(route, session) + return self.request(route, session, params=params) def edit_webhook_message( self, @@ -291,10 +297,16 @@ def edit_webhook_message( message_id: int, *, session: aiohttp.ClientSession, + thread_id: Optional[int] = None, payload: Optional[Dict[str, Any]] = None, multipart: Optional[List[Dict[str, Any]]] = None, files: Optional[List[File]] = None, ) -> Response[Message]: + params = {} + + if thread_id: + params['thread_id'] = thread_id + route = Route( 'PATCH', '/webhooks/{webhook_id}/{webhook_token}/messages/{message_id}', @@ -302,7 +314,7 @@ def edit_webhook_message( webhook_token=token, message_id=message_id, ) - return self.request(route, session, payload=payload, multipart=multipart, files=files) + return self.request(route, session, params=params, payload=payload, multipart=multipart, files=files) def delete_webhook_message( self, @@ -311,7 +323,13 @@ def delete_webhook_message( message_id: int, *, session: aiohttp.ClientSession, + thread_id: Optional[int] = None, ) -> Response[None]: + params = {} + + if thread_id: + params['thread_id'] = thread_id + route = Route( 'DELETE', '/webhooks/{webhook_id}/{webhook_token}/messages/{message_id}', @@ -319,7 +337,7 @@ def delete_webhook_message( webhook_token=token, message_id=message_id, ) - return self.request(route, session) + return self.request(route, session, params=params) def fetch_webhook( self, @@ -1429,7 +1447,12 @@ async def send( return msg - async def fetch_message(self, id: int, /) -> WebhookMessage: + async def fetch_message( + self, + id: int, + *, + thread_id: Optional[int] = None + ) -> WebhookMessage: """|coro| Retrieves a single :class:`~discord.WebhookMessage` owned by this webhook. @@ -1440,6 +1463,8 @@ async def fetch_message(self, id: int, /) -> WebhookMessage: ------------ id: :class:`int` The message ID to look for. + thread_id: Optional[:class:`int`] + The ID of the thread where the message is. Raises -------- @@ -1467,6 +1492,7 @@ async def fetch_message(self, id: int, /) -> WebhookMessage: self.token, id, session=self.session, + thread_id=thread_id, ) return self._create_message(data) @@ -1481,6 +1507,7 @@ async def edit_message( files: List[File] = MISSING, view: Optional[View] = MISSING, allowed_mentions: Optional[AllowedMentions] = None, + thread: Optional[Snowflake] = MISSING ) -> WebhookMessage: """|coro| @@ -1522,7 +1549,9 @@ async def edit_message( the view is removed. The webhook must have state attached, similar to :meth:`send`. - .. versionadded:: 2.0 + .. versionadded:: 2. + thread: Optional[:class:`~discord.abc.Snowflake`] + The thread where the message is. Raises ------- @@ -1564,12 +1593,18 @@ async def edit_message( allowed_mentions=allowed_mentions, previous_allowed_mentions=previous_mentions, ) + + thread_id: Optional[int] = None + if thread_id is not MISSING: + thread_id = thread.id + adapter = async_context.get() data = await adapter.edit_webhook_message( self.id, self.token, message_id, session=self.session, + thread_id=thread_id, payload=params.payload, multipart=params.multipart, files=params.files, @@ -1580,7 +1615,12 @@ async def edit_message( self._state.store_view(view, message_id) return message - async def delete_message(self, message_id: int, /) -> None: + async def delete_message( + self, + message_id: int, + *, + thread_id: Optional[int] = None + ) -> None: """|coro| Deletes a message owned by this webhook. @@ -1594,6 +1634,8 @@ async def delete_message(self, message_id: int, /) -> None: ------------ message_id: :class:`int` The message ID to delete. + thread_id: Optional[:class:`int`] + The ID of the thread where the message is. Raises ------- @@ -1611,4 +1653,5 @@ async def delete_message(self, message_id: int, /) -> None: self.token, message_id, session=self.session, + thread_id=thread_id, ) diff --git a/discord/webhook/sync.py b/discord/webhook/sync.py index 973bac5fbf..3b83ec034d 100644 --- a/discord/webhook/sync.py +++ b/discord/webhook/sync.py @@ -281,7 +281,13 @@ def get_webhook_message( message_id: int, *, session: Session, + thread_id: Optional[int] = None, ): + params = {} + + if thread_id: + params['thread_id'] = thread_id + route = Route( 'GET', '/webhooks/{webhook_id}/{webhook_token}/messages/{message_id}', @@ -289,7 +295,7 @@ def get_webhook_message( webhook_token=token, message_id=message_id, ) - return self.request(route, session) + return self.request(route, session, params=params) def edit_webhook_message( self, @@ -298,10 +304,16 @@ def edit_webhook_message( message_id: int, *, session: Session, + thread_id: Optional[int] = None, payload: Optional[Dict[str, Any]] = None, multipart: Optional[List[Dict[str, Any]]] = None, files: Optional[List[File]] = None, ): + params = {} + + if thread_id: + params['thread_id'] = thread_id + route = Route( 'PATCH', '/webhooks/{webhook_id}/{webhook_token}/messages/{message_id}', @@ -309,7 +321,7 @@ def edit_webhook_message( webhook_token=token, message_id=message_id, ) - return self.request(route, session, payload=payload, multipart=multipart, files=files) + return self.request(route, session, params=params, payload=payload, multipart=multipart, files=files) def delete_webhook_message( self, @@ -318,7 +330,13 @@ def delete_webhook_message( message_id: int, *, session: Session, + thread_id: Optional[int] = None, ): + params = {} + + if thread_id: + params['thread_id'] = thread_id + route = Route( 'DELETE', '/webhooks/{webhook_id}/{webhook_token}/messages/{message_id}', @@ -326,7 +344,7 @@ def delete_webhook_message( webhook_token=token, message_id=message_id, ) - return self.request(route, session) + return self.request(route, session, params=params) def fetch_webhook( self, @@ -920,7 +938,12 @@ def send( if wait: return self._create_message(data) - def fetch_message(self, id: int, /) -> SyncWebhookMessage: + def fetch_message( + self, + id: int, + *, + thread_id: Optional[int] = None + ) -> SyncWebhookMessage: """Retrieves a single :class:`~discord.SyncWebhookMessage` owned by this webhook. .. versionadded:: 2.0 @@ -929,6 +952,8 @@ def fetch_message(self, id: int, /) -> SyncWebhookMessage: ------------ id: :class:`int` The message ID to look for. + thread_id: Optional[:class:`int`] + The ID of the thread where the message is. Raises -------- @@ -956,6 +981,7 @@ def fetch_message(self, id: int, /) -> SyncWebhookMessage: self.token, id, session=self.session, + thread_id=thread_id, ) return self._create_message(data) @@ -969,6 +995,7 @@ def edit_message( file: File = MISSING, files: List[File] = MISSING, allowed_mentions: Optional[AllowedMentions] = None, + thread: Optional[Snowflake] = MISSING, ) -> SyncWebhookMessage: """Edits a message owned by this webhook. @@ -996,6 +1023,8 @@ def edit_message( allowed_mentions: :class:`AllowedMentions` Controls the mentions being processed in this message. See :meth:`.abc.Messageable.send` for more information. + thread: Optional[:class:`~discord.abc.Snowflake`] + The thread where the message is. Raises ------- @@ -1025,19 +1054,30 @@ def edit_message( previous_allowed_mentions=previous_mentions, ) adapter: WebhookAdapter = _get_webhook_adapter() + + thread_id: Optional[int] = None + if thread is not MISSING: + thread_id = thread.id + data = adapter.edit_webhook_message( self.id, self.token, message_id, session=self.session, + thread_id=thread_id, payload=params.payload, multipart=params.multipart, files=params.files, ) return self._create_message(data) - def delete_message(self, message_id: int, /) -> None: - """Deletes a message owned by this webhook. + def delete_message( + self, + message_id: int, + *, + thread_id: Optional[int] = None + ) -> None: + """Deletes a message owned by this webhook. This is a lower level interface to :meth:`WebhookMessage.delete` in case you only have an ID. @@ -1048,6 +1088,8 @@ def delete_message(self, message_id: int, /) -> None: ------------ message_id: :class:`int` The message ID to delete. + thread_id: Optional[:class:`int`] + The ID of the thread where the message is. Raises ------- @@ -1065,4 +1107,5 @@ def delete_message(self, message_id: int, /) -> None: self.token, message_id, session=self.session, + thread_id=thread_id, ) From c9f66c4a36db4527ae5986636fdf64bc4d1f1283 Mon Sep 17 00:00:00 2001 From: Makiyu <73825066+Makiyu-py@users.noreply.github.com> Date: Thu, 28 Oct 2021 11:22:20 +0800 Subject: [PATCH 5/6] Apply Lalasuggestions from code review Co-authored-by: Lala Sabathil --- discord/webhook/async_.py | 6 +++--- discord/webhook/sync.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/discord/webhook/async_.py b/discord/webhook/async_.py index 5c4045f71f..be0b66519e 100644 --- a/discord/webhook/async_.py +++ b/discord/webhook/async_.py @@ -1464,7 +1464,7 @@ async def fetch_message( id: :class:`int` The message ID to look for. thread_id: Optional[:class:`int`] - The ID of the thread where the message is. + The ID of the thread that contains the message. Raises -------- @@ -1551,7 +1551,7 @@ async def edit_message( .. versionadded:: 2. thread: Optional[:class:`~discord.abc.Snowflake`] - The thread where the message is. + The thread that contains the message. Raises ------- @@ -1635,7 +1635,7 @@ async def delete_message( message_id: :class:`int` The message ID to delete. thread_id: Optional[:class:`int`] - The ID of the thread where the message is. + The ID of the thread that contains the message. Raises ------- diff --git a/discord/webhook/sync.py b/discord/webhook/sync.py index 3b83ec034d..81892181f3 100644 --- a/discord/webhook/sync.py +++ b/discord/webhook/sync.py @@ -953,7 +953,7 @@ def fetch_message( id: :class:`int` The message ID to look for. thread_id: Optional[:class:`int`] - The ID of the thread where the message is. + The ID of the thread that contains the message. Raises -------- @@ -1024,7 +1024,7 @@ def edit_message( Controls the mentions being processed in this message. See :meth:`.abc.Messageable.send` for more information. thread: Optional[:class:`~discord.abc.Snowflake`] - The thread where the message is. + The thread that contains the message. Raises ------- @@ -1077,7 +1077,7 @@ def delete_message( *, thread_id: Optional[int] = None ) -> None: - """Deletes a message owned by this webhook. + """Deletes a message owned by this webhook. This is a lower level interface to :meth:`WebhookMessage.delete` in case you only have an ID. @@ -1089,7 +1089,7 @@ def delete_message( message_id: :class:`int` The message ID to delete. thread_id: Optional[:class:`int`] - The ID of the thread where the message is. + The ID of the thread that contains the message. Raises ------- From 1d09bf2ab9f617eff76c16de082d6d47165d296f Mon Sep 17 00:00:00 2001 From: Makiyu <73825066+Makiyu-py@users.noreply.github.com> Date: Thu, 28 Oct 2021 13:37:59 +0800 Subject: [PATCH 6/6] versionadded mistake oops Co-authored-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> --- discord/webhook/async_.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/webhook/async_.py b/discord/webhook/async_.py index be0b66519e..c4f016da3e 100644 --- a/discord/webhook/async_.py +++ b/discord/webhook/async_.py @@ -1549,7 +1549,7 @@ async def edit_message( the view is removed. The webhook must have state attached, similar to :meth:`send`. - .. versionadded:: 2. + .. versionadded:: 2.0 thread: Optional[:class:`~discord.abc.Snowflake`] The thread that contains the message.