def get_all_echos(): try: return SESSION.query(ECHOSQL).all() except BaseException: return None finally: SESSION.close()
def is_gbanned(chat_id): try: return SESSION.query(GBan).filter(GBan.chat_id == str(chat_id)).one() except BaseException: return None finally: SESSION.close()
def is_gmuted(sender_id): try: return SESSION.query(GMute).all() except BaseException: return None finally: SESSION.close()
def addgvar(variable, value): if SESSION.query(Globals).filter( Globals.variable == str(variable)).one_or_none(): delgvar(variable) adder = Globals(str(variable), value) SESSION.add(adder) SESSION.commit()
def get_s(user_id, chat_id): try: return SESSION.query(LydiaAI).get((user_id, chat_id)) except BaseException: return None finally: SESSION.close()
def get_all_s(): try: return SESSION.query(LydiaAI).all() except BaseException: return None finally: SESSION.close()
def is_folder(folder_id): try: return SESSION.query(Gdrive).filter(Gdrive.cat == str(folder_id)) except BaseException: return None finally: SESSION.close()
def is_echo(user_id, chat_id): try: return SESSION.query(ECHOSQL).get((str(user_id), str(chat_id))) except BaseException: return None finally: SESSION.close()
def get_parent_id(): try: return SESSION.query(Gdrive).all() except BaseException: return None finally: SESSION.close()
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() BLACKLIST_SQL_.CHAT_BLACKLISTS.setdefault(str(chat_id), set()).add(trigger)
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 gvarstatus(variable): try: return (SESSION.query(Globals).filter( Globals.variable == str(variable)).first().value) except BaseException: return None finally: 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 get_warn_setting(chat_id): try: setting = SESSION.query(WarnSettings).get(str(chat_id)) if setting: return setting.warn_limit, setting.soft_warn return 3, False finally: SESSION.close()
def is_approved(chat_id): try: return SESSION.query(NOLogPMs).filter( NOLogPMs.chat_id == chat_id).one() except BaseException: return None finally: SESSION.close()
def is_approved(chat_id): try: return SESSION.query(PMPermit).filter( PMPermit.chat_id == str(chat_id)).one() except BaseException: return None finally: SESSION.close()
def add_s(user_id, chat_id, session_id, session_expires): adder = SESSION.query(LydiaAI).get((user_id, chat_id)) if adder: adder.session_id = session_id adder.session_expires = session_expires else: adder = LydiaAI(user_id, chat_id, session_id, session_expires) SESSION.add(adder) SESSION.commit()
def __load_flood_settings(): try: all_chats = SESSION.query(FloodControl).all() ANTIFLOOD_SQL_.CHAT_FLOOD = { chat.chat_id: (None, DEF_COUNT, chat.limit) for chat in all_chats } finally: SESSION.close() return ANTIFLOOD_SQL_.CHAT_FLOOD
def get_warns(user_id, chat_id): try: user = SESSION.query(Warns).get((user_id, str(chat_id))) if not user: return None reasons = user.reasons num = user.num_warns return num, reasons finally: SESSION.close()
def migrate_chat(old_chat_id, new_chat_id): with INSERTION_LOCK: flood = SESSION.query(FloodControl).get(str(old_chat_id)) if flood: ANTIFLOOD_SQL_.CHAT_FLOOD[str( new_chat_id)] = ANTIFLOOD_SQL_.CHAT_FLOOD.get( str(old_chat_id), DEF_OBJ) flood.chat_id = str(new_chat_id) SESSION.commit() SESSION.close()
def init_locks(chat_id, reset=False): curr_restr = SESSION.query(Locks).get(str(chat_id)) if reset: SESSION.delete(curr_restr) SESSION.flush() restr = Locks(str(chat_id)) SESSION.add(restr) SESSION.commit() return restr
def warn_user(user_id, chat_id, reason=None): with WARN_INSERTION_LOCK: warned_user = SESSION.query(Warns).get((user_id, str(chat_id))) if not warned_user: warned_user = Warns(user_id, str(chat_id)) warned_user.num_warns += 1 if reason: warned_user.reasons = warned_user.reasons + "\r\n\r\n" + reason reasons = warned_user.reasons num = warned_user.num_warns SESSION.add(warned_user) SESSION.commit() return num, reasons
def set_flood(chat_id, amount): with INSERTION_LOCK: flood = SESSION.query(FloodControl).get(str(chat_id)) if not flood: flood = FloodControl(str(chat_id)) flood.user_id = None flood.limit = amount ANTIFLOOD_SQL_.CHAT_FLOOD[str(chat_id)] = (None, DEF_COUNT, amount) SESSION.add(flood) SESSION.commit()
def is_locked(chat_id, lock_type): curr_perm = SESSION.query(Locks).get(str(chat_id)) SESSION.close() if not curr_perm: return False if lock_type == "bots": return curr_perm.bots if lock_type == "commands": return curr_perm.commands if lock_type == "email": return curr_perm.email if lock_type == "forward": return curr_perm.forward if lock_type == "url": return curr_perm.url
def update_lock(chat_id, lock_type, locked): curr_perm = SESSION.query(Locks).get(str(chat_id)) if not curr_perm: curr_perm = init_locks(chat_id) if lock_type == "bots": curr_perm.bots = locked elif lock_type == "commands": curr_perm.commands = locked elif lock_type == "email": curr_perm.email = locked elif lock_type == "forward": curr_perm.forward = locked elif lock_type == "url": curr_perm.url = locked SESSION.add(curr_perm) SESSION.commit()
def __load_chat_blacklists(): try: chats = SESSION.query(BlackListFilters.chat_id).distinct().all() for (chat_id, ) in chats: # remove tuple by ( ,) BLACKLIST_SQL_.CHAT_BLACKLISTS[chat_id] = [] all_filters = SESSION.query(BlackListFilters).all() for x in all_filters: BLACKLIST_SQL_.CHAT_BLACKLISTS[x.chat_id] += [x.trigger] BLACKLIST_SQL_.CHAT_BLACKLISTS = { x: set(y) for x, y in BLACKLIST_SQL_.CHAT_BLACKLISTS.items() } finally: SESSION.close()
def check_data_base_heal_th(): # https://stackoverflow.com/a/41961968 is_database_working = False output = "No Database is set" if not Config.DB_URI: return is_database_working, output from ruserbot.plugins.sql_helper import SESSION try: # to check database we will execute raw query SESSION.execute("SELECT 1") except Exception as e: output = f"❌ {str(e)}" is_database_working = False else: output = "Functioning Normally" is_database_working = True return is_database_working, output
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 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 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 BLACKLIST_SQL_.CHAT_BLACKLISTS.get( str(chat_id), set()): # sanity check BLACKLIST_SQL_.CHAT_BLACKLISTS.get(str(chat_id), set()).remove(trigger) SESSION.delete(blacklist_filt) SESSION.commit() return True SESSION.close() return False