Skip to content

Components V2 #10166

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

Open
wants to merge 114 commits into
base: master
Choose a base branch
from
Open

Components V2 #10166

wants to merge 114 commits into from

Conversation

DA-344
Copy link
Contributor

@DA-344 DA-344 commented Apr 18, 2025

Summary

Adds support for discord's components v2.

Testing and bug reporting is very much appreciated!

DDevs PR: discord/discord-api-docs#7487

  1. Adds the following Components -> UI Version:
  • SectionComponent -> ui.Section
  • TextDisplay -> ui.TextDisplay
  • ThumbnailComponent -> ui.Thumbnail
  • MediaGalleryComponent -> ui.MediaGallery
  • FileComponent -> ui.File
  • SeparatorComponent -> ui.Separator
  • Container -> ui.Container
  • ActionRow -> ui.ActionRow
  1. Adds a new ui view like class, ui.LayoutView, that allows for these components to exist.
  2. Adds the id attribute to all Components and UI Components.
    -> This also adds the get_item_by_id method to ui.View, ui.LayoutView, ui.Container, and ui.Section.
  3. Deprecates ui.View in favour of ui.LayoutView.

You can install this branch for testing using py -m pip install -U git+https://github.com/DA-344/d.py@feat/components-v2

These changes have been tested with the following snippets:

All of these use the jishaku package

Sending Components v2

Code:

class TestButton(discord.ui.Button):
    async def callback(self, interaction):
        await interaction.response.send_message('t')

class TestContainer(discord.ui.Container):
    text1 = discord.ui.TextDisplay("Hello world")
    text2 = discord.ui.TextDisplay("Row 3", row=2)

    section = discord.ui.Section(
        accessory=TestButton(
            label="Section Button",
        )
    ).add_item(discord.ui.TextDisplay("Text in a section"))


class TestView(discord.ui.LayoutView):
    container = TestContainer(id=1)

await _ctx.send(view=TestView())

Result (as expected):
cv2 ex

Sending Components v2 items on webhooks

1st snippet: sends interactible items on app owned and not app owned webhooks

wh = await _channel.create_webhook(name='sumth')  # app owned

class TestButton(discord.ui.Button):
    async def callback(self, interaction):
        await interaction.response.send_message('t')

class TestContainer(discord.ui.Container):
    text1 = discord.ui.TextDisplay("Hello world")
    text2 = discord.ui.TextDisplay("Row 3", row=2)

    section = discord.ui.Section(
        accessory=TestButton(
            label="Section Button",
        )
    ).add_item(discord.ui.TextDisplay("Text in a section"))


class TestView(discord.ui.LayoutView):
    container = TestContainer(id=1)

v = TestView()

await wh.send(view=v)

wh2 = discord.Webhook.from_url('https://discord.com/api/webhooks/1362896342581641286/TOKEN', client=_bot)  # not app owned

await wh2.send(view=TestView())

Result (as expected):
owner wh cv2

2nd snippet: sends non-interactible items on not app owned webhook

class TestButton(discord.ui.Button):
    async def callback(self, interaction):
        await interaction.response.send_message('t')

class TestContainer(discord.ui.Container):
    text1 = discord.ui.TextDisplay("Hello world")
    text2 = discord.ui.TextDisplay("Row 3", row=2)

    section = discord.ui.Section(
        accessory=TestButton(
            url="https://google.com",
            label='gugol',
        )
    ).add_item(discord.ui.TextDisplay("Text in a section"))


class TestView(discord.ui.LayoutView):
    container = TestContainer(id=1)

wh2 = discord.Webhook.from_url('https://discord.com/api/webhooks/1362896342581641286/TOKEN', client=_bot)

await wh2.send(view=TestView())

Result (as expected):
not owner wh cv2

Receiving other message's components v2

Code:

yield _message.reference.resolved.components

Result (as expected):
receiving cv2

Checklist

  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • 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, ...)

TextDisplay attribute error when doing TextDisplay.to_component_dict()
View.to_components() appending Item objects instead of Item.to_component_dict()
Changed View.to_components() sorting key
@jakobdylanc
Copy link

With these changes is there a straightforward way to send the simplest form of a components v2 message, aka just plaintext content?

The reason for wanting this is because the character limit would be 4000 instead of the old 2000.

It would be nice if the character limit of the existing "content" field was increased to 4000; and just make it use components v2 under the hood if it has to.

@DA-344
Copy link
Contributor Author

DA-344 commented Apr 25, 2025

You can make your own LayoutView subclass and automatically assign it a content with a TextDisplay, as making content be a shortcut for TextDisplay would make it not be consistent with the API.

@theProgramer-J
Copy link

I have a problem : I cannot get all components from a message
image
image
I want to get container and text display components

@Soheab
Copy link
Contributor

Soheab commented Apr 25, 2025

I have a problem : I cannot get all components from a message image image I want to get container and text display components

This PR hasn't even been merged yet; how did you already get the docs lol? Anyway, with this PR, Message.components does return all the new components too. Have you tried it?

@theProgramer-J
Copy link

theProgramer-J commented Apr 25, 2025

Yes I tried and I get [ ]

@Soheab
Copy link
Contributor

Soheab commented Apr 25, 2025

Yes I tried and I get [ ]

Code?

Also, consider joining the support server and reporting this in the dedicated discussion thread.

@theProgramer-J
Copy link

I had forgotten to py -m pip install -U git+https://github.com/DA-344/d.py@feat/components-v2 lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants