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 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 __migrate_filters(): try: all_filters = SESSION.query(CustomFilters).distinct().all() for x in all_filters: if x.is_document: file_type = Types.DOCUMENT elif x.is_image: file_type = Types.PHOTO elif x.is_video: file_type = Types.VIDEO elif x.is_sticker: file_type = Types.STICKER elif x.is_audio: file_type = Types.AUDIO elif x.is_voice: file_type = Types.VOICE else: file_type = Types.TEXT print(str(x.chat_id), x.keyword, x.reply, file_type.value) if file_type == Types.TEXT: filt = CustomFilters(str(x.chat_id), x.keyword, x.reply, file_type.value, None) else: filt = CustomFilters(str(x.chat_id), x.keyword, None, file_type.value, x.reply) SESSION.add(filt) SESSION.commit() finally: SESSION.close()
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 add_channel(chat_id, channel): adder = SESSION.query(forceSubscribe).get(chat_id) if adder: adder.channel = channel else: adder = forceSubscribe(chat_id, channel) SESSION.add(adder) SESSION.commit()
def set_welcome_mutes(chat_id, welcomemutes): with WM_LOCK: prev = SESSION.query(WelcomeMute).get((str(chat_id))) if prev: SESSION.delete(prev) welcome_m = WelcomeMute(str(chat_id), welcomemutes) SESSION.add(welcome_m) SESSION.commit()
def migrate_chat(old_chat_id, new_chat_id): with GBAN_SETTING_LOCK: chat = SESSION.query(GbanSettings).get(str(old_chat_id)) if chat: chat.chat_id = new_chat_id SESSION.add(chat) SESSION.commit()
def init_restrictions(chat_id, reset=False): curr_restr = SESSION.query(Restrictions).get(str(chat_id)) if reset: SESSION.delete(curr_restr) SESSION.flush() restr = Restrictions(str(chat_id)) SESSION.add(restr) SESSION.commit() return restr
def init_permissions(chat_id, reset=False): curr_perm = SESSION.query(Permissions).get(str(chat_id)) if reset: SESSION.delete(curr_perm) SESSION.flush() perm = Permissions(str(chat_id)) SESSION.add(perm) SESSION.commit() return perm
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_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_chat_setting(chat_id: Union[int, str], setting: bool): with CHAT_LOCK: chat_setting = SESSION.query(AntiArabicChatSettings).get(str(chat_id)) if not chat_setting: chat_setting = AntiArabicChatSettings(chat_id) chat_setting.antiarabic = setting SESSION.add(chat_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_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 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 disable_gbans(chat_id): with GBAN_SETTING_LOCK: chat = SESSION.query(GbanSettings).get(str(chat_id)) if not chat: chat = GbanSettings(chat_id, False) chat.setting = False SESSION.add(chat) SESSION.commit() GBANSTAT_LIST.add(str(chat_id))
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()
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 migrate_chat(old_chat_id, new_chat_id): with LOGS_INSERTION_LOCK: chat = SESSION.query(GroupLogs).get(str(old_chat_id)) if chat: chat.chat_id = str(new_chat_id) SESSION.add(chat) if str(old_chat_id) in CHANNELS: CHANNELS[str(new_chat_id)] = CHANNELS.get(str(old_chat_id)) SESSION.commit()
def set_chat_log_channel(chat_id, log_channel): with LOGS_INSERTION_LOCK: res = SESSION.query(GroupLogs).get(str(chat_id)) if res: res.log_channel = log_channel else: res = GroupLogs(chat_id, log_channel) SESSION.add(res) CHANNELS[str(chat_id)] = log_channel 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_gbans(chat_id): with GBAN_SETTING_LOCK: chat = SESSION.query(GbanSettings).get(str(chat_id)) if not chat: chat = GbanSettings(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 set_ses(chat_id, ses_id, expires): with INSERTION_LOCK: autochat = SESSION.query(ChatbotChats).get(str(chat_id)) if not autochat: autochat = ChatbotChats(str(chat_id), str(ses_id), str(expires)) else: autochat.ses_id = str(ses_id) autochat.expires = str(expires) SESSION.add(autochat) SESSION.commit()
def blacklist_user(user_id, reason=None): with BLACKLIST_LOCK: user = SESSION.query(BlacklistUsers).get(str(user_id)) if not user: user = BlacklistUsers(str(user_id), reason) else: user.reason = reason SESSION.add(user) SESSION.commit() __load_blacklist_userid_list()
def toggle_afk(user_id, reason=""): with INSERTION_LOCK: curr = SESSION.query(AFK).get(user_id) if not curr: curr = AFK(user_id, reason, True) elif curr.is_afk: curr.is_afk = False elif not curr.is_afk: curr.is_afk = True SESSION.add(curr) SESSION.commit()
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 set_afk(user_id, reason=""): with INSERTION_LOCK: curr = SESSION.query(AFK).get(user_id) if not curr: curr = AFK(user_id, reason, True) else: curr.is_afk = True AFK_USERS[user_id] = reason SESSION.add(curr) SESSION.commit()
def add_filter( chat_id, keyword, reply, is_sticker=False, is_document=False, is_image=False, is_audio=False, is_voice=False, is_video=False, buttons=None, ): global CHAT_FILTERS if buttons is None: buttons = [] with CUST_FILT_LOCK: prev = SESSION.query(CustomFilters).get((str(chat_id), keyword)) if prev: with BUTTON_LOCK: prev_buttons = (SESSION.query(Buttons).filter( Buttons.chat_id == str(chat_id), Buttons.keyword == keyword).all()) for btn in prev_buttons: SESSION.delete(btn) SESSION.delete(prev) filt = CustomFilters( str(chat_id), keyword, reply, is_sticker, is_document, is_image, is_audio, is_voice, is_video, bool(buttons), ) if keyword not in CHAT_FILTERS.get(str(chat_id), []): CHAT_FILTERS[str(chat_id)] = sorted( CHAT_FILTERS.get(str(chat_id), []) + [keyword], key=lambda x: (-len(x), x), ) SESSION.add(filt) SESSION.commit() for b_name, url, same_line in buttons: add_note_button_to_db(chat_id, keyword, b_name, url, same_line)
def set_flood(chat_id, amount): with INSERTION_FLOOD_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()