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_all_snips(): try: return SESSION.query(Snips).all() except: return None finally: SESSION.close()
def get_snips(keyword): try: return SESSION.query(Snips).get(keyword) except: return None finally: SESSION.close()
def is_globelmuted(sender_id): try: return SESSION.query(GLOBELMute).all() except BaseException: return None finally: SESSION.close()
def get_filter(chat_id, keyword): try: return SESSION.query(Filters).get((str(chat_id), keyword)) except: return None finally: SESSION.close()
def is_data_indbs(chat_id: int): try: s__ = SESSION.query(Pa).get(str(chat_id)) if s__: return s__.textto_append finally: SESSION.close()
def is_gmuted(sender_id): try: return SESSION.query(GMute).all() except: return None finally: SESSION.close()
def is_fed_indb(feds): try: return SESSION.query(Fed).filter(Fed.feds == feds).one() except: return None finally: SESSION.close()
def is_footer(chat_id: int): try: s__ = SESSION.query(Pa).get(str(chat_id)) if s__: return s__.append_foot finally: SESSION.close()
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 is_tracker_in_db(amazon_url: str): try: s__ = SESSION.query(Anp).get(str(amazon_url)) if s__: return str(s__.budget) finally: SESSION.close()
def get_current_welcome_settings(chat_id): try: return SESSION.query(Welcome).filter( Welcome.chat_id == str(chat_id)).one() except: return None finally: SESSION.close()
def get_all_filters(chat_id): try: return SESSION.query(Filters).filter( Filters.chat_id == str(chat_id)).all() except: return None finally: SESSION.close()
def is_ping_indb(url): try: return SESSION.query(Serverpinger).filter( Serverpinger.url == url).one() except: return None finally: SESSION.close()
def is_approved(chat_id): try: return SESSION.query(PMPermit).filter( PMPermit.chat_id == str(chat_id)).one() except: return None finally: SESSION.close()
def add_note(chat_id, keyword, reply): adder = SESSION.query(Notes).get((str(chat_id), keyword)) if adder: adder.reply = reply else: adder = Notes(str(chat_id), keyword, reply) SESSION.add(adder) SESSION.commit()
def __load_flood_settings(): global CHAT_FLOOD try: all_chats = SESSION.query(FloodControl).all() CHAT_FLOOD = {chat.chat_id: (None, DEF_COUNT, chat.limit) for chat in all_chats} finally: SESSION.close() return CHAT_FLOOD
def migrate_chat(old_chat_id, new_chat_id): with INSERTION_LOCK: flood = SESSION.query(FloodControl).get(str(old_chat_id)) if flood: CHAT_FLOOD[str(new_chat_id)] = CHAT_FLOOD.get(str(old_chat_id), DEF_OBJ) flood.chat_id = str(new_chat_id) SESSION.commit() SESSION.close()
def __load_chat_warn_filters(): global WARN_FILTERS try: chats = SESSION.query(WarnFilters.chat_id).distinct().all() for (chat_id, ) in chats: # remove tuple by ( ,) WARN_FILTERS[chat_id] = [] all_filters = SESSION.query(WarnFilters).all() for x in all_filters: WARN_FILTERS[x.chat_id] += [x.keyword] WARN_FILTERS = { x: sorted(set(y), key=lambda i: (-len(i), i)) for x, y in WARN_FILTERS.items() } finally: 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 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 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 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 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 get_warn_setting(chat_id): try: setting = SESSION.query(WarnSettings).get(str(chat_id)) if setting: return setting.warn_limit, setting.soft_warn else: return 3, False finally: SESSION.close()
def remove_warn_filter(chat_id, keyword): with WARN_FILTER_INSERTION_LOCK: warn_filt = SESSION.query(WarnFilters).get((str(chat_id), keyword)) if warn_filt: if keyword in WARN_FILTERS.get(str(chat_id), []): # sanity check WARN_FILTERS.get(str(chat_id), []).remove(keyword) SESSION.delete(warn_filt) SESSION.commit() return True SESSION.close() return False
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 CHAT_FLOOD[str(chat_id)] = (None, DEF_COUNT, amount) SESSION.add(flood) SESSION.commit()
def add_snip(keyword, reply, snip_type, media_id, media_access_hash, media_file_reference): adder = SESSION.query(Snips).get(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 = Snips(keyword, reply, snip_type, media_id, media_access_hash, media_file_reference) SESSION.add(adder) SESSION.commit()
def migrate_chat(old_chat_id, new_chat_id): with WARN_INSERTION_LOCK: chat_notes = (SESSION.query(Warns).filter( Warns.chat_id == str(old_chat_id)).all()) for note in chat_notes: note.chat_id = str(new_chat_id) SESSION.commit() with WARN_FILTER_INSERTION_LOCK: chat_filters = (SESSION.query(WarnFilters).filter( WarnFilters.chat_id == str(old_chat_id)).all()) for filt in chat_filters: filt.chat_id = str(new_chat_id) SESSION.commit() WARN_FILTERS[str(new_chat_id)] = WARN_FILTERS[str(old_chat_id)] del WARN_FILTERS[str(old_chat_id)] with WARN_SETTINGS_LOCK: chat_settings = (SESSION.query(WarnSettings).filter( WarnSettings.chat_id == str(old_chat_id)).all()) for setting in chat_settings: setting.chat_id = str(new_chat_id) SESSION.commit()
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