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 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(CommandReactionChatSettings).filter( CommandReactionChatSettings.chat_id == str(old_chat_id)).all() for note in chat_notes: note.chat_id = str(new_chat_id) SESSION.commit()
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 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 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_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 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_clean_pref(chat_id): welc = SESSION.query(Welcome).get(str(chat_id)) SESSION.close() if welc: return welc.clean_welcome return False
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 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_welc_mutes_pref(chat_id): welcomemutes = SESSION.query(WelcomeMute).get(str(chat_id)) SESSION.close() if welcomemutes: return welcomemutes.welcomemutes return False
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 welcome_mutes(chat_id): try: welcomemutes = SESSION.query(WelcomeMute).get(str(chat_id)) if welcomemutes: return welcomemutes.welcomemutes return False 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.custom_content, welc.welcome_type else: # Welcome by default. return True, DEFAULT_WELCOME, None, Types.TEXT
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 __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_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 __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_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 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_clean_welcome(chat_id, clean_welcome): with INSERTION_LOCK: curr = SESSION.query(Welcome).get(str(chat_id)) if not curr: curr = Welcome(str(chat_id)) curr.clean_welcome = int(clean_welcome) SESSION.add(curr) SESSION.commit()
def set_gdbye_preference(chat_id, should_goodbye): with INSERTION_LOCK: curr = SESSION.query(Welcome).get(str(chat_id)) if not curr: curr = Welcome(str(chat_id), should_goodbye=should_goodbye) else: curr.should_goodbye = should_goodbye SESSION.add(curr) SESSION.commit()
def disable_antispam(chat_id): with ASPAM_SETTING_LOCK: chat = SESSION.query(AntispamSettings).get(str(chat_id)) if not chat: chat = AntispamSettings(chat_id, False) chat.setting = False SESSION.add(chat) SESSION.commit() GBANSTAT_LIST.add(str(chat_id))
def set_command_reaction(chat_id: Union[int, str], setting: bool): with CHAT_LOCK: chat_setting = SESSION.query(CommandReactionChatSettings).get( str(chat_id)) if not chat_setting: chat_setting = CommandReactionChatSettings(chat_id) chat_setting.comm_reaction = setting SESSION.add(chat_setting) SESSION.commit()
def set_feds_setting(user_id: int, setting: bool): with FEDS_SETTINGS_LOCK: global FEDERATION_NOTIFICATION user_setting = SESSION.query(FedsUserSettings).get(user_id) if not user_setting: user_setting = FedsUserSettings(user_id) user_setting.should_report = setting FEDERATION_NOTIFICATION[str(user_id)] = setting SESSION.add(user_setting) SESSION.commit()
def enable_antispam(chat_id): with ASPAM_SETTING_LOCK: chat = SESSION.query(AntispamSettings).get(str(chat_id)) if not chat: chat = AntispamSettings(chat_id, True) chat.setting = True SESSION.add(chat) SESSION.commit() if str(chat_id) in GBANSTAT_LIST: GBANSTAT_LIST.remove(str(chat_id))
def migrate_chat(old_chat_id, new_chat_id): with DISABLE_INSERTION_LOCK: chats = SESSION.query(Disable).filter( Disable.chat_id == str(old_chat_id)).all() for chat in chats: chat.chat_id = str(new_chat_id) SESSION.add(chat) if str(old_chat_id) in DISABLED: DISABLED[str(new_chat_id)] = DISABLED.get(str(old_chat_id), set()) SESSION.commit()
def chat_join_fed(fed_id, chat_id): with FEDS_LOCK: global FEDERATION_CHATS, FEDERATION_CHATS_BYID r = ChatF(chat_id, fed_id) SESSION.add(r) FEDERATION_CHATS[str(chat_id)] = {'fid': fed_id} checkid = FEDERATION_CHATS_BYID.get(fed_id) if checkid is None: FEDERATION_CHATS_BYID[fed_id] = [] FEDERATION_CHATS_BYID[fed_id].append(str(chat_id)) SESSION.commit() return r