From 5246c38cc8fa16558c7dc9236638fe24080e79c2 Mon Sep 17 00:00:00 2001 From: ennucore Date: Fri, 5 Aug 2022 09:15:54 +0300 Subject: [PATCH] Fix bug with repeating messages --- bot.py | 20 +++++++++++++------- user.py | 8 +++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/bot.py b/bot.py index a6eba73..7ddf572 100644 --- a/bot.py +++ b/bot.py @@ -193,10 +193,13 @@ class Bot(telebot.TeleBot): yield Community.from_dict(community_data) def poll_user_for_community(self, user: User, community: Community): - def_answer = False # community.default_answers.get(user.user_id, True) - self.send_template(user.user_id, 'poll_user', community=community, answer=def_answer) - community.pool += [user.user_id] * def_answer - community.polled[user.user_id] = datetime.date.today() + try: + def_answer = False # community.default_answers.get(user.user_id, True) + community.polled[user.user_id] = datetime.date.today() + self.send_template(user.user_id, 'poll_user', community=community, answer=def_answer) + community.pool += [user.user_id] * def_answer + except telebot.apihelper.ApiTelegramException: + pass def poll_users_in_community(self, community: Community): for user_id in community.users_to_poll(): @@ -223,9 +226,12 @@ class Bot(telebot.TeleBot): def send_meeting_info_in_community(self, community: Community): for meeting in community.scheduled_meetings: - person_1, person_2 = self.get_chat(meeting[0]), self.get_chat(meeting[1]) - 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) + try: + person_1, person_2 = self.get_chat(meeting[0]), self.get_chat(meeting[1]) + 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) + except telebot.apihelper.ApiTelegramException: + pass community.archived_meetings += community.scheduled_meetings community.scheduled_meetings = [] self.save_community(community) diff --git a/user.py b/user.py index 854c833..7f4d4a7 100644 --- a/user.py +++ b/user.py @@ -2,6 +2,9 @@ from __future__ import annotations from dataclasses import dataclass, field, asdict import typing import time + +import telebot + from community import Community from telebot.types import ChatMember @@ -28,7 +31,10 @@ class User: if chat_id in self.checked_chats: return False self.checked_chats.append(chat_id) - chat_member_info: ChatMember = bot.get_chat_member(chat_id, self.user_id) + try: + chat_member_info: ChatMember = bot.get_chat_member(chat_id, self.user_id) + except telebot.apihelper.ApiTelegramException: # user not found + return False return chat_member_info.is_member or chat_member_info.status in ['creator', 'administrator', 'admin', 'member'] def dict(self) -> dict: