Refactors interaction response handling for flexibility
Simplifies modal response creation by accepting both structured data objects and raw dictionaries, reducing unnecessary type casting and import dependencies. Improves text input decorator by auto-generating custom IDs from function names when not explicitly provided. Enhances example code by properly utilizing parsed Guild objects instead of raw data and fixes modal component registration.
This commit is contained in:
parent
b20f1fd292
commit
534b5b3980
@ -395,17 +395,14 @@ class Interaction:
|
|||||||
|
|
||||||
async def respond_modal(self, modal: "Modal") -> None:
|
async def respond_modal(self, modal: "Modal") -> None:
|
||||||
"""|coro| Send a modal in response to this interaction."""
|
"""|coro| Send a modal in response to this interaction."""
|
||||||
|
payload = InteractionResponsePayload(
|
||||||
from typing import Any, cast
|
type=InteractionCallbackType.MODAL,
|
||||||
|
data=modal.to_dict(),
|
||||||
payload = {
|
)
|
||||||
"type": InteractionCallbackType.MODAL.value,
|
|
||||||
"data": modal.to_dict(),
|
|
||||||
}
|
|
||||||
await self._client._http.create_interaction_response(
|
await self._client._http.create_interaction_response(
|
||||||
interaction_id=self.id,
|
interaction_id=self.id,
|
||||||
interaction_token=self.token,
|
interaction_token=self.token,
|
||||||
payload=cast(Any, payload),
|
payload=payload,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def edit(
|
async def edit(
|
||||||
@ -489,7 +486,7 @@ class InteractionResponse:
|
|||||||
"""Sends a modal response."""
|
"""Sends a modal response."""
|
||||||
payload = InteractionResponsePayload(
|
payload = InteractionResponsePayload(
|
||||||
type=InteractionCallbackType.MODAL,
|
type=InteractionCallbackType.MODAL,
|
||||||
data=InteractionCallbackData(modal.to_dict()),
|
data=modal.to_dict(),
|
||||||
)
|
)
|
||||||
await self._interaction._client._http.create_interaction_response(
|
await self._interaction._client._http.create_interaction_response(
|
||||||
self._interaction.id,
|
self._interaction.id,
|
||||||
@ -506,11 +503,13 @@ class InteractionCallbackData:
|
|||||||
self.tts: Optional[bool] = data.get("tts")
|
self.tts: Optional[bool] = data.get("tts")
|
||||||
self.content: Optional[str] = data.get("content")
|
self.content: Optional[str] = data.get("content")
|
||||||
self.embeds: Optional[List[Embed]] = (
|
self.embeds: Optional[List[Embed]] = (
|
||||||
[Embed(e) for e in data.get("embeds", [])] if data.get("embeds") else None
|
[Embed(e) for e in data.get("embeds", [])]
|
||||||
|
if data.get("embeds")
|
||||||
|
else None
|
||||||
)
|
)
|
||||||
self.allowed_mentions: Optional[AllowedMentions] = (
|
self.allowed_mentions: Optional[AllowedMentions] = (
|
||||||
AllowedMentions(data["allowed_mentions"])
|
AllowedMentions(data["allowed_mentions"])
|
||||||
if data.get("allowed_mentions")
|
if "allowed_mentions" in data
|
||||||
else None
|
else None
|
||||||
)
|
)
|
||||||
self.flags: Optional[int] = data.get("flags") # MessageFlags enum could be used
|
self.flags: Optional[int] = data.get("flags") # MessageFlags enum could be used
|
||||||
@ -557,15 +556,18 @@ class InteractionResponsePayload:
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
type: InteractionCallbackType,
|
type: InteractionCallbackType,
|
||||||
data: Optional[InteractionCallbackData] = None,
|
data: Optional[Union[InteractionCallbackData, Dict[str, Any]]] = None,
|
||||||
):
|
):
|
||||||
self.type: InteractionCallbackType = type
|
self.type = type
|
||||||
self.data: Optional[InteractionCallbackData] = data
|
self.data = data
|
||||||
|
|
||||||
def to_dict(self) -> Dict[str, Any]:
|
def to_dict(self) -> Dict[str, Any]:
|
||||||
payload: Dict[str, Any] = {"type": self.type.value}
|
payload: Dict[str, Any] = {"type": self.type.value}
|
||||||
if self.data:
|
if self.data:
|
||||||
payload["data"] = self.data.to_dict()
|
if isinstance(self.data, dict):
|
||||||
|
payload["data"] = self.data
|
||||||
|
else:
|
||||||
|
payload["data"] = self.data.to_dict()
|
||||||
return payload
|
return payload
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
|
@ -74,7 +74,7 @@ def text_input(
|
|||||||
|
|
||||||
item = TextInput(
|
item = TextInput(
|
||||||
label=label,
|
label=label,
|
||||||
custom_id=custom_id,
|
custom_id=custom_id or func.__name__,
|
||||||
style=style,
|
style=style,
|
||||||
placeholder=placeholder,
|
placeholder=placeholder,
|
||||||
required=required,
|
required=required,
|
||||||
|
@ -28,6 +28,7 @@ if os.path.join(os.getcwd(), "examples") == os.path.dirname(os.path.abspath(__fi
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
import disagreement
|
import disagreement
|
||||||
|
from disagreement.models import Guild
|
||||||
from disagreement.ext import commands # Import the new commands extension
|
from disagreement.ext import commands # Import the new commands extension
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print(
|
print(
|
||||||
@ -190,9 +191,9 @@ async def on_message(message: disagreement.Message):
|
|||||||
@client.on_event(
|
@client.on_event(
|
||||||
"GUILD_CREATE"
|
"GUILD_CREATE"
|
||||||
) # Example of listening to a specific event by its Discord name
|
) # Example of listening to a specific event by its Discord name
|
||||||
async def on_guild_available(guild_data: dict): # Receives raw data for now
|
async def on_guild_available(guild: Guild):
|
||||||
# In a real scenario, guild_data would be parsed into a Guild model
|
# The event now passes a Guild object directly
|
||||||
print(f"Guild available: {guild_data.get('name')} (ID: {guild_data.get('id')})")
|
print(f"Guild available: {guild.name} (ID: {guild.id})")
|
||||||
|
|
||||||
|
|
||||||
# --- Main Execution ---
|
# --- Main Execution ---
|
||||||
|
@ -26,6 +26,7 @@ class NameModal(ui.Modal):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(title="Your Name", custom_id="name_modal")
|
super().__init__(title="Your Name", custom_id="name_modal")
|
||||||
self.name = ui.TextInput(label="Name", custom_id="name")
|
self.name = ui.TextInput(label="Name", custom_id="name")
|
||||||
|
self.add_item(self.name)
|
||||||
|
|
||||||
|
|
||||||
@slash_command(name="namemodal", description="Shows a modal")
|
@slash_command(name="namemodal", description="Shows a modal")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user