From 6cf40488d4086b56b4899a4a62895dc1a19b397c Mon Sep 17 00:00:00 2001 From: Ville Ranki Date: Sun, 5 Jan 2020 00:28:57 +0200 Subject: [PATCH] Added initial bot module for bot mgmt commands. --- README.md | 13 +++++++++++++ bot.py | 4 +++- modules/bot.py | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 modules/bot.py diff --git a/README.md b/README.md index 170a3d9..dcf696c 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,13 @@ Support room: #hemppa:hacklab.fi - https://matrix.to/#/#hemppa:hacklab.fi ## Module list +### Bot + +Bot management commands. + +* !bot version - print version of the bot +* !bot quit - quit the bot process (Must be done as bot owner) + ### Help Prints help on existing modules. @@ -23,14 +30,20 @@ Prints help on existing modules. Simple example module that just echoes what user said. +* !echo Hello, world! + ### Metar Aviation weather metar service access. +* !metar eftp + ### TAF Aviation weather TAF service access. +* !taf eftp + ### Uptime Prints bot uptime. diff --git a/bot.py b/bot.py index c9d3459..afb4912 100755 --- a/bot.py +++ b/bot.py @@ -234,7 +234,8 @@ class Bot: if self.join_on_invite: print('Note: Bot will join rooms if invited') print('Bot running as', self.client.user, ', owners', self.owners) - await self.client.sync_forever(timeout=30000) + self.bot_task = asyncio.create_task(self.client.sync_forever(timeout=30000)) + await self.bot_task else: print('Client was not able to log in, check env variables!') @@ -246,5 +247,6 @@ try: except KeyboardInterrupt: if bot.poll_task: bot.poll_task.cancel() + bot.bot_task.cancel() bot.stop() diff --git a/modules/bot.py b/modules/bot.py new file mode 100644 index 0000000..15caf25 --- /dev/null +++ b/modules/bot.py @@ -0,0 +1,19 @@ +import urllib.request + + +class MatrixModule: + async def matrix_message(self, bot, room, event): + args = event.body.split() + if len(args) == 2: + if args[1]=='quit': + bot.must_be_admin(room, event) + await bot.send_text(room, f'Quitting, as requested') + print(f'{event.sender} commanded bot to quit, so quitting..') + bot.bot_task.cancel() + elif args[1]=='version': + await bot.send_text(room, f'Hemppa version {bot.version} - https://github.com/vranki/hemppa') + else: + await bot.send_text(room, 'Unknown command, sorry.') + + def help(self): + return('Bot management commands')