From 6ce7e36b18937bc53e9f01c8525156f74bc07016 Mon Sep 17 00:00:00 2001 From: gammafn Date: Tue, 30 Mar 2021 11:50:04 -0500 Subject: [PATCH] Add !alias commnd to user-alias modules --- README.md | 8 +++++++ modules/alias.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 modules/alias.py diff --git a/README.md b/README.md index 53dfa74..632c9f7 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,14 @@ Bot management commands. Prints help on existing modules. +### Alias + +Add or remove aliases for a module. + +* !alias add osm loc +* !alias list +* !alias remove osm + ### Echo Simple example module that just echoes what user said. diff --git a/modules/alias.py b/modules/alias.py new file mode 100644 index 0000000..915ad90 --- /dev/null +++ b/modules/alias.py @@ -0,0 +1,55 @@ +from modules.common.module import BotModule +import random + +class MatrixModule(BotModule): + def __init__(self, name): + super().__init__(name) + self.aliases = dict() + + def set_settings(self, data): + super().set_settings(data) + if data.get('aliases'): + self.aliases = data['aliases'] + + def get_settings(self): + data = super().get_settings() + data['aliases'] = self.aliases + return data + + def matrix_start(self, bot): + super().matrix_start(bot) + bot.module_aliases.update(self.aliases) + + async def matrix_message(self, bot, room, event): + + args = event.body.split() + args.pop(0) + + if len(args) == 3: + if args.pop(0) == 'add': + bot.must_be_owner(event) + self.logger.debug(f"room: {room.name} sender: {event.sender} wants to add an alias") + + bot.module_aliases.update({args[0]: args[1]}) + self.aliases.update({args[0]: args[1]}) + bot.save_settings() + await bot.send_text(room, f'Aliased !{args[0]} to !{args[1]}') + + elif len(args) == 2: + if args.pop(0) in ['rm', 'remove']: + bot.must_be_owner(event) + self.logger.debug(f"room: {room.name} sender: {event.sender} wants to remove an alias") + + old = bot.module_aliases.pop(args[0]) + self.aliases.pop(args[0]) + bot.save_settings() + await bot.send_text(room, f'Removed alias !{args[0]}') + + elif len(args) == 1: + if args.pop(0) in ['ls', 'list']: + self.logger.debug(f"room: {room.name} sender: {event.sender} wants to list aliases") + msg = '\n'.join([ f'- {key} => {val}' for key, val in bot.module_aliases.items() ]) + await bot.send_text(room, 'Aliases:\n' + msg) + + def help(self): + return 'Alias a command'