def set_user(user_id, username): with INSERTION_LOCK: user = SESSION.query(LastFMUsers).get(str(user_id)) if not user: user = LastFMUsers(str(user_id), str(username)) else: user.username = str(username) SESSION.add(user) 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 disapprove(chat_id, user_id): with APPROVE_INSERTION_LOCK: note = SESSION.query(Approvals).get((str(chat_id), user_id)) if note: SESSION.delete(note) SESSION.commit() return True else: SESSION.close() return False
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 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 get_ses(chat_id): autochat = SESSION.query(ChatbotChats).get(str(chat_id)) sesh = "" exp = "" if autochat: sesh = str(autochat.ses_id) exp = str(autochat.expires) SESSION.close() return sesh, exp
def __load_afk_users(): global AFK_USERS try: all_afk = SESSION.query(AFK).all() AFK_USERS = { user.user_id: user.reason for user in all_afk if user.is_afk } finally: SESSION.close()
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 reset_warns(user_id, chat_id): with WARN_INSERTION_LOCK: warned_user = SESSION.query(Warns).get((user_id, str(chat_id))) if warned_user: warned_user.num_warns = 0 warned_user.reasons = [] SESSION.add(warned_user) SESSION.commit() 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 get_warns(user_id, chat_id): try: user = SESSION.query(Warns).get((user_id, str(chat_id))) if not user: return None reasons = user.reasons num = user.num_warns return num, reasons finally: SESSION.close()
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_warn_limit(chat_id, warn_limit): with WARN_SETTINGS_LOCK: curr_setting = SESSION.query(WarnSettings).get(str(chat_id)) if not curr_setting: curr_setting = WarnSettings(chat_id, warn_limit=warn_limit) curr_setting.warn_limit = warn_limit SESSION.add(curr_setting) SESSION.commit()
def __load_flood_settings(): global CHAT_FLOOD try: all_chats = SESSION.query(FloodControl).all() CHAT_FLOOD = { chat.chat_id: (None, DEF_COUNT, chat.limit) for chat in all_chats } finally: SESSION.close()
def get_flood_setting(chat_id): try: setting = SESSION.query(FloodSettings).get(str(chat_id)) if setting: return setting.flood_type, setting.value else: return 1, "0" finally: SESSION.close()
def migrate_chat(old_chat_id, new_chat_id): with INSERTION_FLOOD_LOCK: flood = SESSION.query(FloodControl).get(str(old_chat_id)) if flood: CHAT_FLOOD[str(new_chat_id)] = CHAT_FLOOD.get( str(old_chat_id), DEF_OBJ) flood.chat_id = str(new_chat_id) SESSION.commit() SESSION.close()
def set_warn_strength(chat_id, soft_warn): with WARN_SETTINGS_LOCK: curr_setting = SESSION.query(WarnSettings).get(str(chat_id)) if not curr_setting: curr_setting = WarnSettings(chat_id, soft_warn=soft_warn) curr_setting.soft_warn = soft_warn SESSION.add(curr_setting) SESSION.commit()
def get_warn_setting(chat_id): try: setting = SESSION.query(WarnSettings).get(str(chat_id)) if setting: return setting.warn_limit, setting.soft_warn else: return 3, False finally: SESSION.close()
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 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 INSERTION_LOCK: chat = SESSION.query(Chats).get(str(old_chat_id)) if chat: chat.chat_id = str(new_chat_id) SESSION.add(chat) SESSION.flush() chat_members = ( SESSION.query(ChatMembers) .filter(ChatMembers.chat == str(old_chat_id)) .all() ) for member in chat_members: member.chat = str(new_chat_id) SESSION.add(member) SESSION.commit()
def __load_APPROVE_users(): global APPROVED_USERS try: all_APPROVE = SESSION.query(APPROVE).all() APPROVED_USERS = { user.user_id + " " + user.chat_id for user in all_APPROVE if user.is_approved } finally: SESSION.close()
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 rm_APPROVE(user_id, chat_id): with INSERTION_LOCK: curr = SESSION.query(APPROVE).get(user_id, chat_id) if curr: if user_id and chat_id in APPROVED_USERS: del APPROVED_USERS[user_id, chat_id] SESSION.delete(curr) SESSION.commit() return True SESSION.close() return False
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 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 toggle_APPROVE(user_id, chat_id): with INSERTION_LOCK: curr = SESSION.query(APPROVE).get(user_id, chat_id) if not curr: curr = APPROVE(user_id, chat_id) elif curr.is_approved: curr.is_approved = False elif not curr.is_approved: curr.is_approved = True SESSION.add(curr) 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()