def __load_feds_subscriber(): global FEDS_SUBSCRIBER global MYFEDS_SUBSCRIBER try: feds = SESSION.query(FedSubs.fed_id).distinct().all() for (fed_id,) in feds: # remove tuple by ( ,) FEDS_SUBSCRIBER[fed_id] = [] MYFEDS_SUBSCRIBER[fed_id] = [] all_fedsubs = SESSION.query(FedSubs).all() for x in all_fedsubs: FEDS_SUBSCRIBER[x.fed_id] += [x.fed_subs] try: MYFEDS_SUBSCRIBER[x.fed_subs] += [x.fed_id] except KeyError: getsubs = SESSION.query(FedSubs).get((x.fed_id, x.fed_subs)) if getsubs: SESSION.delete(getsubs) SESSION.commit() FEDS_SUBSCRIBER = {x: set(y) for x, y in FEDS_SUBSCRIBER.items()} MYFEDS_SUBSCRIBER = {x: set(y) for x, y in MYFEDS_SUBSCRIBER.items()} finally: SESSION.close()
def del_fed(fed_id): with FEDS_LOCK: global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME, FEDERATION_CHATS, FEDERATION_CHATS_BYID, FEDERATION_BANNED_USERID, FEDERATION_BANNED_FULL getcache = FEDERATION_BYFEDID.get(fed_id) if getcache is None: return False # Variables getfed = FEDERATION_BYFEDID.get(fed_id) owner_id = getfed["owner"] fed_name = getfed["fname"] # Delete from cache FEDERATION_BYOWNER.pop(owner_id) FEDERATION_BYFEDID.pop(fed_id) FEDERATION_BYNAME.pop(fed_name) if FEDERATION_CHATS_BYID.get(fed_id): for x in FEDERATION_CHATS_BYID[fed_id]: delchats = SESSION.query(ChatF).get(str(x)) if delchats: SESSION.delete(delchats) SESSION.commit() FEDERATION_CHATS.pop(x) FEDERATION_CHATS_BYID.pop(fed_id) # Delete fedban users getall = FEDERATION_BANNED_USERID.get(fed_id) if getall: for x in getall: banlist = SESSION.query(BansF).get((fed_id, str(x))) if banlist: SESSION.delete(banlist) SESSION.commit() if FEDERATION_BANNED_USERID.get(fed_id): FEDERATION_BANNED_USERID.pop(fed_id) if FEDERATION_BANNED_FULL.get(fed_id): FEDERATION_BANNED_FULL.pop(fed_id) # Delete fedsubs getall = MYFEDS_SUBSCRIBER.get(fed_id) if getall: for x in getall: getsubs = SESSION.query(FedSubs).get((fed_id, str(x))) if getsubs: SESSION.delete(getsubs) SESSION.commit() if FEDS_SUBSCRIBER.get(fed_id): FEDS_SUBSCRIBER.pop(fed_id) if MYFEDS_SUBSCRIBER.get(fed_id): MYFEDS_SUBSCRIBER.pop(fed_id) # Delete from database curr = SESSION.query(Federations).get(fed_id) if curr: SESSION.delete(curr) SESSION.commit() return True
def __load_chat_blacklists(): global CHAT_BLACKLISTS try: chats = SESSION.query(BlackListFilters.chat_id).distinct().all() for (chat_id, ) in chats: # remove tuple by ( ,) CHAT_BLACKLISTS[chat_id] = [] all_filters = SESSION.query(BlackListFilters).all() for x in all_filters: CHAT_BLACKLISTS[x.chat_id] += [x.trigger] CHAT_BLACKLISTS = {x: set(y) for x, y in CHAT_BLACKLISTS.items()} finally: SESSION.close()
def get_all_notes(chat_id): try: return SESSION.query(NOTES).filter(NOTES.chat_id == str(chat_id)).all() except BaseException: return None finally: SESSION.close()
def get_notes(chat_id, keyword): try: return SESSION.query(NOTES).get((str(chat_id), keyword)) except BaseException: return None finally: SESSION.close()
def add_filter( chat_id, keyword, reply, snip_type, media_id, media_access_hash, media_file_reference, ): adder = SESSION.query(Filters).get((str(chat_id), keyword)) if adder: adder.reply = reply adder.snip_type = snip_type adder.media_id = media_id adder.media_access_hash = media_access_hash adder.media_file_reference = media_file_reference else: adder = Filters( chat_id, keyword, reply, snip_type, media_id, media_access_hash, media_file_reference, ) SESSION.add(adder) SESSION.commit()
def is_gmuted(sender_id): try: return SESSION.query(GMute).all() except BaseException: return None finally: SESSION.close()
def _load_chat_blacklist(): global CHAT_URL_BLACKLISTS try: chats = SESSION.query(URLBlackListFilters.chat_id).distinct().all() for (chat_id, ) in chats: CHAT_URL_BLACKLISTS[chat_id] = [] all_urls = SESSION.query(URLBlackListFilters).all() for url in all_urls: CHAT_URL_BLACKLISTS[url.chat_id] += [url.domain] CHAT_URL_BLACKLISTS = { k: set(v) for k, v in CHAT_URL_BLACKLISTS.items() } finally: SESSION.close()
def migrate_chat(old_chat_id, new_chat_id): with CHAT_LOCK: chat_notes = (SESSION.query(ReportingChatSettings).filter( ReportingChatSettings.chat_id == str(old_chat_id)).all()) for note in chat_notes: note.chat_id = str(new_chat_id) SESSION.commit()
def rem_chat(chat_id): with INSERTION_LOCK: autochat = SESSION.query(ChatbotChats).get(str(chat_id)) if autochat: SESSION.delete(autochat) SESSION.commit()
def get_filter(chat_id, keyword): try: return SESSION.query(Filters).get((str(chat_id), keyword)) except BaseException: return None finally: SESSION.close()
def __load_all_feds_settings(): global FEDERATION_NOTIFICATION try: getuser = SESSION.query(FedsUserSettings).all() for x in getuser: FEDERATION_NOTIFICATION[str(x.user_id)] = x.should_report finally: SESSION.close()
def get_warn_strength(chat_id): try: setting = SESSION.query(WarnSettings).get(str(chat_id)) if setting: return setting.soft_warn return "ban" finally: SESSION.close()
def user_should_report(user_id: int) -> bool: try: user_setting = SESSION.query(ReportingUserSettings).get(user_id) if user_setting: return user_setting.should_report return True finally: SESSION.close()
def chat_should_report(chat_id: Union[str, int]) -> bool: try: chat_setting = SESSION.query(ReportingChatSettings).get(str(chat_id)) if chat_setting: return chat_setting.should_report return False finally: SESSION.close()
def get_all_filters(chat_id): try: return SESSION.query(Filters).filter( Filters.chat_id == str(chat_id)).all() except BaseException: return None finally: SESSION.close()
def get_rules(chat_id): rules = SESSION.query(Rules).get(str(chat_id)) ret = "" if rules: ret = rules.rules SESSION.close() return ret
def get_current_goodbye_settings(chat_id): try: return SESSION.query(Goodbye).filter( Goodbye.chat_id == str(chat_id)).one() except: return None finally: SESSION.close()
def is_chat(chat_id): try: chat = SESSION.query(ChatbotChats).get(str(chat_id)) if chat: return True return False finally: SESSION.close()
def get_current_welcome_settings(chat_id): try: return SESSION.query(Welcome).filter( Welcome.chat_id == str(chat_id)).one() except: return None finally: SESSION.close()
def __load_afk_users(): global AFK_USERS global AFK_USERSS try: all_afk = SESSION.query(AFK).all() AFK_USERS = {user.user_id: user.reason for user in all_afk if user.is_afk} AFK_USERSS = {user.user_id: user.start_time for user in all_afk if user.is_afk} finally: SESSION.close()
def set_rules(chat_id, rules_text): with INSERTION_LOCK: rules = SESSION.query(Rules).get(str(chat_id)) if not rules: rules = Rules(str(chat_id)) rules.rules = rules_text SESSION.add(rules) SESSION.commit()
def set_chat_setting(chat_id: Union[int, str], setting: bool): with CHAT_LOCK: chat_setting = SESSION.query(ReportingChatSettings).get(str(chat_id)) if not chat_setting: chat_setting = ReportingChatSettings(chat_id) chat_setting.should_report = setting SESSION.add(chat_setting) SESSION.commit()
def set_user_setting(user_id: int, setting: bool): with USER_LOCK: user_setting = SESSION.query(ReportingUserSettings).get(user_id) if not user_setting: user_setting = ReportingUserSettings(user_id) user_setting.should_report = setting SESSION.add(user_setting) SESSION.commit()
def get_warn_setting(chat_id): try: setting = SESSION.query(WarnSettings).get(str(chat_id)) if setting: return setting.warn_limit, setting.soft_warn return 3, "ban" finally: SESSION.close()
def get_ses(chat_id): autochat = SESSION.query(ChatbotChats).get(str(chat_id)) sesh = "" exp = "" if autochat: sesh = str(autochat.ses_id) exp = str(autochat.expires) SESSION.close() return sesh, exp
def __load_flood_settings(): global CHAT_FLOOD try: all_chats = SESSION.query(FloodControl).all() CHAT_FLOOD = { chat.chat_id: (None, DEF_COUNT, chat.limit) for chat in all_chats } finally: SESSION.close()
def migrate_chat(old_chat_id, new_chat_id): with INSERTION_FLOOD_LOCK: flood = SESSION.query(FloodControl).get(str(old_chat_id)) if flood: CHAT_FLOOD[str(new_chat_id)] = CHAT_FLOOD.get( str(old_chat_id), DEF_OBJ) flood.chat_id = str(new_chat_id) SESSION.commit() SESSION.close()
def get_flood_setting(chat_id): try: setting = SESSION.query(FloodSettings).get(str(chat_id)) if setting: return setting.flood_type, setting.value else: return 1, "0" finally: SESSION.close()
def get_warns(user_id, chat_id): try: user = SESSION.query(Warns).get((user_id, str(chat_id))) if not user: return None reasons = user.reasons num = user.num_warns return num, reasons finally: SESSION.close()