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_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 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 unblacklist_user(user_id): with BLACKLIST_LOCK: user = SESSION.query(BlacklistUsers).get(str(user_id)) if user: SESSION.delete(user) SESSION.commit() __load_blacklist_userid_list()
def rem_chat(chat_id): with INSERTION_LOCK: chat = SESSION.query(Chats).get(str(chat_id)) if chat: SESSION.delete(chat) SESSION.commit() else: SESSION.close()
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 connect(user_id, chat_id): with CONNECTION_INSERTION_LOCK: prev = SESSION.query(Connection).get((int(user_id))) if prev: SESSION.delete(prev) connect_to_chat = Connection(int(user_id), chat_id) SESSION.add(connect_to_chat) SESSION.commit() return True
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 disconnect(user_id): with CONNECTION_INSERTION_LOCK: disconnect = SESSION.query(Connection).get((int(user_id))) if disconnect: SESSION.delete(disconnect) SESSION.commit() return True else: SESSION.close() return False
def clear_purgefrom(chat_id, message_from): with PURGES_INSERTION_LOCK: note = SESSION.query(Purges).get((str(chat_id), message_from)) if note: SESSION.delete(note) SESSION.commit() return True else: SESSION.close() return False
def disapprove(chat_id, user_id): with APPROVE_INSERTION_LOCK: disapprove_user = SESSION.query(Approvals).get((str(chat_id), user_id)) if disapprove_user: SESSION.delete(disapprove_user) SESSION.commit() return True else: SESSION.close() return False
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 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 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 del_user(user_id): with INSERTION_LOCK: curr = SESSION.query(Users).get(user_id) if curr: SESSION.delete(curr) SESSION.commit() return True ChatMembers.query.filter(ChatMembers.user == user_id).delete() SESSION.commit() SESSION.close() return False
def remove_warn_filter(chat_id, keyword): with WARN_FILTER_INSERTION_LOCK: warn_filt = SESSION.query(WarnFilters).get((str(chat_id), keyword)) if warn_filt: if keyword in WARN_FILTERS.get(str(chat_id), []): # sanity check WARN_FILTERS.get(str(chat_id), []).remove(keyword) SESSION.delete(warn_filt) SESSION.commit() return True SESSION.close() return False
def rm_afk(user_id): with INSERTION_LOCK: curr = SESSION.query(AFK).get(user_id) if curr: if user_id in AFK_USERS: # sanity check del AFK_USERS[user_id] SESSION.delete(curr) SESSION.commit() return True SESSION.close() return False
def enable_command(chat_id, enable): with DISABLE_INSERTION_LOCK: disabled = SESSION.query(Disable).get((str(chat_id), enable)) if disabled: if enable in DISABLED.get(str(chat_id)): # sanity check DISABLED.setdefault(str(chat_id), set()).remove(enable) SESSION.delete(disabled) SESSION.commit() return True SESSION.close() return False
def rm_from_blacklist(chat_id, trigger): with BLACKLIST_FILTER_INSERTION_LOCK: blacklist_filt = SESSION.query(BlackListFilters).get( (str(chat_id), trigger)) if blacklist_filt: if trigger in CHAT_BLACKLISTS.get(str(chat_id), set()): # sanity check CHAT_BLACKLISTS.get(str(chat_id), set()).remove(trigger) SESSION.delete(blacklist_filt) SESSION.commit() return True SESSION.close() return False
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 remove_filter(chat_id, keyword): global CHAT_FILTERS with CUST_FILT_LOCK: filt = SESSION.query(CustomFilters).get((str(chat_id), keyword)) if filt: if keyword in CHAT_FILTERS.get(str(chat_id), []): # Sanity check CHAT_FILTERS.get(str(chat_id), []).remove(keyword) 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(filt) SESSION.commit() return True SESSION.close() return False
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 add_history_conn(user_id, chat_id, chat_name): global HISTORY_CONNECT with CONNECTION_HISTORY_LOCK: conn_time = int(time.time()) if HISTORY_CONNECT.get(int(user_id)): counting = ( SESSION.query(ConnectionHistory.user_id) .filter(ConnectionHistory.user_id == str(user_id)) .count() ) getchat_id = {} for x in HISTORY_CONNECT[int(user_id)]: getchat_id[HISTORY_CONNECT[int(user_id)][x]["chat_id"]] = x if chat_id in getchat_id: todeltime = getchat_id[str(chat_id)] delold = SESSION.query(ConnectionHistory).get( (int(user_id), str(chat_id)) ) if delold: SESSION.delete(delold) HISTORY_CONNECT[int(user_id)].pop(todeltime) elif counting >= 5: todel = list(HISTORY_CONNECT[int(user_id)]) todel.reverse() todel = todel[4:] 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) else: HISTORY_CONNECT[int(user_id)] = {} delold = SESSION.query(ConnectionHistory).get((int(user_id), str(chat_id))) if delold: SESSION.delete(delold) history = ConnectionHistory(int(user_id), str(chat_id), chat_name, conn_time) SESSION.add(history) SESSION.commit() HISTORY_CONNECT[int(user_id)][conn_time] = { "chat_name": chat_name, "chat_id": str(chat_id), }