Add user param to menu() (#4913)

Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com>
Co-authored-by: Jakub Kuczys <me@jacken.men>
This commit is contained in:
Auguste Charpentier
2024-03-18 01:35:53 +01:00
committed by GitHub
parent 4c7a691ec9
commit 8c2976504a
3 changed files with 158 additions and 28 deletions

View File

@@ -136,6 +136,7 @@ class SimpleMenu(discord.ui.View):
super().__init__(
timeout=timeout,
)
self._fallback_author_to_ctx = True
self.author: Optional[discord.abc.User] = None
self.message: Optional[discord.Message] = None
self._source = _SimplePageSource(items=pages)
@@ -192,6 +193,19 @@ class SimpleMenu(discord.ui.View):
def source(self):
return self._source
@property
def author(self) -> Optional[discord.abc.User]:
if self._author is not None:
return self._author
if self._fallback_author_to_ctx:
return getattr(self.ctx, "author", None)
return None
@author.setter
def author(self, value: Optional[discord.abc.User]) -> None:
self._fallback_author_to_ctx = False
self._author = value
async def on_timeout(self):
try:
if self.delete_after_timeout and not self.message.flags.ephemeral:
@@ -225,19 +239,38 @@ class SimpleMenu(discord.ui.View):
options = self.select_options[:25]
return _SelectMenu(options)
async def start(self, ctx: Context, *, ephemeral: bool = False):
async def start(
self, ctx: Context, *, user: Optional[discord.abc.User] = None, ephemeral: bool = False
):
"""
Used to start the menu displaying the first page requested.
.. warning::
The ``user`` parameter is considered `provisional <developer-guarantees-exclusions>`.
If no issues arise, we plan on including it under developer guarantees
in the first release made after 2024-05-18.
Parameters
----------
ctx: `commands.Context`
The context to start the menu in.
user: discord.User
The user allowed to interact with the menu.
If this is ``None``, ``ctx.author`` will be able to interact with the menu.
.. warning::
This parameter is `provisional <developer-guarantees-exclusions>`.
If no issues arise, we plan on including it under developer guarantees
in the first release made after 2024-05-18.
ephemeral: `bool`
Send the message ephemerally. This only works
if the context is from a slash command interaction.
"""
self.author = ctx.author
self._fallback_author_to_ctx = True
if user is not None:
self.author = user
self.ctx = ctx
kwargs = await self.get_page(self.current_page)
self.message = await ctx.send(**kwargs, ephemeral=ephemeral)