add stop and start message to BotModule class
This commit is contained in:
parent
c50b8209f2
commit
59d9fad9ef
12
bot.py
12
bot.py
|
@ -81,7 +81,6 @@ class Bot:
|
|||
def save_settings(self):
|
||||
module_settings = dict()
|
||||
for modulename, moduleobject in self.modules.items():
|
||||
if "get_settings" in dir(moduleobject):
|
||||
try:
|
||||
module_settings[modulename] = moduleobject.get_settings()
|
||||
except Exception:
|
||||
|
@ -96,7 +95,6 @@ class Bot:
|
|||
return
|
||||
for modulename, moduleobject in self.modules.items():
|
||||
if data['module_settings'].get(modulename):
|
||||
if "set_settings" in dir(moduleobject):
|
||||
try:
|
||||
moduleobject.set_settings(
|
||||
data['module_settings'][modulename])
|
||||
|
@ -118,7 +116,7 @@ class Bot:
|
|||
|
||||
moduleobject = self.modules.get(command)
|
||||
|
||||
if moduleobject.enabled and ("matrix_message" in dir(moduleobject)):
|
||||
if moduleobject.enabled:
|
||||
try:
|
||||
await moduleobject.matrix_message(bot, room, event)
|
||||
except CommandRequiresAdmin:
|
||||
|
@ -153,7 +151,7 @@ class Bot:
|
|||
module = importlib.import_module('modules.' + modulename)
|
||||
module = reload(module)
|
||||
cls = getattr(module, 'MatrixModule')
|
||||
return cls()
|
||||
return cls(modulename)
|
||||
except ModuleNotFoundError:
|
||||
print('Module ', modulename, ' failed to load!')
|
||||
traceback.print_exc(file=sys.stderr)
|
||||
|
@ -183,7 +181,6 @@ class Bot:
|
|||
self.pollcount = self.pollcount + 1
|
||||
for modulename, moduleobject in self.modules.items():
|
||||
if moduleobject.enabled:
|
||||
if "matrix_poll" in dir(moduleobject):
|
||||
try:
|
||||
await moduleobject.matrix_poll(bot, self.pollcount)
|
||||
except Exception:
|
||||
|
@ -221,7 +218,6 @@ class Bot:
|
|||
print("NOTE: check MATRIX_ACCESS_TOKEN or set MATRIX_PASSWORD")
|
||||
sys.exit(2)
|
||||
|
||||
|
||||
def init(self):
|
||||
|
||||
self.matrix_user = os.getenv('MATRIX_USER')
|
||||
|
@ -255,8 +251,6 @@ class Bot:
|
|||
print(f'Starting {len(enabled_modules)} modules..')
|
||||
for modulename, moduleobject in self.modules.items():
|
||||
if moduleobject.enabled:
|
||||
print('Starting', modulename, '..')
|
||||
if "matrix_start" in dir(moduleobject):
|
||||
try:
|
||||
moduleobject.matrix_start(bot)
|
||||
except Exception:
|
||||
|
@ -265,8 +259,6 @@ class Bot:
|
|||
def stop(self):
|
||||
print(f'Stopping {len(self.modules)} modules..')
|
||||
for modulename, moduleobject in self.modules.items():
|
||||
print('Stopping', modulename, '..')
|
||||
if "matrix_stop" in dir(moduleobject):
|
||||
try:
|
||||
moduleobject.matrix_stop(bot)
|
||||
except Exception:
|
||||
|
|
|
@ -4,11 +4,12 @@ from modules.common.module import BotModule
|
|||
|
||||
class MatrixModule(BotModule):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
def __init__(self, name):
|
||||
super().__init__(name)
|
||||
self.enable()
|
||||
|
||||
def matrix_start(self, bot):
|
||||
super().matrix_start(bot)
|
||||
self.starttime = datetime.now()
|
||||
|
||||
async def matrix_message(self, bot, room, event):
|
||||
|
|
|
@ -25,8 +25,9 @@ class BotModule(ABC):
|
|||
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, name):
|
||||
self.enabled = False
|
||||
self.name = name
|
||||
|
||||
def matrix_start(self, bot):
|
||||
"""Called once on startup
|
||||
|
@ -34,7 +35,7 @@ class BotModule(ABC):
|
|||
:param bot: a reference to the bot
|
||||
:type bot: Bot
|
||||
"""
|
||||
pass
|
||||
print('Starting', self.name, '..')
|
||||
|
||||
@abstractmethod
|
||||
async def matrix_message(self, bot, room, event):
|
||||
|
@ -55,7 +56,7 @@ class BotModule(ABC):
|
|||
:param bot: a reference to the bot
|
||||
:type bot: Bot
|
||||
"""
|
||||
pass
|
||||
print('Stopping', self.name, '..')
|
||||
|
||||
async def matrix_poll(self, bot, pollcount):
|
||||
"""Called every 10 seconds
|
||||
|
|
|
@ -5,7 +5,8 @@ from modules.common.module import BotModule
|
|||
|
||||
|
||||
class PollingService(BotModule):
|
||||
def __init__(self):
|
||||
def __init__(self, name):
|
||||
super().__init__(name)
|
||||
self.known_ids = set()
|
||||
self.account_rooms = dict() # Roomid -> [account, account..]
|
||||
self.next_poll_time = dict() # Roomid -> datetime, None = not polled yet
|
||||
|
|
|
@ -3,8 +3,8 @@ from modules.common.module import BotModule
|
|||
|
||||
class MatrixModule(BotModule):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
def __init__(self, name):
|
||||
super().__init__(name)
|
||||
self.enable()
|
||||
|
||||
async def matrix_message(self, bot, room, event):
|
||||
|
@ -17,7 +17,7 @@ class MatrixModule(BotModule):
|
|||
msg = msg + ' - ' + moduleobject.help() + '\n'
|
||||
except AttributeError:
|
||||
pass
|
||||
msg + msg + '\n'
|
||||
msg = msg + '\n'
|
||||
msg = msg + "\nAdd your own commands at https://github.com/vranki/hemppa"
|
||||
await bot.send_text(room, msg)
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ class MatrixModule(BotModule):
|
|||
bot = None
|
||||
|
||||
def matrix_start(self, bot):
|
||||
super().matrix_start(bot)
|
||||
self.bot = bot
|
||||
bot.client.add_event_callback(self.unknown_cb, RoomMessageUnknown)
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ from modules.common.module import BotModule
|
|||
|
||||
class MatrixModule(BotModule):
|
||||
def matrix_start(self, bot):
|
||||
super().matrix_start(bot)
|
||||
self.bot = bot
|
||||
self.SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
|
||||
self.credentials_file = "credentials.json"
|
||||
|
|
|
@ -11,8 +11,8 @@ from modules.common.pollingservice import PollingService
|
|||
|
||||
|
||||
class MatrixModule(PollingService):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
def __init__(self, name):
|
||||
super().__init__(name)
|
||||
self.instagram = Instagram()
|
||||
self.service_name = 'Instagram'
|
||||
|
||||
|
|
|
@ -9,8 +9,8 @@ from modules.common.pollingservice import PollingService
|
|||
# https://github.com/taspinar/twitterscraper/tree/master/twitterscraper
|
||||
|
||||
class MatrixModule(PollingService):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
def __init__(self, name):
|
||||
super().__init__(name)
|
||||
self.service_name = 'Twitter'
|
||||
|
||||
async def poll_implementation(self, bot, account, roomid, send_messages):
|
||||
|
|
|
@ -30,6 +30,7 @@ class MatrixModule(BotModule):
|
|||
"""
|
||||
Register callback for all RoomMessageText events on startup
|
||||
"""
|
||||
super().matrix_start(bot)
|
||||
self.bot = bot
|
||||
bot.client.add_event_callback(self.text_cb, RoomMessageText)
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ class MatrixModule(BotModule):
|
|||
# print ("adding trailing https")
|
||||
url = "https://" + url
|
||||
|
||||
print(url)
|
||||
print("ping:", url)
|
||||
|
||||
start = timer()
|
||||
try:
|
||||
|
|
Loading…
Reference in New Issue