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 add_note_to_db(chat_id, note_name, note_data, is_reply=False, buttons=None): if not buttons: buttons = [] with NOTES_INSERTION_LOCK: prev = SESSION.query(Notes).get((str(chat_id), note_name)) if prev: with BUTTONS_INSERTION_LOCK: prev_buttons = SESSION.query(Buttons).filter( Buttons.chat_id == str(chat_id), Buttons.note_name == note_name).all() for btn in prev_buttons: SESSION.delete(btn) SESSION.delete(prev) note = Notes(str(chat_id), note_name, note_data, is_reply=is_reply, has_buttons=bool(buttons)) SESSION.add(note) SESSION.commit() for b_name, url, same_line in buttons: add_note_button_to_db(chat_id, note_name, b_name, url, same_line)
def set_custom_gdbye(chat_id, custom_goodbye, goodbye_type, buttons=None): if buttons is None: buttons = [] with INSERTION_LOCK: welcome_settings = SESSION.query(Welcome).get(str(chat_id)) if not welcome_settings: welcome_settings = Welcome(str(chat_id), True) if custom_goodbye: welcome_settings.custom_leave = custom_goodbye welcome_settings.leave_type = goodbye_type.value else: welcome_settings.custom_leave = DEFAULT_GOODBYE welcome_settings.leave_type = Types.TEXT.value SESSION.add(welcome_settings) with LEAVE_BTN_LOCK: prev_buttons = SESSION.query(GoodbyeButtons).filter( GoodbyeButtons.chat_id == str(chat_id)).all() for btn in prev_buttons: SESSION.delete(btn) for b_name, url, same_line in buttons: button = GoodbyeButtons(chat_id, b_name, url, same_line) SESSION.add(button) SESSION.commit()
def add_note_to_db(chat_id, note_name, note_data, msgtype, buttons=None, file=None): if not buttons: buttons = [] with NOTES_INSERTION_LOCK: prev = SESSION.query(Notes).get((str(chat_id), note_name)) if prev: with BUTTONS_INSERTION_LOCK: prev_buttons = SESSION.query(Buttons).filter( Buttons.chat_id == str(chat_id), Buttons.note_name == note_name).all() for btn in prev_buttons: SESSION.delete(btn) SESSION.delete(prev) note = Notes(str(chat_id), note_name, note_data or "", msgtype=msgtype.value, file=file) SESSION.add(note) SESSION.commit() for b_name, url, same_line in buttons: add_note_button_to_db(chat_id, note_name, b_name, url, same_line)
def gkick_reset(user_id): user = SESSION.query(GloballyKickedUsers).get(user_id) if user: user.times = 0 SESSION.delete(user) SESSION.commit() __load_gkick_userid_list()
def ungmute_user(user_id): with GMUTED_USERS_LOCK: user = SESSION.query(GloballyMutedUsers).get(user_id) if user: SESSION.delete(user) SESSION.commit() __load_gmuted_userid_list()
def unblacklistChat(chat_id): with BANCHATLOCK: chat = SESSION.query(BannedChat).get(chat_id) if chat: SESSION.delete(chat) SESSION.commit() __load_blacklisted_chats_list()
def add_filter(chat_id, keyword, reply, is_sticker=False, is_document=False, is_image=False, is_audio=False, is_voice=False, is_video=False, buttons=None): if buttons is None: buttons = [] with CUST_FILT_LOCK: prev = SESSION.query(CustomFilters).get((str(chat_id), keyword)) if prev: with BUTTON_LOCK: prev_buttons = SESSION.query(Buttons).filter( Buttons.chat_id == str(chat_id), Buttons.keyword == keyword).all() for btn in prev_buttons: SESSION.delete(btn) SESSION.delete(prev) filt = CustomFilters(str(chat_id), keyword, reply, is_sticker, is_document, is_image, is_audio, is_voice, is_video, bool(buttons)) SESSION.add(filt) SESSION.commit() for b_name, url, same_line in buttons: add_note_button_to_db(chat_id, keyword, b_name, url, same_line)
def rem_chat(chat_id): with INSERTION_LOCK: autochat = SESSION.query(ChatbotChats).get(str(chat_id)) if autochat: SESSION.delete(autochat) SESSION.commit()
def stop_chat_logging(chat_id): with LOGS_INSERTION_LOCK: res = SESSION.query(GroupLogs).get(str(chat_id)) if res: log_channel = res.log_channel SESSION.delete(res) SESSION.commit() return log_channel
def ungsupport_user(user_id): with SUPPORT_USERS_LOCK: user = SESSION.query(SupportUsers).get(user_id) if user: SESSION.delete(user) SESSION.commit() __load_support_userid_list()
def unregister_repo(chat_id): chat = SESSION.query(GithubConfig).filter(GithubConfig.chat_id == str(chat_id)).first() if not chat: return "This chat isn't tracking any repos!" repo = chat.repo SESSION.delete(chat) SESSION.commit() return "Stopped tracking *%s*." % repo
def ungkick_user(user_id): with GKICKED_USERS_LOCK: user = SESSION.query(GloballyKickedUsers).get(user_id) if user: SESSION.delete(user) SESSION.commit() __load_gkicked_userid_list()
def del_birthday(user_id): with BIRTHDAY_LOCK: user = SESSION.query(Birthdays).get(user_id) if user: SESSION.delete(user) SESSION.commit() __load_birthday_userid_list()
def setKickTime(chat_id, value): with AUTOKICK_LOCK: prevObj = SESSION.query(AutoKickSafeMode).get(str(chat_id)) if prevObj: SESSION.delete(prevObj) newObj = AutoKickSafeMode(str(chat_id), int(value)) SESSION.add(newObj) SESSION.commit()
def setDefenseStatus(chat_id, status): with DEFENSE_LOCK: prevObj = SESSION.query(DefenseMode).get(str(chat_id)) if prevObj: SESSION.delete(prevObj) newObj = DefenseMode(str(chat_id), status) SESSION.add(newObj) SESSION.commit()
def remove_chat_summons(chat_id): try: curr_summons = SESSION.query(Summons).filter(Summons.chat_id == str(chat_id)).all() for curr in curr_summons: SESSION.delete(curr) SESSION.commit() finally: SESSION.close()
def unblacklist_user(user_id): with BLACKLIST_LOCK: user = SESSION.query(BlacklistUsers).get(str(user_id)) if user: SESSION.delete(user) SESSION.commit() __load_blacklist_userid_list()
def set_welcome_mutes(chat_id, welcomemutes): with WM_LOCK: prev = SESSION.query(WelcomeMute).get((str(chat_id))) if prev: SESSION.delete(prev) welcome_m = WelcomeMute(str(chat_id), welcomemutes) SESSION.add(welcome_m) SESSION.commit()
def add_repo_to_db(chat_id, name, value, backoffset): with GIT_LOCK: prev = SESSION.query(GitHub).get((str(chat_id), name)) if prev: SESSION.delete(prev) repo = GitHub(str(chat_id), name, value, backoffset) SESSION.add(repo) SESSION.commit()
def ungban_user(user_id): with GBANNED_USERS_LOCK: user = SESSION.query(GloballyBannedUsers).get(user_id) if user: SESSION.delete(user) SESSION.commit() __load_gbanned_userid_list()
def set_welcome_security(chat_id, security): with WS_LOCK: prev = SESSION.query(WelcomeSecurity).get((str(chat_id))) if prev: SESSION.delete(prev) welcome_s = WelcomeSecurity(str(chat_id), security) SESSION.add(welcome_s) SESSION.commit()
def rem_chat(chat_id): with INSERTION_LOCK: chat = SESSION.query(Chats).get(str(chat_id)) if chat: SESSION.delete(chat) SESSION.commit() else: SESSION.close()
def switch_to_locale(chat_id, locale_name): with LOCALES_INSERTION_LOCK: prev = SESSION.query(Locales).get((str(chat_id))) if prev: SESSION.delete(prev) switch_locale = Locales(str(chat_id), locale_name) SESSION.add(switch_locale) SESSION.commit()
def remove_url(tg_chat_id, tg_feed_link): with INSERTION_LOCK: # this loops to delete any possible duplicates for the same TG User ID, TG Chat ID and link for row in check_url_availability(tg_chat_id, tg_feed_link): # add the action to the DB query SESSION.delete(row) SESSION.commit()
def set_clearnotes(chat_id, clearnotes=False, timer=120): with CLEARNOTES_INSERTION_LOCK: exists = SESSION.query(ClearNotes).get(str(chat_id)) if exists: SESSION.delete(exists) clearnotes = ClearNotes(str(chat_id), int(timer)) SESSION.add(clearnotes) SESSION.commit()
def ungpromote_user(user_id): with SUDO_USERS_LOCK: user = SESSION.query(SudoUsers).get(user_id) if user: SESSION.delete(user) SESSION.commit() __load_sudo_userid_list()
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 = ast.literal_eval( ast.literal_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 remove_filter(chat_id, keyword): with CUST_FILT_LOCK: filt = SESSION.query(CustomFilters).get((str(chat_id), keyword)) if filt: SESSION.delete(filt) SESSION.commit() return True SESSION.close() return False
def connect(user_id, chat_id): with CONNECTION_INSERTION_LOCK: prev = SESSION.query(Connection).get((int(user_id))) if prev: SESSION.delete(prev) connect_to_chat = Connection(int(user_id), chat_id) SESSION.add(connect_to_chat) SESSION.commit() return True