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 __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 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 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_gdbye_buttons(chat_id): try: return (SESSION.query(GoodbyeButtons).filter( GoodbyeButtons.chat_id == str(chat_id)).order_by( GoodbyeButtons.id).all()) finally: SESSION.close()
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 set_custom_gdbye(chat_id, custom_content_leave, custom_goodbye, goodbye_type, buttons=None): if buttons is None: buttons = [] with INSERTION_LOCK: welcome_settings = SESSION.query(Welcome).get(str(chat_id)) if not welcome_settings: welcome_settings = Welcome(str(chat_id), True) if custom_goodbye or custom_content_leave: welcome_settings.custom_content_leave = custom_content_leave welcome_settings.custom_leave = custom_goodbye welcome_settings.leave_type = goodbye_type.value else: welcome_settings.custom_leave = DEFAULT_GOODBYE welcome_settings.leave_type = Types.TEXT.value SESSION.add(welcome_settings) with LEAVE_BTN_LOCK: prev_buttons = (SESSION.query(GoodbyeButtons).filter( GoodbyeButtons.chat_id == str(chat_id)).all()) for btn in prev_buttons: SESSION.delete(btn) for b_name, url, same_line in buttons: button = GoodbyeButtons(chat_id, b_name, url, same_line) SESSION.add(button) SESSION.commit()
def get_welc_buttons(chat_id): try: return (SESSION.query(WelcomeButtons).filter( WelcomeButtons.chat_id == str(chat_id)).order_by( WelcomeButtons.id).all()) finally: SESSION.close()
def set_custom_welcome(chat_id, custom_content, custom_welcome, welcome_type, buttons=None): if buttons is None: buttons = [] with INSERTION_LOCK: welcome_settings = SESSION.query(Welcome).get(str(chat_id)) if not welcome_settings: welcome_settings = Welcome(str(chat_id), True) if custom_welcome or custom_content: welcome_settings.custom_content = custom_content welcome_settings.custom_welcome = custom_welcome welcome_settings.welcome_type = welcome_type.value else: welcome_settings.custom_welcome = DEFAULT_WELCOME welcome_settings.welcome_type = Types.TEXT.value SESSION.add(welcome_settings) with WELC_BTN_LOCK: prev_buttons = (SESSION.query(WelcomeButtons).filter( WelcomeButtons.chat_id == str(chat_id)).all()) for btn in prev_buttons: SESSION.delete(btn) for b_name, url, same_line in buttons: button = WelcomeButtons(chat_id, b_name, url, same_line) SESSION.add(button) SESSION.commit()
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 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 get_clean_pref(chat_id): welc = SESSION.query(Welcome).get(str(chat_id)) SESSION.close() if welc: return welc.clean_welcome return False
def get_rules(chat_id): rules = SESSION.query(Rules).get(str(chat_id)) ret = "" if rules: ret = rules.rules SESSION.close() return ret
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 get_custom_gdbye(chat_id): welcome_settings = SESSION.query(Welcome).get(str(chat_id)) ret = DEFAULT_GOODBYE if welcome_settings and welcome_settings.custom_leave: ret = welcome_settings.custom_leave SESSION.close() return ret
def get_custom_welcome(chat_id): welcome_settings = SESSION.query(Welcome).get(str(chat_id)) ret = DEFAULT_WELCOME if welcome_settings and welcome_settings.custom_welcome: ret = welcome_settings.custom_welcome SESSION.close() return ret
def clean_service(chat_id: Union[str, int]) -> bool: try: chat_setting = SESSION.query(CleanServiceSetting).get(str(chat_id)) if chat_setting: return chat_setting.clean_service return False finally: SESSION.close()
def get_user(user_id): user = SESSION.query(LastFMUsers).get(str(user_id)) rep = "" if user: rep = str(user.username) SESSION.close() return rep
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 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 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 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 command_reaction(chat_id: Union[str, int]) -> bool: try: chat_setting = SESSION.query(CommandReactionChatSettings).get( str(chat_id)) if chat_setting: return chat_setting.comm_reaction return False finally: SESSION.close()
def allow_connect_to_chat(chat_id: Union[str, int]) -> bool: try: chat_setting = SESSION.query(ChatAccessConnectionSettings).get( str(chat_id)) if chat_setting: return chat_setting.allow_connect_to_chat return False finally: SESSION.close()
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 __load_gbanned_userid_list(): global GBANNED_LIST try: GBANNED_LIST = { x.user_id for x in SESSION.query(GloballyBannedUsers).all() } 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 __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()