Skip to content

Commit

Permalink
feat!: remove wrapped interactions (#32)
Browse files Browse the repository at this point in the history
* feat: remove wrapped interactions

* feat: update examples
  • Loading branch information
Sharp-Eyes authored Jan 28, 2025
1 parent 684f83b commit e64ab5f
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 626 deletions.
8 changes: 4 additions & 4 deletions examples/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CustomisableSelect(components.RichStringSelect):
def __attrs_post_init__(self) -> None:
self.max_values = len(self.options)

async def callback(self, interaction: components.MessageInteraction) -> None:
async def callback(self, interaction: disnake.MessageInteraction) -> None:
selection = (
"\n".join(f"- {value}" for value in interaction.values)
if interaction.values
Expand All @@ -43,9 +43,9 @@ async def make_select(interaction: disnake.CommandInteraction, options: str) ->
await interaction.response.send_message("You must specify at most 25 options!")
return

wrapped = components.wrap_interaction(interaction)
await wrapped.response.send_message(
components=CustomisableSelect(options=actual_options),
component = await CustomisableSelect(options=actual_options).as_ui_component()
await interaction.response.send_message(
components=component,
)


Expand Down
13 changes: 6 additions & 7 deletions examples/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ class MyButton(components.RichButton):

count: int = 0

async def callback(self, interaction: components.MessageInteraction) -> None:
async def callback(self, interaction: disnake.MessageInteraction) -> None:
self.count += 1
self.label = str(self.count)

await interaction.response.edit_message(components=self)
component = await self.as_ui_component()
await interaction.response.edit_message(components=component)


@bot.slash_command() # pyright: ignore # still some unknowns in disnake
async def test_button(inter: disnake.CommandInteraction) -> None:
wrapped = components.wrap_interaction(inter)
component = MyButton()

await wrapped.response.send_message(components=component)
async def test_button(interaction: disnake.CommandInteraction) -> None:
component = await MyButton().as_ui_component()
await interaction.response.send_message(components=component)


bot.run(os.getenv("EXAMPLE_TOKEN"))
45 changes: 0 additions & 45 deletions examples/interaction.py

This file was deleted.

26 changes: 12 additions & 14 deletions examples/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ class FooButton(components.RichButton):

count: int

async def callback(self, interaction: components.MessageInteraction) -> None:
async def callback(self, interaction: disnake.MessageInteraction) -> None:
self.count += 1
self.label = str(self.count)

await interaction.response.edit_message(components=self)
component = await self.as_ui_component()
await interaction.response.edit_message(components=component)


@deeply_nested_manager.register
Expand All @@ -34,11 +35,12 @@ class FooBarBazButton(components.RichButton):

count: int

async def callback(self, interaction: components.MessageInteraction) -> None:
async def callback(self, interaction: disnake.MessageInteraction) -> None:
self.count += 1
self.label = str(self.count)

await interaction.response.edit_message(components=self)
component = await self.as_ui_component()
await interaction.response.edit_message(components=component)


@manager.as_callback_wrapper
Expand Down Expand Up @@ -100,19 +102,15 @@ async def error_handler(


@bot.slash_command() # pyright: ignore # still some unknowns in disnake
async def test_button(inter: disnake.CommandInteraction) -> None:
wrapped = components.wrap_interaction(inter)
component = FooButton(count=0)

await wrapped.response.send_message(components=component)
async def test_button(interaction: disnake.CommandInteraction) -> None:
component = await FooButton(count=0).as_ui_component()
await interaction.response.send_message(components=component)


@bot.slash_command() # pyright: ignore # still some unknowns in disnake
async def test_nested_button(inter: disnake.CommandInteraction) -> None:
wrapped = components.wrap_interaction(inter)
component = FooBarBazButton(count=0)

await wrapped.response.send_message(components=component)
async def test_nested_button(interaction: disnake.CommandInteraction) -> None:
component = await FooBarBazButton(count=0).as_ui_component()
await interaction.response.send_message(components=component)


bot.run(os.getenv("EXAMPLE_TOKEN"))
14 changes: 8 additions & 6 deletions examples/row.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def update_select(self, components: typing.Sequence[components.api.RichComponent

select.set_options(options)

async def callback(self, interaction: components.MessageInteraction):
async def callback(self, interaction: disnake.MessageInteraction):
# Get all components on the message for easier re-sending.
# Both of these lists will automagically contain self so that any
# changes immediately reflect without extra effort.
Expand All @@ -68,7 +68,8 @@ async def callback(self, interaction: components.MessageInteraction):
self.update_select(components)

# Re-send and update all components.
await interaction.response.send_message(components=rows)
finalised = await manager.finalise_components(rows)
await interaction.response.edit_message(components=finalised)


@manager.register()
Expand All @@ -89,7 +90,7 @@ def set_options(self, options: typing.List[disnake.SelectOption]):
self.max_values = 1
self.disabled = True

async def callback(self, interaction: components.MessageInteraction) -> None:
async def callback(self, interaction: disnake.MessageInteraction) -> None:
selection = (
"\n".join(f"- {value}" for value in interaction.values)
if interaction.values
Expand All @@ -101,9 +102,8 @@ async def callback(self, interaction: components.MessageInteraction) -> None:

@bot.slash_command() # pyright: ignore
async def test_components(interaction: disnake.CommandInteraction) -> None:
wrapped = components.wrap_interaction(interaction)
await wrapped.response.send_message(
components=[
layout = await manager.finalise_components(
[
[
OptionsToggleButton(label="numbers", options=["1", "2", "3", "4", "5"]),
OptionsToggleButton(label="letters", options=["a", "b", "c", "d", "e"]),
Expand All @@ -113,5 +113,7 @@ async def test_components(interaction: disnake.CommandInteraction) -> None:
]
)

await interaction.response.send_message(components=layout)


bot.run(os.environ["EXAMPLE_TOKEN"])
20 changes: 10 additions & 10 deletions examples/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class MySelect(components.RichStringSelect):
colour_middle: str = BLACK_SQUARE
colour_right: str = BLACK_SQUARE

async def callback(self, inter: components.MessageInteraction) -> None:
assert inter.values is not None
selected = inter.values[0]
async def callback(self, interaction: disnake.MessageInteraction) -> None:
assert interaction.values is not None
selected = interaction.values[0]

if self.state == "slot":
self.handle_slots(selected)
Expand All @@ -69,7 +69,8 @@ async def callback(self, inter: components.MessageInteraction) -> None:
self.handle_colours(selected)

msg = self.render_colours()
await inter.response.edit_message(msg, components=self)
component = await self.as_ui_component()
await interaction.response.edit_message(msg, components=component)

def handle_slots(self, selected: str) -> None:
if selected == "Finalise":
Expand All @@ -94,12 +95,11 @@ def render_colours(self) -> str:


@bot.slash_command() # pyright: ignore # still some unknowns in disnake
async def test_select(inter: disnake.CommandInteraction) -> None:
wrapped = components.wrap_interaction(inter)

component = MySelect()
await wrapped.response.send_message(
component.render_colours(), components=component
async def test_select(interaction: disnake.CommandInteraction) -> None:
my_select = MySelect()
await interaction.response.send_message(
my_select.render_colours(),
components=await my_select.as_ui_component(),
)


Expand Down
1 change: 0 additions & 1 deletion src/disnake/ext/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@
from disnake.ext.components import internal as internal
from disnake.ext.components.fields import *
from disnake.ext.components.impl import *
from disnake.ext.components.interaction import *
9 changes: 9 additions & 0 deletions src/disnake/ext/components/impl/component/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,12 @@ async def make_custom_id(self) -> str:
raise RuntimeError(message)

return await self.manager.make_custom_id(self)

async def callback( # pyright: ignore[reportIncompatibleMethodOverride] # noqa: D102
self, inter: disnake.MessageInteraction, /
) -> None:
# <<docstring inherited from component_api.RichButton>>

# NOTE: We narrow the interaction type down to a disnake.MessageInteraction
# here. This isn't typesafe, but it's just cleaner for the user.
...
11 changes: 1 addition & 10 deletions src/disnake/ext/components/impl/component/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import typing

import disnake
from disnake.ext.components import fields, interaction
from disnake.ext.components import fields
from disnake.ext.components.api import component as component_api
from disnake.ext.components.impl.component import base as component_base

Expand Down Expand Up @@ -64,12 +64,3 @@ async def as_ui_component(self) -> disnake.ui.Button[None]: # noqa: D102
emoji=self.emoji,
custom_id=await self.manager.make_custom_id(self),
)

async def callback( # pyright: ignore[reportIncompatibleMethodOverride] # noqa: D102
self, inter: interaction.MessageInteraction, /
) -> None:
# <<docstring inherited from component_api.RichButton>>

# NOTE: We narrow the interaction type down to a disnake.MessageInteraction
# here. This isn't typesafe, but it's just cleaner for the user.
...
11 changes: 1 addition & 10 deletions src/disnake/ext/components/impl/component/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import attr
import disnake
from disnake.ext.components import fields, interaction
from disnake.ext.components import fields
from disnake.ext.components.api import component as component_api
from disnake.ext.components.impl.component import base as component_base

Expand Down Expand Up @@ -41,15 +41,6 @@ class BaseSelect(
max_values: int = fields.internal(default=1)
disabled: bool = fields.internal(default=False)

async def callback( # pyright: ignore[reportIncompatibleMethodOverride]
self, inter: interaction.MessageInteraction, /
) -> None:
# <<docstring inherited from component_api.RichButton>>

# NOTE: We narrow the interaction type down to a disnake.MessageInteraction
# here. This isn't typesafe, but it's just cleaner for the user.
...


class RichStringSelect(BaseSelect, typing.Protocol):
"""The default implementation of a disnake-ext-components string select.
Expand Down
Loading

0 comments on commit e64ab5f

Please sign in to comment.