Skip to content

Commit

Permalink
Merge branch 'master' into upd-monetization
Browse files Browse the repository at this point in the history
Signed-off-by: plun1331 <plun1331@gmail.com>
  • Loading branch information
plun1331 committed May 29, 2024
2 parents d22db63 + 9fc3d7b commit 9110129
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 63 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ These changes are available on the `master` branch, but have not yet been releas
([#2421](https://github.com/Pycord-Development/pycord/pull/2421))
- Added `member` data to the `raw_reaction_remove` event.
([#2412](https://github.com/Pycord-Development/pycord/pull/2412))
- Added `stacklevel` param to `utils.warn_deprecated` and `utils.deprecated`.
([#2450](https://github.com/Pycord-Development/pycord/pull/2450))
- Added support for one-time purchases for Discord monetization.
([#2438](https://github.com/Pycord-Development/pycord/pull/2438))

Expand All @@ -46,8 +48,12 @@ These changes are available on the `master` branch, but have not yet been releas
([#2411](https://github.com/Pycord-Development/pycord/pull/2411))
- Fixed option typehints being ignored when using `parameter_name`.
([#2417](https://github.com/Pycord-Development/pycord/pull/2417))
- Fixed `Entitlement.delete` not passing all the needed parameters to its HTTP method.
([#2438](https://github.com/Pycord-Development/pycord/pull/2438))
- Fixed parameter `embed=None` causing `AttributeError` on `PartialMessage.edit`.
([#2446](https://github.com/Pycord-Development/pycord/pull/2446))
- Fixed paginator to revert state if a page update callback fails.
([#2448](https://github.com/Pycord-Development/pycord/pull/2448))
- Fixed missing `application_id` in `Entitlement.delete`.
([#2458](https://github.com/Pycord-Development/pycord/pull/2458))

### Changed

Expand Down
32 changes: 22 additions & 10 deletions discord/ext/bridge/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ class BridgeExtCommand(Command):
def __init__(self, func, **kwargs):
super().__init__(func, **kwargs)

# TODO: v2.7: Remove backwards support for Option in bridge commands.
for name, option in self.params.items():
if isinstance(option.annotation, Option) and not isinstance(
option.annotation, BridgeOption
):
# Warn not to do this
warn_deprecated(
"Using Option for bridge commands",
"BridgeOption",
"2.5",
"2.7",
reference="https://github.com/Pycord-Development/pycord/pull/2417",
stacklevel=6,
)
# Override the convert method of the parameter's annotated Option.
# We can use the convert method from BridgeOption, and bind "self"
# using a manual invocation of the descriptor protocol.
# Definitely not a good approach, but gets the job done until removal.
self.params[name].annotation.convert = BridgeOption.convert.__get__(
self.params[name].annotation
)

async def dispatch_error(self, ctx: BridgeExtContext, error: Exception) -> None:
await super().dispatch_error(ctx, error)
ctx.bot.dispatch("bridge_command_error", ctx, error)
Expand Down Expand Up @@ -653,13 +675,3 @@ def decorator(func):
return func

return decorator


discord.commands.options.Option = BridgeOption
discord.Option = BridgeOption
warn_deprecated(
"Option",
"BridgeOption",
"2.5",
reference="https://github.com/Pycord-Development/pycord/pull/2417",
)
93 changes: 50 additions & 43 deletions discord/ext/pages/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
from typing import List

import discord
from discord.errors import DiscordException
from discord.ext.bridge import BridgeContext
from discord.ext.commands import Context
from discord.file import File
from discord.member import Member
from discord.user import User

Expand Down Expand Up @@ -103,26 +105,25 @@ async def callback(self, interaction: discord.Interaction):
interaction: :class:`discord.Interaction`
The interaction created by clicking the navigation button.
"""
new_page = self.paginator.current_page
if self.button_type == "first":
self.paginator.current_page = 0
new_page = 0
elif self.button_type == "prev":
if self.paginator.loop_pages and self.paginator.current_page == 0:
self.paginator.current_page = self.paginator.page_count
new_page = self.paginator.page_count
else:
self.paginator.current_page -= 1
new_page -= 1
elif self.button_type == "next":
if (
self.paginator.loop_pages
and self.paginator.current_page == self.paginator.page_count
):
self.paginator.current_page = 0
new_page = 0
else:
self.paginator.current_page += 1
new_page += 1
elif self.button_type == "last":
self.paginator.current_page = self.paginator.page_count
await self.paginator.goto_page(
page_number=self.paginator.current_page, interaction=interaction
)
new_page = self.paginator.page_count
await self.paginator.goto_page(page_number=new_page, interaction=interaction)


class Page:
Expand Down Expand Up @@ -656,6 +657,20 @@ async def cancel(
else:
await self.message.edit(view=self)

def _goto_page(self, page_number: int = 0) -> tuple[Page, list[File] | None]:
self.current_page = page_number
self.update_buttons()

page = self.pages[page_number]
page = self.get_page_content(page)

if page.custom_view:
self.update_custom_view(page.custom_view)

files = page.update_files()

return page, files

async def goto_page(
self, page_number: int = 0, *, interaction: discord.Interaction | None = None
) -> None:
Expand All @@ -680,42 +695,34 @@ async def goto_page(
:class:`~discord.Message`
The message associated with the paginator.
"""
self.update_buttons()
self.current_page = page_number
if self.show_indicator:
try:
self.buttons["page_indicator"][
"object"
].label = f"{self.current_page + 1}/{self.page_count + 1}"
except KeyError:
pass

page = self.pages[page_number]
page = self.get_page_content(page)
old_page = self.current_page
page, files = self._goto_page(page_number)

if page.custom_view:
self.update_custom_view(page.custom_view)

files = page.update_files()
try:
if interaction:
await interaction.response.defer() # needed to force webhook message edit route for files kwarg support
await interaction.followup.edit_message(
message_id=self.message.id,
content=page.content,
embeds=page.embeds,
attachments=[],
files=files or [],
view=self,
)
else:
await self.message.edit(
content=page.content,
embeds=page.embeds,
attachments=[],
files=files or [],
view=self,
)
except DiscordException:
# Something went wrong, and the paginator couldn't be updated.
# Revert our changes and propagate the error.
self._goto_page(old_page)
raise

if interaction:
await interaction.response.defer() # needed to force webhook message edit route for files kwarg support
await interaction.followup.edit_message(
message_id=self.message.id,
content=page.content,
embeds=page.embeds,
attachments=[],
files=files or [],
view=self,
)
else:
await self.message.edit(
content=page.content,
embeds=page.embeds,
attachments=[],
files=files or [],
view=self,
)
if self.trigger_on_display:
await self.page_action(interaction=interaction)

Expand Down
2 changes: 1 addition & 1 deletion discord/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2039,7 +2039,7 @@ async def edit(self, **fields: Any) -> Message | None:
raise InvalidArgument("Cannot pass both embed and embeds parameters.")

if embed is not MISSING:
fields["embeds"] = [embed.to_dict()]
fields["embeds"] = [] if embed is None else [embed.to_dict()]

if embeds is not MISSING:
fields["embeds"] = [embed.to_dict() for embed in embeds]
Expand Down
4 changes: 1 addition & 3 deletions discord/monetization.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,4 @@ async def delete(self) -> None:
HTTPException
Deleting the entitlement failed.
"""
await self._state.http.delete_test_entitlement(
self._state.application_id, self.id
)
await self._state.http.delete_test_entitlement(self.application_id, self.id)
8 changes: 7 additions & 1 deletion discord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ def warn_deprecated(
since: str | None = None,
removed: str | None = None,
reference: str | None = None,
stacklevel: int = 3,
) -> None:
"""Warn about a deprecated function, with the ability to specify details about the deprecation. Emits a
DeprecationWarning.
Expand All @@ -315,6 +316,8 @@ def warn_deprecated(
reference: Optional[:class:`str`]
A reference that explains the deprecation, typically a URL to a page such as a changelog entry or a GitHub
issue/PR.
stacklevel: :class:`int`
The stacklevel kwarg passed to :func:`warnings.warn`. Defaults to 3.
"""
warnings.simplefilter("always", DeprecationWarning) # turn off filter
message = f"{name} is deprecated"
Expand All @@ -328,7 +331,7 @@ def warn_deprecated(
if reference:
message += f" See {reference} for more information."

warnings.warn(message, stacklevel=3, category=DeprecationWarning)
warnings.warn(message, stacklevel=stacklevel, category=DeprecationWarning)
warnings.simplefilter("default", DeprecationWarning) # reset filter


Expand All @@ -337,6 +340,7 @@ def deprecated(
since: str | None = None,
removed: str | None = None,
reference: str | None = None,
stacklevel: int = 3,
*,
use_qualname: bool = True,
) -> Callable[[Callable[[P], T]], Callable[[P], T]]:
Expand All @@ -356,6 +360,8 @@ def deprecated(
reference: Optional[:class:`str`]
A reference that explains the deprecation, typically a URL to a page such as a changelog entry or a GitHub
issue/PR.
stacklevel: :class:`int`
The stacklevel kwarg passed to :func:`warnings.warn`. Defaults to 3.
use_qualname: :class:`bool`
Whether to use the qualified name of the function in the deprecation warning. If ``False``, the short name of
the function will be used instead. For example, __qualname__ will display as ``Client.login`` while __name__
Expand Down
6 changes: 3 additions & 3 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
-r _.txt
pylint~=3.1.0
pytest~=8.2.0
pylint~=3.2.2
pytest~=8.2.1
pytest-asyncio~=0.23.3
# pytest-order~=1.0.1
mypy~=1.10.0
coverage~=7.5
pre-commit==3.5.0
codespell==2.2.6
codespell==2.3.0
bandit==1.7.8
flake8==7.0.0

0 comments on commit 9110129

Please sign in to comment.