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 __load_cleaner_list(): global GLOBAL_IGNORE_COMMANDS global CLEANER_CHATS try: GLOBAL_IGNORE_COMMANDS = { int(x.command) for x in SESSION.query(CleanerBlueTextGlobal).all() } finally: SESSION.close() try: for x in SESSION.query(CleanerBlueTextChatSettings).all(): CLEANER_CHATS.setdefault(x.chat_id, { "setting": False, "commands": set() }) CLEANER_CHATS[x.chat_id]["setting"] = x.is_enable finally: SESSION.close() try: for x in SESSION.query(CleanerBlueTextChat).all(): CLEANER_CHATS.setdefault(x.chat_id, { "setting": False, "commands": set() }) CLEANER_CHATS[x.chat_id]["commands"].add(x.command) 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 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 get_filter(chat_id, keyword): try: return SESSION.query(Filters).get((str(chat_id), keyword)) except BaseException: return None 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 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 _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 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 rem_chat(chat_id): with INSERTION_LOCK: autochat = SESSION.query(ChatbotChats).get(str(chat_id)) if autochat: SESSION.delete(autochat) SESSION.commit()
def get_notes(chat_id, keyword): try: return SESSION.query(NOTES).get((str(chat_id), keyword)) except BaseException: return None finally: SESSION.close()
def is_nightmode_indb(chat_id: str): try: s__ = SESSION.query(Nightmode).get(str(chat_id)) if s__: return str(s__.chat_id) finally: SESSION.close()
def is_nsfwatch_indb(chat_id: str): try: s__ = SESSION.query(Nsfwatch).get(str(chat_id)) if s__: return str(s__.chat_id) 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_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 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 is_chat(chat_id): try: chat = SESSION.query(ChatbotChats).get(str(chat_id)) if chat: return True return False 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 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_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 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 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 user_demote_fed(fed_id, user_id): with FEDS_LOCK: global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME # Variables getfed = FEDERATION_BYFEDID.get(str(fed_id)) owner_id = getfed["owner"] fed_name = getfed["fname"] fed_rules = getfed["frules"] fed_log = getfed["flog"] # Temp set try: members = eval(eval(getfed["fusers"])["members"]) except ValueError: return False members.remove(user_id) # Set user FEDERATION_BYOWNER[str(owner_id)]["fusers"] = str({ "owner": str(owner_id), "members": str(members) }) FEDERATION_BYFEDID[str(fed_id)]["fusers"] = str({ "owner": str(owner_id), "members": str(members) }) FEDERATION_BYNAME[fed_name]["fusers"] = str({ "owner": str(owner_id), "members": str(members) }) # Set on database fed = Federations( str(owner_id), fed_name, str(fed_id), fed_rules, fed_log, str({ "owner": str(owner_id), "members": str(members) }), ) SESSION.merge(fed) SESSION.commit() return True curr = SESSION.query(UserF).all() result = False for r in curr: if int(r.user_id) == int(user_id): if r.fed_id == fed_id: SESSION.delete(r) SESSION.commit() result = True SESSION.close() return result
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 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()
def reset_warns(user_id, chat_id): with WARN_INSERTION_LOCK: warned_user = SESSION.query(Warns).get((user_id, str(chat_id))) if warned_user: warned_user.num_warns = 0 warned_user.reasons = "" SESSION.add(warned_user) SESSION.commit() SESSION.close()
def set_warn_strength(chat_id, soft_warn): with WARN_SETTINGS_LOCK: curr_setting = SESSION.query(WarnSettings).get(str(chat_id)) if not curr_setting: curr_setting = WarnSettings(chat_id, soft_warn=soft_warn) curr_setting.soft_warn = soft_warn SESSION.add(curr_setting) SESSION.commit()
def set_warn_limit(chat_id, warn_limit): with WARN_SETTINGS_LOCK: curr_setting = SESSION.query(WarnSettings).get(str(chat_id)) if not curr_setting: curr_setting = WarnSettings(chat_id, warn_limit=warn_limit) curr_setting.warn_limit = warn_limit SESSION.add(curr_setting) SESSION.commit()