def is_locked(chat_id, lock_type): curr_perm = SESSION.query(Permissions).get(str(chat_id)) SESSION.close() if not curr_perm: return False elif lock_type == "sticker": return curr_perm.sticker elif lock_type == "photo": return curr_perm.photo elif lock_type == "audio": return curr_perm.audio elif lock_type == "voice": return curr_perm.voice elif lock_type == "contact": return curr_perm.contact elif lock_type == "video": return curr_perm.video elif lock_type == "videonote": return curr_perm.videonote elif lock_type == "document": return curr_perm.document elif lock_type == "gif": return curr_perm.gif elif lock_type == "url": return curr_perm.url elif lock_type == "bots": return curr_perm.bots elif lock_type == "forward": return curr_perm.forward elif lock_type == "game": return curr_perm.game elif lock_type == "location": return curr_perm.location
def get_gp_exit_pref(chat_id): getsql = SESSION.query(Welcome).get(str(chat_id)) SESSION.close() if getsql: return getsql.should_goodbye else: # Welcome by default. return True
def __load_disabled_commands(): global DISABLED try: all_chats = SESSION.query(Disable).all() for chat in all_chats: DISABLED.setdefault(chat.chat_id, set()).add(chat.command) finally: SESSION.close()
def del_user(user_id): with INSERTION_LOCK: curr = SESSION.query(Users).get(user_id) if curr: SESSION.delete(curr) SESSION.commit() return True ChatMembers.query.filter(ChatMembers.user == user_id).delete() SESSION.commit() SESSION.close() return False
def disable_command(chat_id, disable): with DISABLE_INSERTION_LOCK: disabled = SESSION.query(Disable).get((str(chat_id), disable)) if not disabled: DISABLED.setdefault(str(chat_id), set()).add(disable) disabled = Disable(str(chat_id), disable) SESSION.add(disabled) SESSION.commit() return True SESSION.close() return False
def enable_command(chat_id, enable): with DISABLE_INSERTION_LOCK: disabled = SESSION.query(Disable).get((str(chat_id), enable)) if disabled: if enable in DISABLED.get(str(chat_id)): # sanity check DISABLED.setdefault(str(chat_id), set()).remove(enable) SESSION.delete(disabled) SESSION.commit() return True SESSION.close() return False
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: if trigger in CHAT_BLACKLISTS.get(str(chat_id), set()): # sanity check CHAT_BLACKLISTS.get(str(chat_id), set()).remove(trigger) SESSION.delete(blacklist_filt) SESSION.commit() return True SESSION.close() return False
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 is_restr_locked(chat_id, lock_type): curr_restr = SESSION.query(Restrictions).get(str(chat_id)) SESSION.close() if not curr_restr: return False if lock_type == "messages": return curr_restr.messages elif lock_type == "media": return curr_restr.media elif lock_type == "other": return curr_restr.other elif lock_type == "previews": return curr_restr.preview elif lock_type == "all": return curr_restr.messages and curr_restr.media and curr_restr.other and curr_restr.preview
def num_blacklist_filters(): try: return SESSION.query(BlackListFilters).count() finally: SESSION.close()
def num_blacklist_chat_filters(chat_id): try: return SESSION.query(BlackListFilters.chat_id).filter( BlackListFilters.chat_id == str(chat_id)).count() finally: SESSION.close()
def get_name_by_userid(user_id): try: return SESSION.query(Users).get(Users.user_id == int(user_id)).first() finally: SESSION.close()
def get_userid_by_name(username): try: return SESSION.query(Users).filter( func.lower(Users.username) == username.lower()).all() finally: SESSION.close()
def num_chats(): try: return SESSION.query(func.count(distinct(Disable.chat_id))).scalar() finally: SESSION.close()
def num_disabled(): try: return SESSION.query(Disable).count() finally: SESSION.close()
def get_all_chats(): try: return SESSION.query(Chats).all() finally: SESSION.close()
def get_chat_members(chat_id): try: return SESSION.query(ChatMembers).filter( ChatMembers.chat == str(chat_id)).all() finally: SESSION.close()
def get_restr(chat_id): try: return SESSION.query(Restrictions).get(str(chat_id)) finally: SESSION.close()
def get_user_num_chats(user_id): try: return SESSION.query(ChatMembers).filter( ChatMembers.user == int(user_id)).count() finally: SESSION.close()
def num_blacklist_filter_chats(): try: return SESSION.query(func.count(distinct( BlackListFilters.chat_id))).scalar() finally: SESSION.close()
def num_chats(): try: return SESSION.query(Chats).count() finally: SESSION.close()
def num_users(): try: return SESSION.query(Users).count() finally: SESSION.close()
def get_locks(chat_id): try: return SESSION.query(Permissions).get(str(chat_id)) finally: SESSION.close()
def get_gp_exit_buttons(chat_id): try: return SESSION.query(GoodbyeButtons).filter(GoodbyeButtons.chat_id == str(chat_id)).order_by( GoodbyeButtons.id).all() finally: SESSION.close()