rintrix/chatlib/listener.py

26 lines
738 B
Python
Raw Normal View History

2023-12-02 16:57:04 +11:00
from asyncio import iscoroutinefunction
from .event import Event
class Listener():
def __init__(self,match,function):
self.match=match
self.function=function
@property
def matchstr(self):
return isinstance(self.match,str)
@property
def isasync(self):
return iscoroutinefunction(self.function)
async def __call__(self,*args,**kwargs):
if not self.isasync: return self.function(*args,**kwargs)
return await self.function(*args,**kwargs)
def __eq__(self,other):
if isinstance(other,Event):
if self.matchstr: return self.match==other.event_type
return self.match(other) # If it's not a string, assume it's a callable.
else: return super.__eq__(self,other)
def __str__(self): return self.function.__name_