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

fix: component sending/editing with context #396

Merged
merged 22 commits into from Jan 2, 2022
Merged

fix: component sending/editing with context #396

merged 22 commits into from Jan 2, 2022

Conversation

Toricane
Copy link
Contributor

About

This pull request is about fixing component sending/editing with context, which lets you send/edit components using the context (ctx) properly.
Specifically await ctx.send(...) and await ctx.edit(...).

In addition to fixing component sending/editing, I have also filled in missing ways to send/edit components:

Sending/editing one ActionRow

Here is a sample code example where you can send and edit one ActionRow:

await ctx.send("c", components=button_row1)
await asyncio.sleep(1)
await ctx.edit("c", components=button_row2)

Result:

Sending/editing individual components

Sample code:

await ctx.send("b", components=select)
await asyncio.sleep(1)
await ctx.edit("b", components=button1)
await asyncio.sleep(1)
await ctx.edit("b", components=select)
await asyncio.sleep(1)
await ctx.send("b", components=button1)

Result:

Sending/editing multiple ActionRows

Sample code:

await ctx.send("lol", components=[button_row1, button_row2])
await asyncio.sleep(1)
await ctx.edit("ayo", components=[button_row2, button_row1])
await asyncio.sleep(1)
await ctx.edit("wow", components=[select_row1, select_row2])
await asyncio.sleep(1)
await ctx.edit("ez", components=[select_row2, select_row1])

Result:

Sending/editing with lists

Sample code:

await ctx.send("a", components=[[button1, button2], [select]])
await asyncio.sleep(1)
await ctx.edit("a", components=[[select], [button1, button2]])

Result:

Finally, removing components

When editing, the components stay if components=None, but anything else that is not a list of action rows, a list of components, component, action row, or a list of a list of components (example: []), the components will be removed from the message.
Sample code:

await ctx.send("hi", components=button1)
await asyncio.sleep(1)
await ctx.edit("hi", components=None)
await asyncio.sleep(1)
await ctx.edit("hi", components=[])

Result:

Please review this code and tell me if I have done anything incorrectly.

Checklist

  • I've ran pre-commit to format and lint the change(s) made.
  • I've checked to make sure the change(s) work on 3.8.6 and higher.
  • This fixes/solves an Issue.
    • (If existent):
  • I've made this pull request for/as: (check all that apply)
    • Documentation
    • Breaking change
    • New feature/enhancement
    • Bugfix

@i0bs
Copy link
Contributor

i0bs commented Dec 21, 2021

Since you've implemented mutable tables to the components argument for sending, can you test interactions.ActionRow() working alongside []?

@Toricane
Copy link
Contributor Author

You mean like [[button1], ActionRow(components=[button2])?
Yeah I haven't implemented that, I will do it

@Toricane
Copy link
Contributor Author

Okay @goverfl0w, I have implemented it.
Code example:

await ctx.send(
    "testing",
    components=[[button1, button2], interactions.ActionRow(components=[select])],
)
await asyncio.sleep(1)
await ctx.edit(
    "wow",
    components=[interactions.ActionRow(components=[select]), [button2, button]],
)

Result:

@Toricane
Copy link
Contributor Author

I think there may be an issue for checking if custom_id is in the button, the button may be a URL button. I think I can solve this by checking if there is a URL in the button after checking if there is a custom_id.

@Toricane
Copy link
Contributor Author

Yep it works, used or component._json.get("url")

interactions/context.py Outdated Show resolved Hide resolved
]
elif isinstance(components, (Button, SelectMenu)):
if isinstance(components, SelectMenu):
components._json["options"] = [option._json for option in components.options]
Copy link
Contributor

Choose a reason for hiding this comment

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

components._json["options"] should not need to be overwritten as of the latest unstable commit, since we have a recursive generator being applied.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed lines 257 and 258, and the respective lines in edit() as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah @goverfl0w, this is why I put it in the first place:

ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-16' coro=<my_command() done, defined at c:\Users\hi\Desktop\Coding\Python\discord-interactions v4\main.py:31> exception=TypeError('Object of type SelectOption is not JSON serializable')>
Traceback (most recent call last):
  File "c:\Users\hi\Desktop\Coding\Python\discord-interactions v4\main.py", line 143, in my_command
    await ctx.send("b", components=select)
  File "C:\Users\hi\AppData\Local\Programs\Python\Python39\lib\site-packages\interactions\context.py", line 343, in send
    await func()
  File "C:\Users\hi\AppData\Local\Programs\Python\Python39\lib\site-packages\interactions\context.py", line 336, in func
    await self.client.create_interaction_response(
  File "C:\Users\hi\AppData\Local\Programs\Python\Python39\lib\site-packages\interactions\api\http.py", line 2061, in create_interaction_response
    return await self._req.request(
  File "C:\Users\hi\AppData\Local\Programs\Python\Python39\lib\site-packages\interactions\api\http.py", line 200, in request
    async with self.session.request(
  File "C:\Users\hi\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\client.py", line 1117, in __aenter__
    self._resp = await self._coro
  File "C:\Users\hi\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\client.py", line 390, in _request
    data = payload.JsonPayload(json, dumps=self._json_serialize)
  File "C:\Users\hi\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\payload.py", line 381, in __init__
    dumps(value).encode(encoding),
  File "C:\Users\hi\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "C:\Users\hi\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Users\hi\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "C:\Users\hi\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type SelectOption is not JSON serializable

I'm gonna revert the commit and keep the lines and try again

interactions/context.py Outdated Show resolved Hide resolved
Copy link
Contributor

@i0bs i0bs left a comment

Choose a reason for hiding this comment

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

Just noticed, this still has not been fixed:

        components: Optional[
            Union[Union[ActionRow, Button, SelectMenu], List[Union[ActionRow, Button, SelectMenu]]]
        ]

You have Union repeating twice.

@Toricane
Copy link
Contributor Author

Toricane commented Jan 2, 2022

I have resolved the changes, I will test them now

@i0bs i0bs merged commit ffc9865 into interactions-py:unstable Jan 2, 2022
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.

None yet

2 participants