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 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 get_filter(chat_id, keyword): try: return SESSION.query(Filters).get((str(chat_id), keyword)) 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 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_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_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 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()
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_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 fs_settings(chat_id): try: return ( SESSION.query(forceSubscribe) .filter(forceSubscribe.chat_id == chat_id) .one() ) except: return None finally: 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 _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 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 __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 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 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 remove_all_filters(chat_id): saved_filter = SESSION.query(Filters).filter( Filters.chat_id == str(chat_id)) if saved_filter: saved_filter.delete() SESSION.commit()
def remove_filter(chat_id, keyword): saved_filter = SESSION.query(Filters).get((str(chat_id), keyword)) if saved_filter: SESSION.delete(saved_filter) SESSION.commit()
def get_all_chats(): try: return SESSION.query(ChatbotChats.chat_id).all() finally: SESSION.close()
def add_nsfwatch(chat_id: str): nsfws = Nsfwatch(str(chat_id)) SESSION.add(nsfws) SESSION.commit()
def get_all_chat_id(): stark = SESSION.query(Nightmode).all() SESSION.close() return stark
def get_all_nsfw_enabled_chat(): stark = SESSION.query(Nsfwatch).all() SESSION.close() return stark
def rmnsfwatch(chat_id: str): nsfwm = SESSION.query(Nsfwatch).get(str(chat_id)) if nsfwm: SESSION.delete(nsfwm) SESSION.commit()
def add_nightmode(chat_id: str): nightmoddy = Nightmode(str(chat_id)) SESSION.add(nightmoddy) SESSION.commit()
def rmnightmode(chat_id: str): rmnightmoddy = SESSION.query(Nightmode).get(str(chat_id)) if rmnightmoddy: SESSION.delete(rmnightmoddy) SESSION.commit()
def add_talkmode(chat_id: str): talkmoddy = Talkmode(str(chat_id)) SESSION.add(talkmoddy) SESSION.commit()
def check_afk_status(user_id): try: return SESSION.query(AFK).get(user_id) finally: SESSION.close()
def disapprove(chat_id): rem = SESSION.query(forceSubscribe).get(chat_id) if rem: SESSION.delete(rem) SESSION.commit()