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 blacklist_url(chat_id, domain): with URL_BLACKLIST_FILTER_INSERTION_LOCK: domain_filt = URLBlackListFilters(str(chat_id), domain) SESSION.merge(domain_filt) SESSION.commit() CHAT_URL_BLACKLISTS.setdefault(str(chat_id), set()).add(domain)
def add_to_blacklist(chat_id, trigger): with BLACKLIST_FILTER_INSERTION_LOCK: blacklist_filt = BlackListFilters(str(chat_id), trigger) SESSION.merge(blacklist_filt) # merge to avoid duplicate key issues SESSION.commit() CHAT_BLACKLISTS.setdefault(str(chat_id), set()).add(trigger)
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 rem_chat(chat_id): with INSERTION_LOCK: autochat = SESSION.query(ChatbotChats).get(str(chat_id)) if autochat: SESSION.delete(autochat) 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 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 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_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 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 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 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()
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_ses(chat_id, ses_id, expires): with INSERTION_LOCK: autochat = SESSION.query(ChatbotChats).get(str(chat_id)) if not autochat: autochat = ChatbotChats(str(chat_id), str(ses_id), str(expires)) else: autochat.ses_id = str(ses_id) autochat.expires = str(expires) SESSION.add(autochat) SESSION.commit()
def set_feds_setting(user_id: int, setting: bool): with FEDS_SETTINGS_LOCK: global FEDERATION_NOTIFICATION user_setting = SESSION.query(FedsUserSettings).get(user_id) if not user_setting: user_setting = FedsUserSettings(user_id) user_setting.should_report = setting FEDERATION_NOTIFICATION[str(user_id)] = setting SESSION.add(user_setting) SESSION.commit()
def add_note(chat_id, keyword, reply): adder = SESSION.query(NOTES).get((str(chat_id), keyword)) if adder: adder.reply = reply else: adder = NOTES(chat_id, keyword, reply) adder = NOTES( chat_id, keyword, reply, ) SESSION.add(adder) SESSION.commit()
def set_flood(chat_id, amount): with INSERTION_FLOOD_LOCK: flood = SESSION.query(FloodControl).get(str(chat_id)) if not flood: flood = FloodControl(str(chat_id)) flood.user_id = None flood.limit = amount CHAT_FLOOD[str(chat_id)] = (None, DEF_COUNT, amount) SESSION.add(flood) SESSION.commit()
def unsubs_fed(fed_id, my_fed): with FEDS_SUBSCRIBER_LOCK: getsubs = SESSION.query(FedSubs).get((fed_id, my_fed)) if getsubs: if my_fed in FEDS_SUBSCRIBER.get(fed_id, set()): # sanity check FEDS_SUBSCRIBER.get(fed_id, set()).remove(my_fed) SESSION.delete(getsubs) SESSION.commit() return True SESSION.close() return False
def rm_afk(user_id): with INSERTION_LOCK: curr = SESSION.query(AFK).get(user_id) if curr: if user_id in AFK_USERS: # sanity check del AFK_USERS[user_id] del AFK_USERSS[user_id] SESSION.delete(curr) SESSION.commit() return True SESSION.close() return False
def set_afk(user_id, reason, start_time=""): with INSERTION_LOCK: curr = SESSION.query(AFK).get(user_id) if not curr: curr = AFK(user_id, reason, True, start_time) else: curr.is_afk = True curr.reason = reason curr.start_time = time.time() AFK_USERS[user_id] = reason AFK_USERSS[user_id] = start_time SESSION.add(curr) SESSION.commit()
def rm_url_from_blacklist(chat_id, domain): with URL_BLACKLIST_FILTER_INSERTION_LOCK: domain_filt = SESSION.query(URLBlackListFilters).get( (str(chat_id), domain)) if domain_filt: if domain in CHAT_URL_BLACKLISTS.get(str(chat_id), set()): CHAT_URL_BLACKLISTS.get(str(chat_id), set()).remove(domain) SESSION.delete(domain_filt) SESSION.commit() return True SESSION.close() return False
def multi_fban_user( multi_fed_id, multi_user_id, multi_first_name, multi_last_name, multi_user_name, multi_reason, ): if True: # with FEDS_LOCK: counter = 0 time = 0 for x in range(len(multi_fed_id)): fed_id = multi_fed_id[x] user_id = multi_user_id[x] first_name = multi_first_name[x] last_name = multi_last_name[x] user_name = multi_user_name[x] reason = multi_reason[x] r = SESSION.query(BansF).all() for I in r: if I.fed_id == fed_id: if int(I.user_id) == int(user_id): SESSION.delete(I) r = BansF( str(fed_id), str(user_id), first_name, last_name, user_name, reason, time, ) SESSION.add(r) counter += 1 if str(str(counter)[-2:]) == "00": print(user_id) print(first_name) print(reason) print(counter) try: SESSION.commit() except: SESSION.rollback() return False finally: SESSION.commit() __load_all_feds_banned() print("Done") return counter
def remove_warn(user_id, chat_id): with WARN_INSERTION_LOCK: removed = False warned_user = SESSION.query(Warns).get((user_id, str(chat_id))) if warned_user and warned_user.num_warns > 0: warned_user.num_warns -= 1 SESSION.add(warned_user) SESSION.commit() removed = True SESSION.close() return removed
def subs_fed(fed_id, my_fed): check = get_spec_subs(fed_id, my_fed) if check: return False with FEDS_SUBSCRIBER_LOCK: subsfed = FedSubs(fed_id, my_fed) SESSION.merge(subsfed) # merge to avoid duplicate key issues SESSION.commit() global FEDS_SUBSCRIBER if FEDS_SUBSCRIBER.get(fed_id, set()) == set(): FEDS_SUBSCRIBER[fed_id] = {my_fed} else: FEDS_SUBSCRIBER.get(fed_id, set()).add(my_fed) return True
def rm_from_blacklist(chat_id, trigger): with BLACKLIST_FILTER_INSERTION_LOCK: blacklist_filt = SESSION.query(BlackListFilters).get( (str(chat_id), trigger)) if blacklist_filt: # sanity check if trigger in CHAT_BLACKLISTS.get(str(chat_id), set()): CHAT_BLACKLISTS.get(str(chat_id), set()).remove(trigger) SESSION.delete(blacklist_filt) SESSION.commit() return True SESSION.close() return False
def chat_join_fed(fed_id, chat_name, chat_id): with FEDS_LOCK: global FEDERATION_CHATS, FEDERATION_CHATS_BYID r = ChatF(chat_id, chat_name, fed_id) SESSION.add(r) FEDERATION_CHATS[str(chat_id)] = { "chat_name": chat_name, "fid": fed_id } checkid = FEDERATION_CHATS_BYID.get(fed_id) if checkid is None: FEDERATION_CHATS_BYID[fed_id] = [] FEDERATION_CHATS_BYID[fed_id].append(str(chat_id)) SESSION.commit() return r
def global_ignore_command(command): command = command.lower() with CLEANER_GLOBAL_LOCK: ignored = SESSION.query(CleanerBlueTextGlobal).get(str(command)) if not ignored: GLOBAL_IGNORE_COMMANDS.add(command) ignored = CleanerBlueTextGlobal(str(command)) SESSION.add(ignored) SESSION.commit() return True SESSION.close() return False
def global_unignore_command(command): command = command.lower() with CLEANER_GLOBAL_LOCK: unignored = SESSION.query(CleanerBlueTextGlobal).get(str(command)) if unignored: if command in GLOBAL_IGNORE_COMMANDS: GLOBAL_IGNORE_COMMANDS.remove(command) SESSION.delete(command) SESSION.commit() return True SESSION.close() return False
def un_fban_user(fed_id, user_id): with FEDS_LOCK: r = SESSION.query(BansF).all() for I in r: if I.fed_id == fed_id: if int(I.user_id) == int(user_id): SESSION.delete(I) try: SESSION.commit() except: SESSION.rollback() return False finally: SESSION.commit() __load_all_feds_banned() return I