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 == "": reason = "No reason given." if reason: if warned_user.reasons is None: warned_user.reasons = [reason] else: warned_user.reasons = warned_user.reasons + [ reason ] # TODO:: double check this Daan: Not really wizardry, it adds a new entry to a list/array which can be done this way, basically append equivalent reasons = warned_user.reasons num = warned_user.num_warns SESSION.add(warned_user) SESSION.commit() return num, reasons
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 switch_to_locale(chat_id, locale_name): with LOCALES_INSERTION_LOCK: prev = SESSION.query(Locales).get((str(chat_id))) if prev: SESSION.delete(prev) switch_locale = Locales(str(chat_id), locale_name) SESSION.add(switch_locale) SESSION.commit()
def migrate_chat(old_chat_id, new_chat_id): with ASPAM_SETTING_LOCK: gban = SESSION.query(AntispamSettings).get(str(old_chat_id)) if gban: gban.chat_id = new_chat_id SESSION.add(gban) SESSION.commit()
def add_repo_to_db(chat_id, name, value, backoffset): with GIT_LOCK: prev = SESSION.query(GitHub).get((str(chat_id), name)) if prev: SESSION.delete(prev) repo = GitHub(str(chat_id), name, value, backoffset) SESSION.add(repo) 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_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 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_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 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 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 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 disable_antispam(chat_id): with ASPAM_SETTING_LOCK: chat = SESSION.query(AntispamSettings).get(str(chat_id)) if not chat: chat = AntispamSettings(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 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 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 enable_antispam(chat_id): with ASPAM_SETTING_LOCK: chat = SESSION.query(AntispamSettings).get(str(chat_id)) if not chat: chat = AntispamSettings(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_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 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_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 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 chat_join_fed(fed_id, chat_id): with FEDS_LOCK: global FEDERATION_CHATS, FEDERATION_CHATS_BYID r = ChatF(chat_id, fed_id) SESSION.add(r) FEDERATION_CHATS[str(chat_id)] = {'fid': fed_id} checkid = FEDERATION_CHATS_BYID.get(fed_id) if checkid == None: FEDERATION_CHATS_BYID[fed_id] = [] FEDERATION_CHATS_BYID[fed_id].append(str(chat_id)) SESSION.commit() return r
def set_flood(chat_id, amount): with INSERTION_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 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.', 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.', '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.', '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.', 'fusers': str({ 'owner': str(owner_id), 'members': '[]' }) }) return fed
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))) temp_reason = [] if warned_user and warned_user.num_warns > 0: warned_user.num_warns -= 1 if warned_user and warned_user.reasons is not None: for reason in warned_user.reasons: temp_reason.append(reason) del temp_reason[-1] warned_user.reasons = temp_reason SESSION.add(warned_user) SESSION.commit() removed = True SESSION.close() return removed
def fban_user(fed_id, user_id, first_name, last_name, user_name, reason): 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) SESSION.add(r) try: SESSION.commit() except Exception: SESSION.rollback() return False finally: SESSION.commit() __load_all_feds_banned() return r