def create_command(self, alias_str, **options): aliases = alias_str.lower().replace('!', '').split('|') for alias in aliases: if alias in self.data: return self.data[alias], False, alias command = Command(command=alias_str, **options) command.data = CommandData(command.id, **options) self.add_db_command_aliases(command) with DBManager.create_session_scope(expire_on_commit=False) as db_session: db_session.add(command) db_session.add(command.data) db_session.commit() db_session.expunge(command) db_session.expunge(command.data) self.db_session.add(command.data) self.commit() self.rebuild() return command, True, ''
def commands_create(**options): session.pop('command_created_id', None) session.pop('command_edited_id', None) if request.method == 'POST': if 'aliases' not in request.form: abort(403) alias_str = request.form.get('aliases', '').replace('!', '').lower() delay_all = request.form.get('cd', Command.DEFAULT_CD_ALL) delay_user = request.form.get('usercd', Command.DEFAULT_CD_USER) level = request.form.get('level', Command.DEFAULT_LEVEL) cost = request.form.get('cost', 0) try: delay_all = int(delay_all) delay_user = int(delay_user) level = int(level) cost = int(cost) except ValueError: abort(403) if len(alias_str) == 0: abort(403) if delay_all < 0 or delay_all > 9999: abort(403) if delay_user < 0 or delay_user > 9999: abort(403) if level < 0 or level > 2000: abort(403) if cost < 0 or cost > 9999999: abort(403) user = options.get('user', None) if user is None: abort(403) options = { 'delay_all': delay_all, 'delay_user': delay_user, 'level': level, 'cost': cost, 'added_by': user.id, } valid_action_types = ['say', 'me', 'whisper', 'reply'] action_type = request.form.get('reply', 'say').lower() if action_type not in valid_action_types: abort(403) response = request.form.get('response', '') if len(response) == 0: abort(403) action = { 'type': action_type, 'message': response } options['action'] = action command_manager = ( CommandManager( socket_manager=None, module_manager=ModuleManager(None).load(), bot=None).load(enabled=None)) command_aliases = [] for alias, command in command_manager.items(): command_aliases.append(alias) if command.command and len(command.command) > 0: command_aliases.extend(command.command.split('|')) command_aliases = set(command_aliases) alias_str = alias_str.replace(' ', '').replace('!', '').lower() alias_list = alias_str.split('|') alias_list = [alias for alias in alias_list if len(alias) > 0] if len(alias_list) == 0: return render_template('admin/create_command_fail.html') for alias in alias_list: if alias in command_aliases: return render_template('admin/create_command_fail.html') alias_str = '|'.join(alias_list) command = Command(command=alias_str, **options) command.data = CommandData(command.id, **options) log_msg = 'The !{} command has been created'.format(command.command.split('|')[0]) AdminLogManager.add_entry('Command created', user, log_msg) with DBManager.create_session_scope(expire_on_commit=False) as db_session: db_session.add(command) db_session.add(command.data) db_session.commit() db_session.expunge(command) db_session.expunge(command.data) SocketClientManager.send('command.update', {'command_id': command.id}) session['command_created_id'] = command.id return redirect('/admin/commands/', 303) else: return render_template('admin/create_command.html')
def commands_create(**options) -> ResponseReturnValue: session.pop("command_created_id", None) session.pop("command_edited_id", None) if request.method != "POST": return render_template("admin/create_command.html") if "aliases" not in request.form: abort(403) alias_str = request.form.get("aliases", "").replace("!", "").lower() delay_all = request.form.get("cd", Command.DEFAULT_CD_ALL) delay_user = request.form.get("usercd", Command.DEFAULT_CD_USER) level = request.form.get("level", Command.DEFAULT_LEVEL) cost = request.form.get("cost", 0) can_execute_with_whisper = request.form.get("whisperable", "off") == "on" sub_only = request.form.get("subonly", "off") == "on" mod_only = request.form.get("modonly", "off") == "on" run_through_banphrases = request.form.get("checkmsg", "off") == "on" try: delay_all = int(delay_all) delay_user = int(delay_user) level = int(level) cost = int(cost) except ValueError: abort(403) if not alias_str: abort(403) if delay_all < 0 or delay_all > 9999: abort(403) if delay_user < 0 or delay_user > 9999: abort(403) if level < 0 or level > 2000: abort(403) if cost < 0 or cost > 9999999: abort(403) user = options.get("user", None) if user is None: abort(403) options = { "delay_all": delay_all, "delay_user": delay_user, "level": level, "cost": cost, "added_by": user.id, "can_execute_with_whisper": can_execute_with_whisper, "sub_only": sub_only, "mod_only": mod_only, "run_through_banphrases": run_through_banphrases, } valid_action_types = ["say", "me", "announce", "whisper", "reply"] action_type = request.form.get("reply", "say").lower() if action_type not in valid_action_types: abort(403) response = request.form.get("response", "") if not response: abort(403) action = {"type": action_type, "message": response} options["action"] = action command_manager = pajbot.managers.command.CommandManager( socket_manager=None, module_manager=ModuleManager(None).load(), bot=None).load(enabled=None) command_aliases_list = [] for alias, command in command_manager.items(): command_aliases_list.append(alias) if command.command and len(command.command) > 0: command_aliases_list.extend(command.command.split("|")) command_aliases = set(command_aliases_list) alias_str = alias_str.replace(" ", "").replace("!", "").lower() alias_list = alias_str.split("|") alias_list = [alias for alias in alias_list if len(alias) > 0] if not alias_list: return render_template("admin/create_command_fail.html") for alias in alias_list: if alias in command_aliases: return render_template("admin/create_command_fail.html") alias_str = "|".join(alias_list) command = Command(command=alias_str, **options) command.data = CommandData(command.id, **options) log_msg = f"The !{command.command.split('|')[0]} command has been created" AdminLogManager.add_entry("Command created", user, log_msg) with DBManager.create_session_scope( expire_on_commit=False) as db_session: db_session.add(command) db_session.add(command.data) db_session.commit() db_session.expunge(command) db_session.expunge(command.data) SocketClientManager.send("command.update", {"command_id": command.id}) session["command_created_id"] = command.id return redirect("/admin/commands", 303)
def commands_create(**options): session.pop('command_created_id', None) session.pop('command_edited_id', None) if request.method == 'POST': if 'aliases' not in request.form: abort(403) alias_str = request.form.get('aliases', '').replace('!', '').lower() delay_all = request.form.get('cd', Command.DEFAULT_CD_ALL) delay_user = request.form.get('usercd', Command.DEFAULT_CD_USER) level = request.form.get('level', Command.DEFAULT_LEVEL) cost = request.form.get('cost', 0) try: delay_all = int(delay_all) delay_user = int(delay_user) level = int(level) cost = int(cost) except ValueError: abort(403) if len(alias_str) == 0: abort(403) if delay_all < 0 or delay_all > 9999: abort(403) if delay_user < 0 or delay_user > 9999: abort(403) if level < 0 or level > 2000: abort(403) if cost < 0 or cost > 9999999: abort(403) options = { 'delay_all': delay_all, 'delay_user': delay_user, 'level': level, 'cost': cost, } valid_action_types = ['say', 'me', 'whisper', 'reply'] action_type = request.form.get('reply', 'say').lower() if action_type not in valid_action_types: abort(403) response = request.form.get('response', '') if len(response) == 0: abort(403) action = {'type': action_type, 'message': response} options['action'] = action command_manager = CommandManager( socket_manager=None, module_manager=ModuleManager(None).load(), bot=None).load(enabled=None) command_aliases = [] for alias, command in command_manager.items(): command_aliases.append(alias) if command.command and len(command.command) > 0: command_aliases.extend(command.command.split('|')) command_aliases = set(command_aliases) alias_str = alias_str.replace(' ', '').replace('!', '').lower() alias_list = alias_str.split('|') alias_list = [alias for alias in alias_list if len(alias) > 0] if len(alias_list) == 0: return render_template('admin/create_command_fail.html') for alias in alias_list: if alias in command_aliases: return render_template('admin/create_command_fail.html') alias_str = '|'.join(alias_list) command = Command(command=alias_str, **options) command.data = CommandData(command.id) with DBManager.create_session_scope( expire_on_commit=False) as db_session: db_session.add(command) db_session.add(command.data) db_session.commit() db_session.expunge(command) db_session.expunge(command.data) SocketClientManager.send('command.update', {'command_id': command.id}) session['command_created_id'] = command.id return redirect('/admin/commands/', 303) else: return render_template('admin/create_command.html')