@@ -383,6 +383,7 @@ def __init__(
383
383
] = {}
384
384
"""A dictionary of registered application commands in a tree"""
385
385
self ._component_callbacks : Dict [str , Callable [..., Coroutine ]] = {}
386
+ self ._regex_component_callbacks : Dict [re .Pattern , Callable [..., Coroutine ]] = {}
386
387
self ._modal_callbacks : Dict [str , Callable [..., Coroutine ]] = {}
387
388
self ._global_autocompletes : Dict [str , GlobalAutoComplete ] = {}
388
389
self .processors : Dict [str , Callable [..., Coroutine ]] = {}
@@ -1270,10 +1271,15 @@ def add_component_callback(self, command: ComponentCommand) -> None:
1270
1271
1271
1272
"""
1272
1273
for listener in command .listeners :
1273
- # I know this isn't an ideal solution, but it means we can lookup callbacks with O(1)
1274
- if listener in self ._component_callbacks .keys ():
1275
- raise ValueError (f"Duplicate Component! Multiple component callbacks for `{ listener } `" )
1276
- self ._component_callbacks [listener ] = command
1274
+ if isinstance (listener , re .Pattern ):
1275
+ if listener in self ._regex_component_callbacks .keys ():
1276
+ raise ValueError (f"Duplicate Component! Multiple component callbacks for `{ listener } `" )
1277
+ self ._regex_component_callbacks [listener ] = command
1278
+ else :
1279
+ # I know this isn't an ideal solution, but it means we can lookup callbacks with O(1)
1280
+ if listener in self ._component_callbacks .keys ():
1281
+ raise ValueError (f"Duplicate Component! Multiple component callbacks for `{ listener } `" )
1282
+ self ._component_callbacks [listener ] = command
1277
1283
continue
1278
1284
1279
1285
def add_modal_callback (self , command : ModalCommand ) -> None :
@@ -1410,7 +1416,7 @@ async def wrap(*args, **kwargs) -> Absent[List[Dict]]:
1410
1416
if cmd_name not in found and warn_missing :
1411
1417
self .logger .error (
1412
1418
f'Detected yet to sync slash command "/{ cmd_name } " for scope '
1413
- f" { ' global' if scope == GLOBAL_SCOPE else scope } "
1419
+ f' { " global" if scope == GLOBAL_SCOPE else scope } '
1414
1420
)
1415
1421
continue
1416
1422
found .add (cmd_name )
@@ -1668,7 +1674,15 @@ async def _dispatch_interaction(self, event: RawGatewayEvent) -> None:
1668
1674
component_type = interaction_data ["data" ]["component_type" ]
1669
1675
1670
1676
self .dispatch (events .Component (ctx = ctx ))
1671
- if callback := self ._component_callbacks .get (ctx .custom_id ):
1677
+ component_callback = self ._component_callbacks .get (ctx .custom_id )
1678
+ if not component_callback :
1679
+ # evaluate regex component callbacks
1680
+ for regex , callback in self ._regex_component_callbacks .items ():
1681
+ if regex .match (ctx .custom_id ):
1682
+ component_callback = callback
1683
+ break
1684
+
1685
+ if component_callback :
1672
1686
await self .__dispatch_interaction (
1673
1687
ctx = ctx ,
1674
1688
callback = callback (ctx ),
0 commit comments