|
@@ -45,6 +45,39 @@ async def handle_patrons_list_file(bot: davtelepot.bot.Bot, update: dict, langua
|
|
|
return bot.get_message('patreon', 'list_updated', language=language)
|
|
|
|
|
|
|
|
|
+async def handle_left_chat_event(bot, update):
|
|
|
+ if update['chat']['id'] != bot.shared_data['bic_chat_id']:
|
|
|
+ return
|
|
|
+ user_record = bot.db['users'].find_one(telegram_id=update['left_chat_member']['id'])
|
|
|
+ bot.db['patrons'].upsert(
|
|
|
+ dict(
|
|
|
+ user_id=user_record['id'],
|
|
|
+ is_in_chat=False
|
|
|
+ ),
|
|
|
+ ['user_id']
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+async def handle_new_members(bot, update):
|
|
|
+ if update['chat']['id'] != bot.shared_data['bic_chat_id']:
|
|
|
+ return
|
|
|
+ for member in update['new_chat_members']:
|
|
|
+ user_record = bot.db['users'].find_one(telegram_id=member['id'])
|
|
|
+ patron_record = bot.db['patrons'].find_one(user_id=user_record['id'])
|
|
|
+ # If user is not white-listed, kick them
|
|
|
+ if patron_record is None or not patron_record['tier']:
|
|
|
+ await bot.kickChatMember(chat_id=bot.shared_data['bic_chat_id'],
|
|
|
+ user_id=user_record['telegram_id'])
|
|
|
+ else: # Otherwise, take not of their joining
|
|
|
+ bot.db['patrons'].upsert(
|
|
|
+ dict(
|
|
|
+ user_id=user_record['id'],
|
|
|
+ is_in_chat=True
|
|
|
+ ),
|
|
|
+ ['user_id']
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
def init(telegram_bot: davtelepot.bot.Bot):
|
|
|
telegram_bot.messages['patreon'] = patreon_messages
|
|
|
if 'patrons' not in telegram_bot.db.tables:
|
|
@@ -67,3 +100,6 @@ def init(telegram_bot: davtelepot.bot.Bot):
|
|
|
language: str):
|
|
|
return await handle_patrons_list_file(bot=bot, update=update,
|
|
|
language=language)
|
|
|
+
|
|
|
+ telegram_bot.set_message_handler('new_chat_members', handle_new_members)
|
|
|
+ telegram_bot.set_message_handler('left_chat_member', handle_left_chat_event)
|