def get_buttons(chat_id, keyword): try: return (SESSION.query(Buttons).filter( Buttons.chat_id == str(chat_id), Buttons.keyword == keyword).order_by(Buttons.id).all()) finally: SESSION.close()
def list_approved(chat_id): try: return (SESSION.query(Approvals).filter( Approvals.chat_id == str(chat_id)).order_by( Approvals.user_id.asc()).all()) finally: SESSION.close()
def get_note(chat_id, note_name): try: return (SESSION.query(Notes).filter( func.lower(Notes.name) == note_name, Notes.chat_id == str(chat_id)).first()) finally: SESSION.close()
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 get_user_com_chats(user_id): try: chat_members = (SESSION.query(ChatMembers).filter( ChatMembers.user == int(user_id)).all()) return [i.chat for i in chat_members] finally: SESSION.close()
def get_buttons(chat_id, note_name): try: return (SESSION.query(Buttons).filter( Buttons.chat_id == str(chat_id), Buttons.note_name == note_name).order_by(Buttons.id).all()) 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 migrate_chat(old_chat_id, new_chat_id): with CHAT_LOCK: chat_notes = (SESSION.query(AntiArabicChatSettings).filter( AntiArabicChatSettings.chat_id == str(old_chat_id)).all()) for note in chat_notes: note.chat_id = str(new_chat_id) SESSION.commit()
def set_blacklist_strength(chat_id, blacklist_type, value): # for blacklist_type # 0 = nothing # 1 = delete # 2 = warn # 3 = mute # 4 = kick # 5 = ban # 6 = tban # 7 = tmute with STICKSET_FILTER_INSERTION_LOCK: global CHAT_BLSTICK_BLACKLISTS curr_setting = SESSION.query(StickerSettings).get(str(chat_id)) if not curr_setting: curr_setting = StickerSettings(chat_id, blacklist_type=int(blacklist_type), value=value) curr_setting.blacklist_type = int(blacklist_type) curr_setting.value = str(value) CHAT_BLSTICK_BLACKLISTS[str(chat_id)] = { "blacklist_type": int(blacklist_type), "value": value, } SESSION.add(curr_setting) SESSION.commit()
def migrate_chat(old_chat_id, new_chat_id): with INSERTION_LOCK: chat = SESSION.query(Welcome).get(str(old_chat_id)) if chat: chat.chat_id = str(new_chat_id) with WELC_BTN_LOCK: chat_buttons = ( SESSION.query(WelcomeButtons) .filter(WelcomeButtons.chat_id == str(old_chat_id)) .all() ) for btn in chat_buttons: btn.chat_id = str(new_chat_id) with LEAVE_BTN_LOCK: chat_buttons = ( SESSION.query(GoodbyeButtons) .filter(GoodbyeButtons.chat_id == str(old_chat_id)) .all() ) for btn in chat_buttons: btn.chat_id = str(new_chat_id) SESSION.commit() #made by AKAY
def new_fed(owner_id, fed_name, fed_id): with FEDS_LOCK: global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME fed = Federations( str(owner_id), fed_name, str(fed_id), "Rules is not set in this federation.", None, str({"owner": str(owner_id), "members": "[]"}), ) SESSION.add(fed) SESSION.commit() FEDERATION_BYOWNER[str(owner_id)] = { "fid": str(fed_id), "fname": fed_name, "frules": "Rules is not set in this federation.", "flog": None, "fusers": str({"owner": str(owner_id), "members": "[]"}), } FEDERATION_BYFEDID[str(fed_id)] = { "owner": str(owner_id), "fname": fed_name, "frules": "Rules is not set in this federation.", "flog": None, "fusers": str({"owner": str(owner_id), "members": "[]"}), } FEDERATION_BYNAME[fed_name] = { "fid": str(fed_id), "owner": str(owner_id), "frules": "Rules is not set in this federation.", "flog": None, "fusers": str({"owner": str(owner_id), "members": "[]"}), } return fed
def user_join_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 members = eval(eval(getfed["fusers"])["members"]) members.append(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() __load_all_feds_chats() return True
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 disapprove(chat_id): rem = SESSION.query(forceSubscribe).get(chat_id) if rem: SESSION.delete(rem) SESSION.commit() #MADE BY AKAY
def migrate_chat(old_chat_id, new_chat_id): with STICKERS_FILTER_INSERTION_LOCK: chat_filters = (SESSION.query(StickersFilters).filter( StickersFilters.chat_id == str(old_chat_id)).all()) for filt in chat_filters: filt.chat_id = str(new_chat_id) SESSION.commit()
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 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 migrate_chat(old_chat_id, new_chat_id): with GBAN_SETTING_LOCK: chat = SESSION.query(GbanSettings).get(str(old_chat_id)) if chat: chat.chat_id = new_chat_id SESSION.add(chat) SESSION.commit()
def __load_gban_stat_list(): global GBANSTAT_LIST try: GBANSTAT_LIST = { x.chat_id for x in SESSION.query(GbanSettings).all() if not x.setting } finally: SESSION.close()
def get_clean_pref(chat_id): welc = SESSION.query(Welcome).get(str(chat_id)) SESSION.close() if welc: return welc.clean_welcome return False
def get_welc_mutes_pref(chat_id): welcomemutes = SESSION.query(WelcomeMute).get(str(chat_id)) SESSION.close() if welcomemutes: return welcomemutes.welcomemutes return False
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 get_chat_filters(chat_id): try: return (SESSION.query(CustomFilters).filter( CustomFilters.chat_id == str(chat_id)).order_by( func.length(CustomFilters.keyword).desc()).order_by( CustomFilters.keyword.asc()).all()) finally: SESSION.close()
def get_custom_gdbye(chat_id): welcome_settings = SESSION.query(Welcome).get(str(chat_id)) ret = DEFAULT_GOODBYE if welcome_settings and welcome_settings.custom_leave: ret = welcome_settings.custom_leave SESSION.close() return ret
def get_custom_welcome(chat_id): welcome_settings = SESSION.query(Welcome).get(str(chat_id)) ret = DEFAULT_WELCOME if welcome_settings and welcome_settings.custom_welcome: ret = welcome_settings.custom_welcome SESSION.close() return ret
def chat_antiarabic(chat_id: Union[str, int]) -> bool: try: chat_setting = SESSION.query(AntiArabicChatSettings).get(str(chat_id)) if chat_setting: return chat_setting.antiarabic return False finally: SESSION.close()
def welcome_mutes(chat_id): try: welcomemutes = SESSION.query(WelcomeMute).get(str(chat_id)) if welcomemutes: return welcomemutes.welcomemutes return False finally: SESSION.close()
def get_gdbye_pref(chat_id): welc = SESSION.query(Welcome).get(str(chat_id)) SESSION.close() if welc: return welc.should_goodbye, welc.custom_leave, welc.leave_type else: # Welcome by default. return True, DEFAULT_GOODBYE, Types.TEXT
def clean_service(chat_id: Union[str, int]) -> bool: try: chat_setting = SESSION.query(CleanServiceSetting).get(str(chat_id)) if chat_setting: return chat_setting.clean_service return False finally: SESSION.close()
def add_channel(chat_id, channel): adder = SESSION.query(forceSubscribe).get(chat_id) if adder: adder.channel = channel else: adder = forceSubscribe(chat_id, channel) SESSION.add(adder) SESSION.commit()