def set_blacklist_strength(chat_id, blacklist_type, value): # for blacklist_type # 0 = nothing # 1 = delete # 2 = warn # 3 = mute # 4 = kick # 5 = ban # 6 = tban # 7 = tmute with BLACKLIST_SETTINGS_INSERTION_LOCK: global CHAT_SETTINGS_BLACKLISTS curr_setting = SESSION.query(BlacklistSettings).get(str(chat_id)) if not curr_setting: curr_setting = BlacklistSettings( chat_id, blacklist_type=int(blacklist_type), value=value) curr_setting.blacklist_type = int(blacklist_type) curr_setting.value = str(value) CHAT_SETTINGS_BLACKLISTS[str(chat_id)] = { "blacklist_type": int(blacklist_type), "value": value, } SESSION.add(curr_setting) SESSION.commit()
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 rm_note(chat_id, note_name): with NOTES_INSERTION_LOCK: note = ( SESSION.query(Notes) .filter(func.lower(Notes.name) == note_name, Notes.chat_id == str(chat_id)) .first() ) if note: with BUTTONS_INSERTION_LOCK: buttons = ( SESSION.query(Buttons) .filter( Buttons.chat_id == str(chat_id), Buttons.note_name == note_name ) .all() ) for btn in buttons: SESSION.delete(btn) SESSION.delete(note) SESSION.commit() return True else: SESSION.close() return False
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_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 set_custom_gdbye(chat_id, 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: 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 migrate_chat(old_chat_id, new_chat_id): with INSERTION_LOCK: chat = SESSION.query(Welcome).get(str(old_chat_id)) if chat: chat.chat_id = str(new_chat_id) with WELC_BTN_LOCK: chat_buttons = ( SESSION.query(WelcomeButtons) .filter(WelcomeButtons.chat_id == str(old_chat_id)) .all() ) for btn in chat_buttons: btn.chat_id = str(new_chat_id) with LEAVE_BTN_LOCK: chat_buttons = ( SESSION.query(GoodbyeButtons) .filter(GoodbyeButtons.chat_id == str(old_chat_id)) .all() ) for btn in chat_buttons: btn.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 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 __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 get_welc_pref(chat_id): welc = SESSION.query(Welcome).get(str(chat_id)) SESSION.close() if welc: return welc.should_welcome, welc.custom_welcome, welc.welcome_type else: # Welcome by default. return True, DEFAULT_WELCOME, Types.TEXT
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 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 get_welc_mutes_pref(chat_id): welcomemutes = SESSION.query(WelcomeMute).get(str(chat_id)) SESSION.close() if welcomemutes: return welcomemutes.welcomemutes return False
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_gdbye_pref(chat_id): welc = SESSION.query(Welcome).get(str(chat_id)) SESSION.close() if welc: return welc.should_goodbye, welc.custom_leave, welc.leave_type else: # Welcome by default. return True, DEFAULT_GOODBYE, Types.TEXT
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 welcome_mutes(chat_id): try: welcomemutes = SESSION.query(WelcomeMute).get(str(chat_id)) if welcomemutes: return welcomemutes.welcomemutes return False 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 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 __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_clean_service(chat_id: Union[int, str], setting: bool): with CS_LOCK: chat_setting = SESSION.query(CleanServiceSetting).get(str(chat_id)) if not chat_setting: chat_setting = CleanServiceSetting(chat_id) chat_setting.clean_service = setting SESSION.add(chat_setting) SESSION.commit()
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 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 get_human_checks(user_id, chat_id): try: human_check = SESSION.query(WelcomeMuteUsers).get((user_id, str(chat_id))) if not human_check: return None human_check = human_check.human_check return human_check finally: SESSION.close()
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_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 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 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()