Source code for discord.components.components_holder
from__future__importannotationsfromtypingimportGeneric,castfromtyping_extensionsimportTypeVarTuple,Unpack,overridefrom.componentimportComponent,WalkableComponentMixinfrom.partial_componentsimportPartialComponent,PartialWalkableComponentMixinfrom.type_aliasesimportAnyComponent,AnyPartialComponentTs=TypeVarTuple("Ts",default=Unpack[tuple[AnyComponent|AnyPartialComponent]])# Unforntunately, we cannot use `TypeVarTuple` with upper bounds yet.
[docs]classComponentsHolder(tuple[Unpack[Ts]],Generic[Unpack[Ts]]):"""A sequence of components that can be used in Discord Bot UI Kit. This holder that is used to represent a collection of components, notably in a message. .. versionadded:: 3.0 """__slots__:tuple[str,...]=()def__new__(cls,*components:Unpack[Ts])->ComponentsHolder[Unpack[Ts]]:returnsuper().__new__(cls,components)
[docs]defget_by_id(self,component_id:str|int)->AnyComponent|AnyPartialComponent|None:"""Get a component by its custom ID."""formaybe_componentinself:ifnotisinstance(maybe_component,(Component,PartialComponent)):raiseTypeError(f"Expected {Component} or {PartialComponent} but got {maybe_component}")component=cast(AnyComponent|AnyPartialComponent,maybe_component)ifisinstance(component_id,str)andgetattr(component,"custom_id",None)==component_id:returncomponentelifisinstance(component_id,int)andgetattr(component,"id",None)==component_id:returncomponentifisinstance(component,(WalkableComponentMixin,PartialWalkableComponentMixin)):iffound:=component.get_by_id(component_id):returnfoundreturnNone