13
13
log = logging .getLogger ("mmpy.event_handler" )
14
14
15
15
16
- class EventHandler ( object ) :
16
+ class EventHandler :
17
17
def __init__ (
18
18
self ,
19
19
driver : Driver ,
@@ -37,10 +37,8 @@ def start(self):
37
37
def _should_ignore (self , message : Message ):
38
38
# Ignore message from senders specified in settings, and maybe from ourself
39
39
return (
40
- True
41
- if message .sender_name .lower ()
40
+ message .sender_name .lower ()
42
41
in (name .lower () for name in self .settings .IGNORE_USERS )
43
- else False
44
42
) or (self .ignore_own_messages and message .sender_name == self .driver .username )
45
43
46
44
async def _check_queue_loop (self , webhook_queue : queue .Queue ):
@@ -75,37 +73,34 @@ async def _handle_post(self, post):
75
73
76
74
# Find all the listeners that match this message, and have their plugins handle
77
75
# the rest.
78
- tasks = []
79
- for matcher , functions in self .plugin_manager .message_listeners .items ():
80
- match = matcher .search (message .text )
81
- if match :
82
- groups = list ([group for group in match .groups () if group != "" ])
83
- for function in functions :
84
- # Create an asyncio task to handle this callback
85
- tasks .append (
86
- asyncio .create_task (
87
- function .plugin .call_function (
88
- function , message , groups = groups
89
- )
90
- )
91
- )
76
+ tasks = [
77
+ asyncio .create_task (
78
+ function .plugin .call_function (
79
+ function ,
80
+ message ,
81
+ groups = [group for group in match .groups () if group != "" ],
82
+ )
83
+ )
84
+ for matcher , functions in self .plugin_manager .message_listeners .items ()
85
+ if (match := matcher .search (message .text ))
86
+ for function in functions
87
+ ]
88
+
92
89
# Execute the callbacks in parallel
93
90
asyncio .gather (* tasks )
94
91
95
92
async def _handle_webhook (self , event : WebHookEvent ):
96
93
# Find all the listeners that match this webhook id, and have their plugins
97
94
# handle the rest.
98
- tasks = []
99
- for matcher , functions in self .plugin_manager .webhook_listeners .items ():
100
- match = matcher .search (event .webhook_id )
101
- if match :
102
- for function in functions :
103
- # Create an asyncio task to handle this callback
104
- tasks .append (
105
- asyncio .create_task (
106
- function .plugin .call_function (function , event )
107
- )
108
- )
95
+
96
+ tasks = [
97
+ # Create an asyncio task to handle this callback
98
+ asyncio .create_task (function .plugin .call_function (function , event ))
99
+ for matcher , functions in self .plugin_manager .webhook_listeners .items ()
100
+ if matcher .search (event .webhook_id )
101
+ for function in functions
102
+ ]
103
+
109
104
# If this webhook doesn't correspond to any listeners, signal the WebHookServer
110
105
# to not wait for any response
111
106
if len (tasks ) == 0 :
0 commit comments