diff options
author | Dmitrii Morozov <snoopdesigns@gmail.com> | 2024-04-11 21:17:51 +0200 |
---|---|---|
committer | Dmitrii Morozov <snoopdesigns@gmail.com> | 2024-04-11 21:17:51 +0200 |
commit | 1e2ffe48cb470017b1d760238186dc625356ee2e (patch) | |
tree | fde96ff9071767cdc57e55c9bea298f05078ab7a | |
parent | 08cd4e0b8e6473dcf8dc18915b00aafc86f13215 (diff) |
Remove user if was kicked from chat
-rw-r--r-- | persistence.py | 18 | ||||
-rwxr-xr-x | tgbot.py | 22 |
2 files changed, 28 insertions, 12 deletions
diff --git a/persistence.py b/persistence.py index d2998e9..941e2f7 100644 --- a/persistence.py +++ b/persistence.py @@ -9,7 +9,7 @@ class UserRepository: def __initialize(self): cur = self.__getConnection().cursor() - cur.execute("CREATE TABLE IF NOT EXISTS user(chat_id INT)") + cur.execute("CREATE TABLE IF NOT EXISTS user(chat_id INT, alias TEXT)") cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS chat_id_idx ON user(chat_id)") def __getConnection(self): @@ -28,14 +28,22 @@ class UserRepository: query = "select * from user" cur.execute(query) return cur.fetchall() + + def removeChat(self, chat_id): + connection = self.__getConnection() + cur = connection.cursor() + query = "DELETE FROM user where chat_id = {chat_id}".format( + chat_id = chat_id) + cur.execute(query) + connection.commit() - def putUser(self, chat_id): + def putChat(self, chat_id, alias): if not self.getUser(chat_id): connection = self.__getConnection() cur = connection.cursor() - query = "INSERT INTO user(chat_id) VALUES({chat_id})".format(chat_id = chat_id) + query = "INSERT INTO user(chat_id, alias) VALUES({chat_id}, '{text}')".format( + chat_id = chat_id, + text = alias) cur.execute(query) connection.commit() - else: - print("User {} already exsits, skipping putUser()".format(chat_id)) @@ -40,8 +40,12 @@ fortniteStatusWrapper = FortniteStatusNotifier(FortniteStatusObserver()) fortniteClient = FortniteClient(FortnitePresenceObserver()) @bot.message_handler(commands = ['start']) -async def startCommand(message): - userRepository.putUser(message.chat.id) +async def startCommand(message: telebot.types.Message): + if message.chat.type == 'private': + alias = message.chat.username + else: + alias = message.chat.title + userRepository.putUser(message.chat.id, alias) await reply(message, 'This chat successfully registered to receive Fortnite updates') @bot.message_handler(commands = ['status']) @@ -80,11 +84,15 @@ async def addUser(message): async def send_message_to_all(message_text: str): for user in userRepository.getAllUsers(): - await bot.send_message( - user[0], - message_text, - parse_mode='MarkdownV2' - ) + try: + await bot.send_message( + user[0], + message_text, + parse_mode='MarkdownV2' + ) + except Exception as error: + if 'bot was kicked from the group chat' in str(error): + userRepository.removeChat(user[0]) async def reply(message, message_text): await bot.reply_to( |