def migrate_chat(old_chat_id, new_chat_id): with WARN_INSERTION_LOCK: chat_notes = SESSION.query(Warns).filter( Warns.chat_id == str(old_chat_id)).all() for note in chat_notes: note.chat_id = str(new_chat_id) SESSION.commit() with WARN_FILTER_INSERTION_LOCK: chat_filters = SESSION.query(WarnFilters).filter( WarnFilters.chat_id == str(old_chat_id)).all() for filt in chat_filters: filt.chat_id = str(new_chat_id) SESSION.commit() old_warn_filt = WARN_FILTERS.get(str(old_chat_id)) if old_warn_filt is not None: WARN_FILTERS[str(new_chat_id)] = old_warn_filt del WARN_FILTERS[str(old_chat_id)] with WARN_SETTINGS_LOCK: chat_settings = SESSION.query(WarnSettings).filter( WarnSettings.chat_id == str(old_chat_id)).all() for setting in chat_settings: setting.chat_id = str(new_chat_id) SESSION.commit()
def __load_feds_subscriber(): global FEDS_SUBSCRIBER global MYFEDS_SUBSCRIBER try: feds = SESSION.query(FedSubs.fed_id).distinct().all() for (fed_id,) in feds: # remove tuple by ( ,) FEDS_SUBSCRIBER[fed_id] = [] MYFEDS_SUBSCRIBER[fed_id] = [] all_fedsubs = SESSION.query(FedSubs).all() for x in all_fedsubs: FEDS_SUBSCRIBER[x.fed_id] += [x.fed_subs] try: MYFEDS_SUBSCRIBER[x.fed_subs] += [x.fed_id] except KeyError: getsubs = SESSION.query(FedSubs).get((x.fed_id, x.fed_subs)) if getsubs: SESSION.delete(getsubs) SESSION.commit() FEDS_SUBSCRIBER = {x: set(y) for x, y in FEDS_SUBSCRIBER.items()} MYFEDS_SUBSCRIBER = {x: set(y) for x, y in MYFEDS_SUBSCRIBER.items()} finally: SESSION.close()
def add_to_stickers(chat_id, trigger): with STICKERS_FILTER_INSERTION_LOCK: stickers_filt = StickersFilters(str(chat_id), trigger) SESSION.merge(stickers_filt) # merge to avoid duplicate key issues SESSION.commit() global CHAT_STICKERS if CHAT_STICKERS.get(str(chat_id), set()) == set(): CHAT_STICKERS[str(chat_id)] = {trigger} else: CHAT_STICKERS.get(str(chat_id), set()).add(trigger)
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 stop_chat_logging(chat_id): with LOGS_INSERTION_LOCK: res = SESSION.query(GroupLogs).get(str(chat_id)) if res: if str(chat_id) in CHANNELS: del CHANNELS[str(chat_id)] log_channel = res.log_channel SESSION.delete(res) SESSION.commit() return log_channel
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 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 add_warn_filter(chat_id, keyword, reply): with WARN_FILTER_INSERTION_LOCK: warn_filt = WarnFilters(str(chat_id), keyword, reply) if keyword not in WARN_FILTERS.get(str(chat_id), []): WARN_FILTERS[str(chat_id)] = sorted( WARN_FILTERS.get(str(chat_id), []) + [keyword], key=lambda x: (-len(x), x)) SESSION.merge(warn_filt) # merge to avoid duplicate key issues 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 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() global CHAT_BLACKLISTS if CHAT_BLACKLISTS.get(str(chat_id), set()) == set(): CHAT_BLACKLISTS[str(chat_id)] = {trigger} else: CHAT_BLACKLISTS.get(str(chat_id), set()).add(trigger)
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 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 __load_chat_settings_blacklists(): global CHAT_SETTINGS_BLACKLISTS try: chats_settings = SESSION.query(BlacklistSettings).all() for x in chats_settings: # remove tuple by ( ,) CHAT_SETTINGS_BLACKLISTS[x.chat_id] = { "blacklist_type": x.blacklist_type, "value": x.value, } finally: SESSION.close()
def __load_chat_stickerset_blacklists(): global CHAT_BLSTICK_BLACKLISTS try: chats_settings = SESSION.query(StickerSettings).all() for x in chats_settings: # remove tuple by ( ,) CHAT_BLSTICK_BLACKLISTS[x.chat_id] = { 'blacklist_type': x.blacklist_type, 'value': x.value } finally: SESSION.close()
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 clear_history_conn(user_id): global HISTORY_CONNECT todel = list(HISTORY_CONNECT[int(user_id)]) for x in todel: chat_old = HISTORY_CONNECT[int(user_id)][x]["chat_id"] delold = SESSION.query(ConnectionHistory).get( (int(user_id), str(chat_old))) if delold: SESSION.delete(delold) HISTORY_CONNECT[int(user_id)].pop(x) SESSION.commit() return True
def update_gban_reason(user_id, name, reason=None): with GBANNED_USERS_LOCK: user = SESSION.query(GloballyBannedUsers).get(user_id) if not user: return None old_reason = user.reason user.name = name user.reason = reason SESSION.merge(user) SESSION.commit() return old_reason
def gban_user(user_id, name, reason=None): with GBANNED_USERS_LOCK: user = SESSION.query(GloballyBannedUsers).get(user_id) if not user: user = GloballyBannedUsers(user_id, name, reason) else: user.name = name user.reason = reason SESSION.merge(user) SESSION.commit() __load_gbanned_userid_list()
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 chat_join_fed(fed_id, chat_name, chat_id): with FEDS_LOCK: global FEDERATION_CHATS, FEDERATION_CHATS_BYID r = ChatF(chat_id, chat_name, fed_id) SESSION.add(r) FEDERATION_CHATS[str(chat_id)] = {'chat_name': chat_name, '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
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 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()
def migrate_chat(old_chat_id, new_chat_id): with NOTES_INSERTION_LOCK: chat_notes = SESSION.query(Notes).filter( Notes.chat_id == str(old_chat_id)).all() for note in chat_notes: note.chat_id = str(new_chat_id) with BUTTONS_INSERTION_LOCK: chat_buttons = SESSION.query(Buttons).filter( Buttons.chat_id == str(old_chat_id)).all() for btn in chat_buttons: btn.chat_id = str(new_chat_id) SESSION.commit()
def set_human_checks(user_id, chat_id): with INSERTION_LOCK: human_check = SESSION.query(WelcomeMuteUsers).get( (user_id, str(chat_id))) if not human_check: human_check = WelcomeMuteUsers(user_id, str(chat_id), True) else: human_check.human_check = True SESSION.add(human_check) SESSION.commit() return human_check
def __load_chat_blacklists(): global CHAT_BLACKLISTS try: chats = SESSION.query(BlackListFilters.chat_id).distinct().all() for (chat_id, ) in chats: # remove tuple by ( ,) CHAT_BLACKLISTS[chat_id] = [] all_filters = SESSION.query(BlackListFilters).all() for x in all_filters: CHAT_BLACKLISTS[x.chat_id] += [x.trigger] CHAT_BLACKLISTS = {x: set(y) for x, y in CHAT_BLACKLISTS.items()} finally: SESSION.close()
def __load_CHAT_STICKERS(): global CHAT_STICKERS try: chats = SESSION.query(StickersFilters.chat_id).distinct().all() for (chat_id, ) in chats: # remove tuple by ( ,) CHAT_STICKERS[chat_id] = [] all_filters = SESSION.query(StickersFilters).all() for x in all_filters: CHAT_STICKERS[x.chat_id] += [x.trigger] CHAT_STICKERS = {x: set(y) for x, y in CHAT_STICKERS.items()} finally: SESSION.close()
def __load_user_history(): global HISTORY_CONNECT try: qall = SESSION.query(ConnectionHistory).all() HISTORY_CONNECT = {} for x in qall: check = HISTORY_CONNECT.get(x.user_id) if check is None: HISTORY_CONNECT[x.user_id] = {} HISTORY_CONNECT[x.user_id][x.conn_time] = { "chat_name": x.chat_name, "chat_id": x.chat_id, } finally: SESSION.close()
def subs_fed(fed_id, my_fed): check = get_spec_subs(fed_id, my_fed) if check: return False with FEDS_SUBSCRIBER_LOCK: subsfed = FedSubs(fed_id, my_fed) SESSION.merge(subsfed) # merge to avoid duplicate key issues SESSION.commit() global FEDS_SUBSCRIBER if FEDS_SUBSCRIBER.get(fed_id, set()) == set(): FEDS_SUBSCRIBER[fed_id] = {my_fed} else: FEDS_SUBSCRIBER.get(fed_id, set()).add(my_fed) return True