Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add modals and text input #253

Merged
merged 73 commits into from
Feb 10, 2022

Conversation

Victorsitou
Copy link
Member

@Victorsitou Victorsitou commented Jan 6, 2022

Summary

Bored of using wait_for("message") to get user input? I present you Modals, a new way to asking for user input, along with TextInput, a component to write text (These can only be used in modals).

Examples
from disnake.ui.text_input import TextInput
from typing import List

# High-level implementation.

class Tag(disnake.ui.Modal):
    def __init__(self) -> None:

        components: List[TextInput] = [
            disnake.ui.TextInput(
                style=disnake.TextInputStyle.short,
                label="Name",
                placeholder="The name of the tag.",
                custom_id="name",
                max_length=50,
            ),
            disnake.ui.TextInput(
                style=disnake.TextInputStyle.long,
                label="Content",
                placeholder="The content of the tag.",
                custom_id="content",
            ),
        ]

        super().__init__(title="Tag creation", custom_id="tag_creation", components=components)

    async def callback(self, inter: disnake.ModalInteraction) -> None:
        embed = disnake.Embed(title="Tag Creation")
        for key, value in inter.text_values.items():
            embed.add_field(name=key.capitalize(), value=value, inline=False)
        await inter.response.send_message(embed=embed)

    async def on_error(self, error: Exception, inter: disnake.ModalInteraction) -> None:
        await inter.response.send_message("Oops, something went wrong.", ephemeral=True)

@bot.slash_command()
async def create_tag(inter: disnake.AppCmdInter) -> None:
    await inter.response.send_modal(Tag())


# Same example but with low-level implementation.

@bot.slash_command()
async def create_tag_low(inter: disnake.AppCmdInter) -> None:
    await inter.response.send_modal(
        title="Tag creation",
        custom_id="tag_creation",
        components=[
            disnake.ui.TextInput(
                style=disnake.TextInputStyle.short,
                label="Name",
                placeholder="The name of the tag.",
                custom_id="name",
                max_length=50,
            ),
            disnake.ui.TextInput(
                style=disnake.TextInputStyle.long,
                label="Content",
                placeholder="The content of the tag.",
                custom_id="content",
            ),
        ],
    )
    modal_inter: disnake.ModalInteraction = await bot.wait_for(
        "modal_submit",
        check=lambda i: i.author == inter.author and i.custom_id == "tag_creation",
    )
    embed = disnake.Embed(title="Tag Creation")
    for key, value in modal_inter.text_values.items():
        embed.add_field(name=key.capitalize(), value=value, inline=False)
    await modal_inter.response.send_message(embed=embed)

Final result
image

TODO list

  • Add modals. (disnake.ui.Modal)
  • Add text input. (disnake.ui.TextInput and disnake.components.TextInput)
  • Add ModalInteraction (And ModalInteractionData).
  • Add InteractionRespone.send_modal.
  • New ModalChaingNotSupported error
  • Add documentation.
  • Tests once it's released.

At the moment only TextInput is supported in modals, with a maximum of 5 action rows; 1 component per row.

And to finish I want to thanks zhu for helping me to test this when we didn't have access to the modals beta.

Closes #212

Checklist

  • If code changes were made, then they have been tested
    • I have updated the documentation to reflect the changes
    • I have formatted the code properly by running pre-commit run --all-files
  • This PR fixes an issue
  • This PR adds something new (e.g. new method or parameters)
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, ...)

@Victorsitou Victorsitou added t: api support Support of Discord API features t: enhancement New feature s: in progress Issue/PR is being worked on s: waiting for api/docs Issue/PR is waiting for API support/documentation labels Jan 6, 2022
Since Discord doesn't dispatch this event, it wouldn't make sense to dispatch it, as it isn't accurate with the actual close event.
@EQUENOS
Copy link
Member

EQUENOS commented Jan 7, 2022

Good stuff! There're still things that should be slightly changed but I can't explain them in a review. I'll make a PR to your repo later.

Copy link
Member

@shiftinv shiftinv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat, some pretty cool changes!
I've got just a few comments, some of which aren't about modals themselves but about WrappedComponent and handling of actionrows, and a few things will probably require further discussion

disnake/interactions/base.py Outdated Show resolved Hide resolved
disnake/interactions/base.py Outdated Show resolved Hide resolved
disnake/interactions/base.py Outdated Show resolved Hide resolved
disnake/interactions/base.py Outdated Show resolved Hide resolved
disnake/interactions/modal.py Outdated Show resolved Hide resolved
disnake/ui/modal.py Outdated Show resolved Hide resolved
disnake/ui/modal.py Outdated Show resolved Hide resolved
disnake/ui/modal.py Show resolved Hide resolved
disnake/ui/modal.py Outdated Show resolved Hide resolved
disnake/state.py Show resolved Hide resolved
@EQUENOS EQUENOS changed the title Add support for modals and input text. feat!: add modals Feb 1, 2022
@Victorsitou Victorsitou changed the title feat!: add modals feat!: add modals and input text Feb 1, 2022
disnake/ui/action_row.py Outdated Show resolved Hide resolved
disnake/ui/action_row.py Outdated Show resolved Hide resolved
Copy link
Member

@EQUENOS EQUENOS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost every collaborator worked on this. We can finally call it a complete implementation of modals and text inputs.

Copy link
Member

@shiftinv shiftinv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@EQUENOS EQUENOS merged commit bb443ae into DisnakeDev:master Feb 10, 2022
@onerandomusername onerandomusername removed the s: needs review Issue/PR is awaiting reviews label Apr 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
t: api support Support of Discord API features t: enhancement New feature t: refactor/typing/lint Refactors, typing changes and/or linting changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for modals and input texts.
4 participants