From 7e57b75bf31bb717d8983225fdcc529b7772c216 Mon Sep 17 00:00:00 2001 From: ennucore Date: Tue, 21 Jun 2022 21:04:55 +0300 Subject: [PATCH] Checking if users have already met, small fixes --- README.md | 2 +- bot.py | 9 +++++---- community.py | 10 +++++++++- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 326adc7..111a592 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,6 @@ Random meetings for chats, kind of like RandomCoffee. 4. The bot sends the matches and links to their Telegram accounts TODO: -- Improve scheduling: don't add meetings if the users have already met each other - Auto-scheduling: run it at every event - A button to request a meeting +- Add commands like info diff --git a/bot.py b/bot.py index 739cb01..b28b2d6 100644 --- a/bot.py +++ b/bot.py @@ -35,9 +35,6 @@ class Bot(telebot.TeleBot): for template_file in config.get('template_files', []): self.templates.load_file(template_file) self.main_keyboard = None - if config.get('main_keyboard') is not None: - kb = config.get('main_keyboard') - self.main_keyboard = {key: utils.create_keyboard(kb[key]) for key in kb.keys()} def render_template(self, template_name: str, locale: str = 'ru', **kwargs) -> \ typing.Tuple[str, typing.Union[telebot.types.ReplyKeyboardMarkup, telebot.types.InlineKeyboardMarkup]]: @@ -120,7 +117,6 @@ class Bot(telebot.TeleBot): self.db.users.replace_one({'user_id': user.user_id}, user.dict()) def save_community(self, community: Community): - print(community.dict()) self.db.communities.replace_one({'chat_id': community.chat_id}, community.dict()) def handle_commands(self, cmds: typing.List[str]): @@ -209,6 +205,11 @@ class Bot(telebot.TeleBot): self.send_template(meeting[0], 'meeting_info', community=community, meeting=meeting, person=person_2) self.send_template(meeting[1], 'meeting_info', community=community, meeting=meeting, person=person_1) + def community_post_edit_routine(self, community: Community): + community.schedule_meetings() + self.send_meeting_info_in_community(community) + self.save_community(community) + def register_next_step_handler(self, message, callback, *args, **kwargs): if isinstance(message, telebot.types.CallbackQuery): message = message.message diff --git a/community.py b/community.py index a5d1213..1965fb0 100644 --- a/community.py +++ b/community.py @@ -89,11 +89,19 @@ class Community: self.default_answers[user_id] = bool(answer) bot.save_community(self) + def have_already_met(self, user_id_1: int, user_id_2: int) -> bool: + """ + return True if there is a meeting with these users not earlier than 6 weeks ago + """ + return any( + {l, m} == {user_id_1, user_id_2} and r > date.today() - timedelta(days=42) + for l, m, r in self.scheduled_meetings) + def schedule_meetings(self): random.shuffle(self.pool) # It's called RandomTea for a reason today = date.today() while len(self.pool) > 1: user_id_1 = self.pool.pop() - while (user_id_2 := self.pool.pop()) == user_id_1: + while (user_id_2 := self.pool.pop()) == user_id_1 or self.have_already_met(user_id_1, user_id_2): continue self.scheduled_meetings.append((user_id_1, user_id_2, today))