33from .slash .types import ContextCommand , SlashCommand , SlashPermission , SlashSubcommand
44from .tools import MISSING , setup_logger , _none
55from .http import BetterRoute , jsonifyMessage , send_files
6- from .components import ActionRow , Button , LinkButton , SelectMenu , SelectOption , make_component
6+ from .components import ActionRow , Button , Component , LinkButton , SelectMenu , SelectOption , make_component
77
88import discord
99from discord .ext .commands import Bot
@@ -167,7 +167,7 @@ async def respond(self, content=MISSING, *, tts=False, embed=MISSING, embeds=MIS
167167
168168
169169 r = None
170- if delete_after is not MISSING and hide_message is True :
170+ if not _none ( delete_after ) and hide_message is True :
171171 raise EphemeralDeletion ()
172172
173173 if hide_message :
@@ -199,7 +199,7 @@ async def respond(self, content=MISSING, *, tts=False, embed=MISSING, embeds=MIS
199199 if not hide_message :
200200 responseMSG = await self ._state .http .request (BetterRoute ("GET" , f"/webhooks/{ self .application_id } /{ self .token } /messages/@original" ))
201201 msg = await getMessage (self ._state , data = responseMSG , response = False )
202- if delete_after is not MISSING :
202+ if not _none ( delete_after ) :
203203 await msg .delete (delete_after )
204204 return msg
205205
@@ -263,6 +263,12 @@ def _handle_auto_defer(self, auto_defer):
263263 self .deferred = auto_defer [0 ]
264264 self ._deferred_hidden = auto_defer [1 ]
265265
266+ class ComponentContext (Interaction , Component ):
267+ """A received component"""
268+ def __init__ (self , state , data , user , message ) -> None :
269+ Interaction .__init__ (self , state , data , user = user , message = message )
270+ Component .__init__ (self , data ["data" ]["component_type" ])
271+
266272class SelectedMenu (Interaction , SelectMenu ):
267273 """A :class:`~SelectMenu` object in which an item was selected"""
268274 def __init__ (self , data , user , s , msg , client ) -> None :
@@ -349,7 +355,7 @@ async def getMessage(state: ConnectionState, data, response = True):
349355 :class:`~Message` | :class:`~EphemeralMessage`
350356 The sent message
351357 """
352- channel = state .get_channel (int (data ["channel_id" ]))
358+ channel = state .get_channel (int (data ["channel_id" ])) or state . _get_private_channel_by_user ( data [ "message" ][ "author" ][ "id" ])
353359 if response :
354360 if data .get ("message" ) is not None and data .get ("message" , data )["flags" ] == 64 :
355361 return EphemeralResponseMessage (state = state , channel = channel , data = data .get ("message" , data ))
@@ -423,7 +429,7 @@ def _update(self, data):
423429 super ()._update (data )
424430 self ._update_components (data )
425431
426- async def edit (self , * , content = MISSING , embed = MISSING , embeds = MISSING , attachments = MISSING , suppress = MISSING ,
432+ async def edit (self , content = MISSING , * , embed = MISSING , embeds = MISSING , attachments = MISSING , suppress = MISSING ,
427433 delete_after = MISSING , allowed_mentions = MISSING , components = MISSING ):
428434 """Edits the message and updates its properties
429435
@@ -454,7 +460,7 @@ async def edit(self, *, content=MISSING, embed=MISSING, embeds=MISSING, attachme
454460 data = await self ._state .http .edit_message (self .channel .id , self .id , ** payload )
455461 self ._update (data )
456462
457- if delete_after is not MISSING :
463+ if not _none ( delete_after ) :
458464 await self .delete (delay = delete_after )
459465
460466 async def disable_action_row (self , row , disable = True ):
@@ -528,17 +534,17 @@ def action_rows(self):
528534 rows .append (c_row )
529535 return rows
530536
531- async def wait_for (self , event_name : Literal ["select" , "button" ], client , custom_id = MISSING , by = MISSING , check = lambda component : True , timeout = None ) -> Union [PressedButton , SelectedMenu ]:
537+ async def wait_for (self , event_name : Literal ["select" , "button" , "component" ], client , custom_id = MISSING , by = MISSING , check = lambda component : True , timeout = None ) -> Union [PressedButton , SelectedMenu ]:
532538 """Waits for a message component to be invoked in this message
533539
534540 Parameters
535541 -----------
536542 event_name: :class:`str`
537- The name of the event which will be awaited [``"select"`` | ``"button"``]
543+ The name of the event which will be awaited [``"select"`` | ``"button"`` | ``"component"`` ]
538544
539545 .. note::
540546
541- ``event_name`` must be ``select`` for a select menu selection and ``button`` for a button press
547+ ``event_name`` must be ``select`` for a select menu selection, ``button`` for a button press and ``component`` for any component
542548
543549 client: :class:`discord.ext.commands.Bot`
544550 The discord client
@@ -577,7 +583,7 @@ async def wait_for(self, event_name: Literal["select", "button"], client, custom
577583 except asyncio.TimeoutError:
578584 # no button press was received in 20 seconds timespan
579585 """
580- if event_name .lower () in ["button" , "select" ]:
586+ if event_name .lower () in ["button" , "select" , "component" ]:
581587 def _check (com ):
582588 if com .message .id == self .id :
583589 statements = []
@@ -591,23 +597,15 @@ def _check(com):
591597 return False
592598 if not isinstance (client , Bot ):
593599 raise WrongType ("client" , client , "discord.ext.commands.Bot" )
594- return (await client .wait_for ('button_press' if event_name .lower () == "button" else "menu_select" , check = _check , timeout = timeout ))
600+ return (await client .wait_for ('button_press' if event_name .lower () == "button" else ( "menu_select" if event_name . lower () == "menu" else "component" ) , check = _check , timeout = timeout ))
595601
596- raise InvalidEvent (event_name , ["button" , "select" ])
597-
598- class ResponseMessage (Message ):
599- r"""A message Object which extends the `Message` Object optimized for an interaction component"""
600- def __init__ (self , * , state , channel , data , user , interaction_component = None ):
601- Message .__init__ (self , state = state , channel = channel , data = data ["message" ])
602- self .interaction = Interaction (state , data , user , self )
603- self .interaction_component = interaction_component
604-
602+ raise InvalidEvent (event_name , ["button" , "select" , "component" ])
605603
606604class WebhookMessage (Message , discord .WebhookMessage ):
607605 def __init__ (self , * , state , channel , data ):
608606 Message .__init__ (self , state = state , channel = channel , data = data )
609607 discord .WebhookMessage .__init__ (self , state = state , channel = channel , data = data )
610- async def edit (self , ** fields ):
608+ async def edit (self , * args , * *fields ):
611609 """Edits the message
612610
613611 content: :class:`str`, Optional
@@ -621,7 +619,7 @@ async def edit(self, **fields):
621619 allowed_mentions: :class`discord.AllowedMentions`
622620 Controls the mentions being processed in this message. See `discord.abc.Messageable.send` for more information.
623621 """
624- return await self ._state ._webhook ._adapter .edit_webhook_message (message_id = self .id , payload = jsonifyMessage (** fields ))
622+ return await self ._state ._webhook ._adapter .edit_webhook_message (message_id = self .id , payload = jsonifyMessage (* args , * *fields ))
625623
626624
627625class EphemeralComponent (Interaction ):
@@ -651,9 +649,9 @@ def __init__(self, state, channel, data, application_id=MISSING, token=MISSING):
651649 Message .__init__ (self , state = state , channel = channel , data = data )
652650 self ._application_id = application_id
653651 self ._interaction_token = token
654- async def edit (self , ** fields ):
652+ async def edit (self , * args , * *fields ):
655653 r = BetterRoute ("PATCH" , f"/webhooks/{ self ._application_id } /{ self ._interaction_token } /messages/{ self .id } " )
656- self ._update (await self ._state .http .request (r , json = jsonifyMessage (** fields )))
654+ self ._update (await self ._state .http .request (r , json = jsonifyMessage (* args , * *fields )))
657655 async def delete (self ):
658656 """Override for delete function that will throw an exception"""
659657 raise EphemeralDeletion ()
@@ -668,7 +666,7 @@ class EphemeralResponseMessage(Message):
668666 def __init__ (self , * , state , channel , data ):
669667 Message .__init__ (self , state = state , channel = channel , data = data )
670668
671- async def edit (self , token , ** fields ):
669+ async def edit (self , token , * args , * *fields ):
672670 """Edits the message
673671
674672 Parameters
@@ -689,7 +687,7 @@ async def testing(ctx):
689687
690688 """
691689 route = BetterRoute ("PATCH" , f"/webhooks/{ self .interaction .application_id } /{ token } /messages/{ self .id } " )
692- self ._update (await self ._state .http .request (route , json = jsonifyMessage (** fields )))
690+ self ._update (await self ._state .http .request (route , json = jsonifyMessage (* args , * *fields )))
693691 async def delete (self ):
694692 """Override for delete function that will throw an exception"""
695693 raise EphemeralDeletion ()
0 commit comments