feat: components v2 & `View` improvements
Summary
Soon
- Edit by Lala: Temporary reference: https://github.com/Lulalaby/discord-api-docs/blob/comp_v2/docs/interactions/Message_Components.md#v2-components
- Yet another edit by Lala: Official PR for the docs is available now at https://github.com/discord/discord-api-docs/pull/7487
Information
- [ ] This PR fixes an issue.
- [x] 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, typehinting, examples, ...).
Checklist
- [x] I have searched the open pull requests for duplicates.
- [ ] If code changes were made then they have been tested.
- [ ] I have updated the documentation to reflect the changes.
- [ ] If
type: ignorecomments were used, a comment is also left explaining why. - [ ] I have updated the changelog to include these changes.
Reference:
note to self, https://github.com/discord/discord-api-docs/pull/7368 would be appropriate here too (webhooks may make full use of the new V2 components and link buttons, but not the existing interactive components)
yeah agreed. we should get that in as well. i originally included that in my discord-docs, but since it was kinda separate, i removed it again since advaith made a separate pr
todo: changelog, example
pls test within whatever limits you can
https://discord.com/channels/881207955029110855/881735314987708456/1344584270601261067
if you get any errors apart from maze then ping me there
Sometimes when parsing a received components the code crashes here saying no attribute custom_id:
https://github.com/Pycord-Development/pycord/blob/8fdf18ec0b7d47d395194470b21621ebc6b6d971/discord/ui/view.py#L464
Sometimes when parsing a received components the code crashes here saying no attribute
custom_id:https://github.com/Pycord-Development/pycord/blob/8fdf18ec0b7d47d395194470b21621ebc6b6d971/discord/ui/view.py#L464
that's odd, is_dispatchable should mean it has a custom id
Yeah I thought the same
Experiment flag has been lifted; this PR can now be freely tested by any user. You may have to force canary API; this can be done by running the below line in your main bot file.
discord.Route.API_BASE_URL = "https://canary.discord.com/api/v10"
Additionally, examples in the example/ui directory should be something to consider.
Additionally, examples in the
example/uidirectory should be something to consider.
This could be done in a separate PR to not delay this PR.
When using a decorator in a container, should the items appear above or below items set in __init__
Can row not be used? But I would say below when not specified.
Is there a way to add your own ActionRow?
The button limit was not increased correctly, this could most likely be fixed by allowing manual ActionRow creation and not bother fixing the weights system as of now (as suggested by RealBongoChongo). The 26th button is what causes the error.
Code
```py v = discord.ui.View() for i in range(33): v.add_item(discord.ui.Button(label=str(i))) ```Traceback
``` Traceback (most recent call last): File "site-packages\discord\commands\core.py", line 138, in wrapped ret = await coro(arg) ^^^^^^^^^^^^^^^ File "site-packages\discord\commands\core.py", line 1082, in _invoke await self.callback(ctx, **kwargs) File "PycordTesting\components_v2.py", line 24, in components_v2 v.add_item(discord.ui.Button(label=str(i))) File "site-packages\discord\ui\view.py", line 341, in add_item self.__weights.add_item(item) File "site-packages\discord\ui\view.py", line 145, in add_item index = self.find_open_space(item) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "site-packages\discord\ui\view.py", line 131, in find_open_space raise ValueError("could not find open space for item") ValueError: could not find open space for item ```
Implementing ui.ActionRow alongside weights is not ideal and not a goal for 2.7; this will be added in v3.
Not copilot being almost completely worthless on this mildly large PR
hi, I am just trying out this addition with the default bot example, and I am recieving a 404 error. Is this an known or unknown problem? -> moved to Pycord Discord
@DTheIcyDragon Heyy, would you mind asking in the discord server https://discord.gg/pycord as this issue is not related to this pull request specifically ?
Would it be possible to not include timeout handling in views without an interactive component. I dont think there is any reason to save the view in the cache when there are no interactive components. This would also save a lot of memory in active bots that send a lot of containers in place of embeds where the current behavior saves the view for 3 minutes.
I've now started to implement this PR in the Beta version of my bot, and I have a question: Is it possible to add an item to a container via decorator? Or is self.container.add_item(callback) the only way?
Is it possible to add an item to a container via decorator?
@button and @select work when subclassing container, same usage as in views
Would it be possible to not include timeout handling in views without an interactive component
This has been added through the new View.is_dispatchable() function - non-dispatchable views will no longer automatically call store_view when being sent
Kinda hefty but since this is already a large pr on Views i added some other conveniences
- New
command,viewandmodalattributes onInteraction - New events
on_view_errorandon_modal_error
Will go through full review comments and suggestions this weekend.
~~View.disable_all_items() does not disable section accessories. Is this intentional?~~
EDIT: MB I was using an older version.
self.disable_all_item() returns error AttributeError: 'MediaGallery' object has no attribute 'disable_all_items'
Task exception was never retrieved future: <Task finished name='discord-ui-view-timeout-a2d765a0e561bfb23f667ef91f67f8ad' coro=<MyView.on_timeout() done, defined at C:\Users\0000\Desktop\0000\main.py:64> exception=AttributeError("'NoneType' object has no attribute 'edit'")> Traceback (most recent call last): File "C:\Users\0000\Desktop\0000\main.py", line 66, in on_timeout await self.message.edit(view=self) AttributeError: 'NoneType' object has no attribute 'edit'
It looks like there's a problem with editing messages.
Task exception was never retrieved future: <Task finished name='discord-ui-view-timeout-a2d765a0e561bfb23f667ef91f67f8ad' coro=<MyView.on_timeout() done, defined at C:\Users\0000\Desktop\0000\main.py:64> exception=AttributeError("'NoneType' object has no attribute 'edit'")> Traceback (most recent call last): File "C:\Users\0000\Desktop\0000\main.py", line 66, in on_timeout await self.message.edit(view=self) AttributeError: 'NoneType' object has no attribute 'edit'
It looks like there's a problem with editing messages.
This is not new. It also happens in the stable version. I've complained about it several times, but they haven't fixed it yet.
This is not new. It also happens in the stable version. I've complained about it several times, but they haven't fixed it yet.
@iqnite Please create a new issue with the necessary information, we'll look into it.
https://github.com/discord/discord-api-spec/commit/2a2605c2df77df198aef8fc78e298a3652cbd746
we have attachment_id on the UnfurledMedia object now
and the file display component has two more fields: size in bytes and name ref https://github.com/discord/discord-api-docs/pull/7609
@NeloBlivion I think something broke with your recent pushes:
if payload["content"] or payload["embeds"]
in ``discord/webhooks/_async.py`
Is causing:
KeyError: 'content'
File "discord/commands/core.py", line 138, in wrapped
ret = await coro(arg)
File "discord/commands/core.py", line 1078, in _invoke
await self.callback(self.cog, ctx, **kwargs)
File "src/extensions/chatcop/cogs/admin/__init__.py", line 260, in view
await ctx.respond(view=view, ephemeral=True, allowed_mentions=discord.AllowedMentions.none())
File "discord/ext/bridge/context.py", line 92, in respond
return await self._respond(*args, **kwargs)
File "discord/ext/bridge/context.py", line 145, in _respond
return await self._get_super("respond")(*args, **kwargs)
File "discord/interactions.py", line 694, in respond
return await self.followup.send(*args, **kwargs)
File "discord/webhook/async_.py", line 1822, in send
params = handle_message_parameters(
File "discord/webhook/async_.py", line 673, in handle_message_parameters
if payload["content"] or payload["embeds"]:
