diff --git a/src/bot.py b/src/bot.py index 4c04a77..dae74b0 100644 --- a/src/bot.py +++ b/src/bot.py @@ -44,12 +44,7 @@ def ls(update, context): dispatcher.add_handler(CommandHandler('ls', ls)) def do(update, context): - for arg in context.args: - if not arg.isnumeric(): - task_ids = [db.fuzzy_get_task_id(update.effective_user, ' '.join(context.args))] - break - else: - task_ids = [int(x) for x in context.args] + task_ids = db.get_task_ids_from_context(update.effective_user, context) for task_id in task_ids: task = db.get_task(update.effective_user, task_id) @@ -72,13 +67,7 @@ def new_task(update, context): dispatcher.add_handler(MessageHandler(Filters.text & (~Filters.command), new_task)) def delete(update, context): - print(db._get_db()) - for arg in context.args: - if not arg.isnumeric(): - task_ids = [db.fuzzy_get_task_id(update.effective_user, ' '.join(context.args))] - break - else: - task_ids = [int(x) for x in context.args] + task_ids = db.get_task_ids_from_context(update.effective_user, context) for task_id in task_ids: task = db.remove_task_by_id(update.effective_user, task_id) diff --git a/src/db.py b/src/db.py index e7d2799..484a95e 100644 --- a/src/db.py +++ b/src/db.py @@ -54,6 +54,16 @@ def fuzzy_get_task_id(user, text): task_strs = [str(task) for task in get_all_user_tasks(user)] return task_strs.index(fuzzyprocess.extractOne(text, task_strs)[0]) +def get_task_ids_from_context(user, context): + for arg in context.args: + if not arg.isnumeric(): + task_ids = [fuzzy_get_task_id(user, ' '.join(context.args))] + break + else: + task_ids = [int(x) for x in context.args] + + return task_ids + def create_user(db, user: telegram.User): if str(user.id) not in db[DbKeys.USER_TASKS].keys(): db[DbKeys.USER_TASKS][str(user.id)] = []