]> us-delaporte.pagekite.me Git - matrix-chore-bot.git/blobdiff - matrix_chore_bot/bot.py
Add filter command
[matrix-chore-bot.git] / matrix_chore_bot / bot.py
index 3130d354bd0ac2805415a33505706b6262f540a9..c97be63659ba47f057b6b1f47aaee85bcc0966d9 100644 (file)
@@ -6,22 +6,18 @@ import simplematrixbotlib as botlib
 
 from .db import Data as ChoreData
 
 
 from .db import Data as ChoreData
 
-CHORES = ChoreData()
-
 SERVER = os.environ.get('MATRIX_SERVER')
 BOT_NAME = os.environ.get('MATRIX_BOT')
 BOT_PWD = os.environ.get('MATRIX_BOT_PWD')
 
 SERVER = os.environ.get('MATRIX_SERVER')
 BOT_NAME = os.environ.get('MATRIX_BOT')
 BOT_PWD = os.environ.get('MATRIX_BOT_PWD')
 
-def get_bot():
-    creds = botlib.Creds(SERVER, BOT_NAME, BOT_PWD)
-    return botlib.Bot(creds)
+CHORES = ChoreData()
 
 logger = logging.getLogger(__name__)
 logging.basicConfig(filename='matrix-chore-bot.log', encoding='utf-8', level=logging.DEBUG)
 logging.getLogger('nio').setLevel(logging.WARNING)
 
 
 logger = logging.getLogger(__name__)
 logging.basicConfig(filename='matrix-chore-bot.log', encoding='utf-8', level=logging.DEBUG)
 logging.getLogger('nio').setLevel(logging.WARNING)
 
-async def chore_bot(room, message, bot):
-    match = botlib.MessageMatch(room, message, bot, '!chore')
+async def chore_bot(room, message, bot, prefix='!chore'):
+    match = botlib.MessageMatch(room, message, bot, prefix)
 
     if not match.is_not_from_this_bot() and match.prefix():
         return
 
     if not match.is_not_from_this_bot() and match.prefix():
         return
@@ -33,7 +29,7 @@ async def chore_bot(room, message, bot):
                 await bot.api.send_text_message(
                     room.room_id, str(line))
         except Exception as ex:
                 await bot.api.send_text_message(
                     room.room_id, str(line))
         except Exception as ex:
-            line = 'Ye flask must be load bearing, or something...'
+            line = f'Ye flask must be load bearing, or something... {str(ex)}'
             await bot.api.send_text_message(
                 room.room_id, str(line))
 
             await bot.api.send_text_message(
                 room.room_id, str(line))
 
@@ -51,15 +47,24 @@ async def handle_chore(match):
       check - check status of a chore
       done - mark a chore as done
       list - print the chore list
       check - check status of a chore
       done - mark a chore as done
       list - print the chore list
+      filter TERM - list all chores that include TERM
       undo - mark a chore as undone
     '''
 
     if match.command('list'):
       undo - mark a chore as undone
     '''
 
     if match.command('list'):
-        results = ['## Listing chores...']
+        results = ['## Listing all...']
         for item in CHORES.list():
             results.append(item)
         results.append('-- End of List --')
         return results
         for item in CHORES.list():
             results.append(item)
         results.append('-- End of List --')
         return results
+
+    if match.command('filter'):
+        term = ' '.join(match.args()[1:])
+        results = [f'## List matching {term}']
+        for item in CHORES.filter(term):
+            results.append(item)
+        results.append('-- End of List --')
+        return results
     
     if match.command('setup'):
         results = []
     
     if match.command('setup'):
         results = []