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 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 rem_chat(chat_id): with INSERTION_LOCK: autochat = SESSION.query(ChatbotChats).get(str(chat_id)) if autochat: SESSION.delete(autochat) SESSION.commit()
def __load_log_channels(): global CHANNELS try: all_chats = SESSION.query(GroupLogs).all() CHANNELS = {chat.chat_id: chat.log_channel for chat in all_chats} finally: SESSION.close()
def migrate_chat(old_chat_id, new_chat_id): with CHAT_LOCK: chat_notes = SESSION.query(ReportingChatSettings).filter( ReportingChatSettings.chat_id == str(old_chat_id)).all() for note in chat_notes: note.chat_id = str(new_chat_id) SESSION.commit()
def migrate_chat(old_chat_id, new_chat_id): with BLACKLIST_FILTER_INSERTION_LOCK: chat_filters = SESSION.query(BlackListFilters).filter( BlackListFilters.chat_id == str(old_chat_id)).all() for filt in chat_filters: filt.chat_id = str(new_chat_id) SESSION.commit()
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() CHAT_BLACKLISTS.setdefault(str(chat_id), set()).add(trigger)
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 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 == "": reason = "No reason given." if reason: if warned_user.reasons is None: warned_user.reasons = [reason] else: warned_user.reasons = warned_user.reasons + [ reason ] # TODO:: double check this Daan: Not really wizardry, it adds a new entry to a list/array which can be done this way, basically append equivalent reasons = warned_user.reasons num = warned_user.num_warns SESSION.add(warned_user) SESSION.commit() return num, reasons
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 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 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 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 ASPAM_SETTING_LOCK: gban = SESSION.query(AntispamSettings).get(str(old_chat_id)) if gban: gban.chat_id = new_chat_id SESSION.add(gban) 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 chat_should_report(chat_id: Union[str, int]) -> bool: try: chat_setting = SESSION.query(ReportingChatSettings).get(str(chat_id)) if chat_setting: return chat_setting.should_report return True finally: SESSION.close()
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 user_should_report(user_id: int) -> bool: try: user_setting = SESSION.query(ReportingUserSettings).get(user_id) if user_setting: return user_setting.should_report return True finally: SESSION.close()
def get_rules(chat_id): rules = SESSION.query(Rules).get(str(chat_id)) ret = "" if rules: ret = rules.rules SESSION.close() return ret
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 __load_gbanned_userid_list(): global GBANNED_LIST try: GBANNED_LIST = { x.user_id for x in SESSION.query(GloballyBannedUsers).all() } finally: SESSION.close()
def __load_gban_stat_list(): global GBANSTAT_LIST try: GBANSTAT_LIST = { x.chat_id for x in SESSION.query(AntispamSettings).all() if not x.setting } finally: SESSION.close()
def is_chat(chat_id): try: chat = SESSION.query(ChatbotChats).get(str(chat_id)) if chat: return True else: return False finally: SESSION.close()
def set_user_setting(user_id: int, setting: bool): with USER_LOCK: user_setting = SESSION.query(ReportingUserSettings).get(user_id) if not user_setting: user_setting = ReportingUserSettings(user_id) user_setting.should_report = setting SESSION.add(user_setting) SESSION.commit()
def set_chat_setting(chat_id: Union[int, str], setting: bool): with CHAT_LOCK: chat_setting = SESSION.query(ReportingChatSettings).get(str(chat_id)) if not chat_setting: chat_setting = ReportingChatSettings(chat_id) chat_setting.should_report = setting SESSION.add(chat_setting) SESSION.commit()
def set_rules(chat_id, rules_text): with INSERTION_LOCK: rules = SESSION.query(Rules).get(str(chat_id)) if not rules: rules = Rules(str(chat_id)) rules.rules = rules_text SESSION.add(rules) SESSION.commit()
def set_user_me_info(user_id, info): with INSERTION_LOCK: userinfo = SESSION.query(UserInfo).get(user_id) if userinfo: userinfo.info = info else: userinfo = UserInfo(user_id, info) SESSION.add(userinfo) SESSION.commit()
def update_url(row_id, new_entry_links): with INSERTION_LOCK: row = SESSION.query(RSS).get(row_id) # set the new old_entry_link with the latest update from the RSS Feed row.old_entry_link = new_entry_links[0] # commit the changes to the DB SESSION.commit()
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 set_user_bio(user_id, bio): with INSERTION_LOCK: userbio = SESSION.query(UserBio).get(user_id) if userbio: userbio.bio = bio else: userbio = UserBio(user_id, bio) SESSION.add(userbio) SESSION.commit()