Add ConfirmView utility #6176

Merged
TrustyJAID merged 18 commits from add-confirm-view into V3/develop 2023-06-21 18:47:48 +02:00
TrustyJAID commented 2023-05-25 19:23:16 +02:00 (Migrated from github.com)

closes #6174

Description of the changes

Have the changes in this PR been tested?

Yes

closes #6174 ### Description of the changes ### Have the changes in this PR been tested? <!-- Choose one (remove the line that doesn't apply): --> Yes <!-- If the question doesn't apply (for example, it's not a code change), choose Yes. Please respond to this question truthfully. We do not delay nor reject PRs based on the answer to this question but it allows to better review this PR. -->
flaree (Migrated from github.com) reviewed 2023-05-25 19:34:49 +02:00
flaree (Migrated from github.com) left a comment

Would it be worth adding an on_timeout that sets the buttons to disabled?

Would it be worth adding an on_timeout that sets the buttons to disabled?
TrustyJAID commented 2023-05-25 19:39:02 +02:00 (Migrated from github.com)

I am open to customizing the timeout but I will not be adding support for disabling the buttons automatically. That would require sending the message through the view which I feel takes away from the simplicity of this. The buttons when interacted with will remove the buttons from the message but timeout will do nothing and it should be up to the user to handle that I think.

I am open to customizing the timeout but I will not be adding support for disabling the buttons automatically. That would require sending the message through the view which I feel takes away from the simplicity of this. The buttons when interacted with will remove the buttons from the message but timeout will do nothing and it should be up to the user to handle that I think.
flaree commented 2023-05-25 19:41:23 +02:00 (Migrated from github.com)

Huh, I have a confirm that doesn't send through a view and works fine.

async def on_timeout(self):
        for item in self.children:
            item.disabled = True
Huh, I have a confirm that doesn't send through a view and works fine. ```py async def on_timeout(self): for item in self.children: item.disabled = True ```
TrustyJAID commented 2023-05-25 19:42:17 +02:00 (Migrated from github.com)

That would disable the button and prevent any response but would still require editing the original message which this does not have any reference to.

That would disable the button and prevent any response but would still require editing the original message which this does not have any reference to.
TrustyJAID commented 2023-05-26 16:14:53 +02:00 (Migrated from github.com)

I have reworked this slightly so we can know if the buttons were not pressed so users can clean them up however they wish, whether that be disabling the buttons or removing them after the timeout.

I have reworked this slightly so we can know if the buttons were not pressed so users can clean them up however they wish, whether that be disabling the buttons or removing them after the timeout.
Jackenmen commented 2023-06-20 12:27:45 +02:00 (Migrated from github.com)

It shouldn't be required (i.e. we should only try doing it if not None) but I think that it would be simple enough to allow setting view.message attribute after the message is sent and remove the view from the message on_timeout in such a case. I would also suggest updating the relevant example to use that, i.e.

view = ConfirmView(ctx.author)
view.message = await ctx.send("Are you sure you about that?", view=view)
await view.wait()
if view.result:
    await ctx.send("Okay I will do that.")
else:
    await ctx.send("I will not be doing that then.")
It shouldn't be required (i.e. we should only try doing it if not `None`) but I think that it would be simple enough to allow setting `view.message` attribute after the message is sent and remove the view from the message `on_timeout` in such a case. I would also suggest updating the relevant example to use that, i.e. ```py view = ConfirmView(ctx.author) view.message = await ctx.send("Are you sure you about that?", view=view) await view.wait() if view.result: await ctx.send("Okay I will do that.") else: await ctx.send("I will not be doing that then.") ```
Jackenmen (Migrated from github.com) requested changes 2023-06-20 21:57:31 +02:00
@ -431,3 +431,190 @@ class SetApiView(discord.ui.View):
return await interaction.response.send_modal(
Jackenmen (Migrated from github.com) commented 2023-06-20 21:45:13 +02:00

That's a weird order, comments generally are placed before the action they talk about:

            # we can't do anything here if message is none
            return
That's a weird order, comments generally are placed before the action they talk about: ```suggestion # we can't do anything here if message is none return ```
Jackenmen (Migrated from github.com) commented 2023-06-20 21:46:42 +02:00

I don't know what this is supposed to convey, making it if not self.disable_buttons with an early return would have been no different so I don't see any prioritization here.

I don't know what this is supposed to convey, making it `if not self.disable_buttons` with an early return would have been no different so I don't see any prioritization here.
Jackenmen (Migrated from github.com) commented 2023-06-20 21:48:34 +02:00

These have been changed to no longer remove the view from the message, why is that?

These have been changed to no longer remove the view from the message, why is that?
Jackenmen (Migrated from github.com) commented 2023-06-20 21:51:08 +02:00

That's a weird order, comments generally are placed before the action they talk about.
The comment also seemed a bit unclear so here's a suggested rephrase:

        # Attach the message to the view after sending it.
        # This way, the view will be automatically removed
        # from the message after the timeout.
        view.message = await ctx.send("Are you sure you about that?", view=view)
That's a weird order, comments generally are placed before the action they talk about. The comment also seemed a bit unclear so here's a suggested rephrase: ```suggestion # Attach the message to the view after sending it. # This way, the view will be automatically removed # from the message after the timeout. view.message = await ctx.send("Are you sure you about that?", view=view) ```
Jackenmen (Migrated from github.com) commented 2023-06-20 21:52:33 +02:00

It would probably make sense to explain the default behavior:

        Whether to disable the buttons instead of removing them from the message after the timeout.
        Defaults to ``False``.
It would probably make sense to explain the default behavior: ```suggestion Whether to disable the buttons instead of removing them from the message after the timeout. Defaults to ``False``. ```
Jackenmen (Migrated from github.com) commented 2023-06-20 21:54:17 +02:00
        Whether to disable the buttons instead of removing them on timeout
        (if the ``message`` attribute has been set on the view).
```suggestion Whether to disable the buttons instead of removing them on timeout (if the ``message`` attribute has been set on the view). ```
Jackenmen (Migrated from github.com) commented 2023-06-20 21:55:16 +02:00

This may be unnecessary but the sentence read weirdly to me:

        nothing will happen in ``on_timeout``, if the view is never interacted with.
This may be unnecessary but the sentence read weirdly to me: ```suggestion nothing will happen in ``on_timeout``, if the view is never interacted with. ```
Jackenmen (Migrated from github.com) commented 2023-06-20 21:56:08 +02:00

Is this meant to be a public method? If so, please document it, otherwise prefix it with _.

Is this meant to be a public method? If so, please document it, otherwise prefix it with `_`.
Jackenmen (Migrated from github.com) commented 2023-06-20 21:57:23 +02:00
    Auto-disable the buttons after timeout if nothing is pressed::
```suggestion Auto-disable the buttons after timeout if nothing is pressed:: ```
Jackenmen (Migrated from github.com) reviewed 2023-06-20 21:59:22 +02:00
@ -11,7 +11,7 @@ from redbot.core.commands.converter import get_dict_converter
if TYPE_CHECKING:
Jackenmen (Migrated from github.com) commented 2023-06-20 21:59:22 +02:00

nitpick: we try to keep items in __all__ in definition order to make it somewhat easier to check if anything is missing in __all__:

__all__ = ("SimpleMenu", "SetApiModal", "SetApiView", "ConfirmView")
nitpick: we try to keep items in `__all__` in definition order to make it somewhat easier to check if anything is missing in `__all__`: ```suggestion __all__ = ("SimpleMenu", "SetApiModal", "SetApiView", "ConfirmView") ```
Jackenmen (Migrated from github.com) requested changes 2023-06-21 02:54:00 +02:00
Jackenmen (Migrated from github.com) left a comment

There's mostly a bunch of docstring fixes left but I also commented with one implementation question.

There's mostly a bunch of docstring fixes left but I also commented with one implementation question.
@ -431,3 +431,190 @@ class SetApiView(discord.ui.View):
return await interaction.response.send_modal(
Jackenmen (Migrated from github.com) commented 2023-06-21 02:43:04 +02:00

This may seem like a nitpick but this is actually a very important fix to how Sphinx ends up displaying this:

        Defaults to ``False``.

image

This may seem like a nitpick but this is actually a very important fix to how Sphinx ends up displaying this: ```suggestion Defaults to ``False``. ``` ![image](https://github.com/Cog-Creators/Red-DiscordBot/assets/6032823/9d54f98b-1810-4f20-87a8-19fdaed9fc42)
Jackenmen (Migrated from github.com) commented 2023-06-21 02:47:41 +02:00
    A simple `discord.ui.View` used for confirming something.
```suggestion A simple `discord.ui.View` used for confirming something. ```
Jackenmen (Migrated from github.com) commented 2023-06-21 02:50:19 +02:00
        nothing will happen in `on_timeout()`, if the view is never interacted with.
```suggestion nothing will happen in `on_timeout()`, if the view is never interacted with. ```
Jackenmen (Migrated from github.com) commented 2023-06-21 02:50:31 +02:00
        A `discord.ui.Button` to confirm the message.
```suggestion A `discord.ui.Button` to confirm the message. ```
Jackenmen (Migrated from github.com) commented 2023-06-21 02:50:37 +02:00
        Changing the style and label of this `discord.ui.Button`::
```suggestion Changing the style and label of this `discord.ui.Button`:: ```
Jackenmen (Migrated from github.com) commented 2023-06-21 02:50:46 +02:00
        A `discord.ui.Button` to dismiss the message.
```suggestion A `discord.ui.Button` to dismiss the message. ```
Jackenmen (Migrated from github.com) commented 2023-06-21 02:50:53 +02:00
        Changing the style and label of this `discord.ui.Button`::
```suggestion Changing the style and label of this `discord.ui.Button`:: ```
Jackenmen (Migrated from github.com) commented 2023-06-21 02:52:04 +02:00
        (if the `message` attribute has been set on the view).
```suggestion (if the `message` attribute has been set on the view). ```
Jackenmen (Migrated from github.com) commented 2023-06-21 02:42:54 +02:00

Looking at the documentation, it seems that it's consistent with it but I do wonder if we actually want to make the behavior of the view's timeout action dependent on whether some random person other than the author decides to click one of the buttons under the message? Would it not make more sense to this API's consumers to expect that not setting view.message will fully prevent it from removing/disabling the buttons on the message?

Looking at the documentation, it seems that it's consistent with it but I do wonder if we actually want to make the behavior of the view's timeout action dependent on whether some random person other than the author decides to click one of the buttons under the message? Would it not make more sense to this API's consumers to expect that not setting `view.message` will fully prevent it from removing/disabling the buttons on the message?
TrustyJAID (Migrated from github.com) reviewed 2023-06-21 03:12:33 +02:00
TrustyJAID (Migrated from github.com) commented 2023-06-21 03:12:33 +02:00

I suppose my original thinking was to prefer cleanup of the view in as many situations as possible. For example, a cog sends the view without explicitly setting the message attribute, if the issuer of the command doesn't click the button but someone else does it would be better to then cleanup the view even if it doesn't actually respond to them. I view it as better in that the buttons no longer sit around unusable for anyone. I could move it under the author check to be more consistent and keep everything else the same I suppose. Though now I also wonder if I should be responding to someone who isn't the author with a message as well.

I suppose my original thinking was to prefer cleanup of the view in as many situations as possible. For example, a cog sends the view without explicitly setting the message attribute, if the issuer of the command doesn't click the button but someone else does it would be better to then cleanup the view even if it doesn't actually respond to them. I view it as better in that the buttons no longer sit around unusable for anyone. I could move it under the author check to be more consistent and keep everything else the same I suppose. Though now I also wonder if I should be responding to someone who isn't the author with a message as well.
Jackenmen (Migrated from github.com) reviewed 2023-06-21 03:19:48 +02:00
Jackenmen (Migrated from github.com) commented 2023-06-21 03:19:48 +02:00

Though now I also wonder if I should be responding to someone who isn't the author with a message as well.

I haven't thought about it but it would definitely make it seem not broken to bystanders. And it would be more consistent with all the other views/modals that we already have (just the simple menu and set api so far).

I could move it under the author check to be more consistent and keep everything else the same I suppose.

It seems like an alright way of handling it. But with your explanation, I honestly am not sure if we should do that or just leave it as is (since I guess that the option of not trying to do the cleanup at all is not something we're interested in).

> Though now I also wonder if I should be responding to someone who isn't the author with a message as well. I haven't thought about it but it would definitely make it seem not broken to bystanders. And it would be more consistent with all the other views/modals that we already have (just the simple menu and set api so far). > I could move it under the author check to be more consistent and keep everything else the same I suppose. It seems like an alright way of handling it. But with your explanation, I honestly am not sure if we should do that or just leave it as is (since I guess that the option of not trying to do the cleanup at all is not something we're interested in).
TrustyJAID (Migrated from github.com) reviewed 2023-06-21 03:38:41 +02:00
TrustyJAID (Migrated from github.com) commented 2023-06-21 03:38:41 +02:00

I added a response if someone other than the author attempts to interact with the buttons. I left in setting the message object from any interaction though. I am much happier with the way this would work now and I would rather prioritize removing or disabling the view in as many situations as possible. Leaving broken buttons lying around outside of development and testing just feels bad.

I added a response if someone other than the author attempts to interact with the buttons. I left in setting the message object from any interaction though. I am much happier with the way this would work now and I would rather prioritize removing or disabling the view in as many situations as possible. Leaving broken buttons lying around outside of development and testing just feels bad.
Jackenmen (Migrated from github.com) approved these changes 2023-06-21 17:28:14 +02:00
Jackenmen (Migrated from github.com) left a comment

This new utility works as expected so from my side, this is good to go.

I would, however, like you to look at the rendered documentation and assess whether the current version of it is acceptable:
https://red-discordbot--6176.org.readthedocs.build/en/6176/framework_utils.html#redbot.core.utils.views.ConfirmView

Basically, I looked at the rendered documentation and thought that showing the documentation from the base class on our overridden version of the methods was confusing and have instead opted to document it with the actual behavior of them. Additionally, I documented confirm/dismiss_button as a regular attribute of the Button type since that's what it actually ends up being on the instance of this view.

If everything seems fine to you, feel free to just merge this.

This new utility works as expected so from my side, this is good to go. I would, however, like you to look at the rendered documentation and assess whether the current version of it is acceptable: https://red-discordbot--6176.org.readthedocs.build/en/6176/framework_utils.html#redbot.core.utils.views.ConfirmView Basically, I looked at the rendered documentation and thought that showing the documentation from the base class on our overridden version of the methods was confusing and have instead opted to document it with the actual behavior of them. Additionally, I documented `confirm/dismiss_button` as a regular attribute of the `Button` type since that's what it actually ends up being on the instance of this view. If everything seems fine to you, feel free to just merge this.
Sign in to join this conversation.
No reviewers
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!6176
No description provided.