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_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 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_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 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 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 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 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 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 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 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_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 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 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_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 disable_command(chat_id, disable): with DISABLE_INSERTION_LOCK: disabled = SESSION.query(Disable).get((str(chat_id), disable)) if not disabled: DISABLED.setdefault(str(chat_id), set()).add(disable) disabled = Disable(str(chat_id), disable) SESSION.add(disabled) SESSION.commit() return True SESSION.close() return False
def remove_warn(user_id, chat_id): with WARN_INSERTION_LOCK: removed = False warned_user = SESSION.query(Warns).get((user_id, str(chat_id))) if warned_user and warned_user.num_warns > 0: warned_user.num_warns -= 1 warned_user.reasons = warned_user.reasons[:-1] SESSION.add(warned_user) SESSION.commit() removed = True SESSION.close() return removed
def global_ignore_command(command): command = command.lower() with CLEANER_GLOBAL_LOCK: ignored = SESSION.query(CleanerBlueTextGlobal).get(str(command)) if not ignored: GLOBAL_IGNORE_COMMANDS.add(command) ignored = CleanerBlueTextGlobal(str(command)) SESSION.add(ignored) SESSION.commit() return True SESSION.close() return False
def new_fed(owner_id, fed_name, fed_id): with FEDS_LOCK: global FEDERATION_BYOWNER, FEDERATION_BYFEDID, FEDERATION_BYNAME fed = Federations( str(owner_id), fed_name, str(fed_id), 'Rules is not set in this federation.', None, str({ 'owner': str(owner_id), 'members': '[]' })) SESSION.add(fed) SESSION.commit() FEDERATION_BYOWNER[str(owner_id)] = ({ 'fid': str(fed_id), 'fname': fed_name, 'frules': 'Rules is not set in this federation.', 'flog': None, 'fusers': str({ 'owner': str(owner_id), 'members': '[]' }) }) FEDERATION_BYFEDID[str(fed_id)] = ({ 'owner': str(owner_id), 'fname': fed_name, 'frules': 'Rules is not set in this federation.', 'flog': None, 'fusers': str({ 'owner': str(owner_id), 'members': '[]' }) }) FEDERATION_BYNAME[fed_name] = ({ 'fid': str(fed_id), 'owner': str(owner_id), 'frules': 'Rules is not set in this federation.', 'flog': None, 'fusers': str({ 'owner': str(owner_id), 'members': '[]' }) }) return fed
def warn_user(user_id, chat_id, reason=None): with WARN_INSERTION_LOCK: warned_user = SESSION.query(Warns).get((user_id, str(chat_id))) if not warned_user: warned_user = Warns(user_id, str(chat_id)) warned_user.num_warns += 1 if reason: warned_user.reasons = warned_user.reasons + [ reason ] # TODO:: double check this wizardry reasons = warned_user.reasons num = warned_user.num_warns SESSION.add(warned_user) SESSION.commit() return num, reasons
def set_cleanbt(chat_id, is_enable): with CLEANER_CHAT_SETTINGS: curr = SESSION.query(CleanerBlueTextChatSettings).get(str(chat_id)) if not curr: curr = CleanerBlueTextChatSettings(str(chat_id), is_enable) else: curr.is_enabled = is_enable if str(chat_id) not in CLEANER_CHATS: CLEANER_CHATS.setdefault(str(chat_id), { "setting": False, "commands": set() }) CLEANER_CHATS[str(chat_id)]["setting"] = is_enable SESSION.add(curr) SESSION.commit()
def update_lock(chat_id, lock_type, locked): with PERM_LOCK: curr_perm = SESSION.query(Permissions).get(str(chat_id)) if not curr_perm: curr_perm = init_permissions(chat_id) if lock_type == "audio": curr_perm.audio = locked elif lock_type == "voice": curr_perm.voice = locked elif lock_type == "contact": curr_perm.contact = locked elif lock_type == "video": curr_perm.video = locked elif lock_type == "document": curr_perm.document = locked elif lock_type == "photo": curr_perm.photo = locked elif lock_type == "sticker": curr_perm.sticker = locked elif lock_type == "gif": curr_perm.gif = locked elif lock_type == "url": curr_perm.url = locked elif lock_type == "bots": curr_perm.bots = locked elif lock_type == "forward": curr_perm.forward = locked elif lock_type == "game": curr_perm.game = locked elif lock_type == "location": curr_perm.location = locked elif lock_type == "rtl": curr_perm.rtl = locked elif lock_type == "button": curr_perm.button = locked elif lock_type == "egame": curr_perm.egame = locked elif lock_type == "inline": curr_perm.inline = locked SESSION.add(curr_perm) 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), }
def update_restriction(chat_id, restr_type, locked): with RESTR_LOCK: curr_restr = SESSION.query(Restrictions).get(str(chat_id)) if not curr_restr: curr_restr = init_restrictions(chat_id) if restr_type == "messages": curr_restr.messages = locked elif restr_type == "media": curr_restr.media = locked elif restr_type == "other": curr_restr.other = locked elif restr_type == "previews": curr_restr.preview = locked elif restr_type == "all": curr_restr.messages = locked curr_restr.media = locked curr_restr.other = locked curr_restr.preview = locked SESSION.add(curr_restr) SESSION.commit()
def update_user(user_id, username, chat_id=None, chat_name=None): with INSERTION_LOCK: user = SESSION.query(Users).get(user_id) if not user: user = Users(user_id, username) SESSION.add(user) SESSION.flush() else: user.username = username if not chat_id or not chat_name: SESSION.commit() return chat = SESSION.query(Chats).get(str(chat_id)) if not chat: chat = Chats(str(chat_id), chat_name) SESSION.add(chat) SESSION.flush() else: chat.chat_name = chat_name member = SESSION.query(ChatMembers).filter( ChatMembers.chat == chat.chat_id, ChatMembers.user == user.user_id).first() if not member: chat_member = ChatMembers(chat.chat_id, user.user_id) SESSION.add(chat_member) SESSION.commit()
def multi_fban_user(multi_fed_id, multi_user_id, multi_first_name, multi_last_name, multi_user_name, multi_reason): if True: # with FEDS_LOCK: counter = 0 time = 0 for x in range(len(multi_fed_id)): fed_id = multi_fed_id[x] user_id = multi_user_id[x] first_name = multi_first_name[x] last_name = multi_last_name[x] user_name = multi_user_name[x] reason = multi_reason[x] r = SESSION.query(BansF).all() for I in r: if I.fed_id == fed_id: if int(I.user_id) == int(user_id): SESSION.delete(I) r = BansF( str(fed_id), str(user_id), first_name, last_name, user_name, reason, time) SESSION.add(r) counter += 1 if str(str(counter)[-2:]) == "00": print(user_id) print(first_name) print(reason) print(counter) try: SESSION.commit() except: SESSION.rollback() return False finally: SESSION.commit() __load_all_feds_banned() print("Done") return counter
def fban_user(fed_id, user_id, first_name, last_name, user_name, reason, time): with FEDS_LOCK: r = SESSION.query(BansF).all() for I in r: if I.fed_id == fed_id: if int(I.user_id) == int(user_id): SESSION.delete(I) r = BansF( str(fed_id), str(user_id), first_name, last_name, user_name, reason, time) SESSION.add(r) try: SESSION.commit() except: SESSION.rollback() return False finally: SESSION.commit() __load_all_feds_banned() return r