Exemple #1
0
    def load_db_commands(self, **options):
        """ This method is only meant to be run once.
        Any further updates to the db_commands dictionary will be done
        in other methods.

        """

        if self.db_commands:
            return self.db_commands

        query = self.db_session.query(Command)

        if options.get("load_examples", False) is True:
            query = query.options(joinedload(Command.examples))
        if options.get("enabled", True) is True:
            query = query.filter_by(enabled=True)

        for command in query:
            self.add_db_command_aliases(command)
            self.db_session.expunge(command)
            if command.data is None:
                log.info(f"Creating command data for {command.command}")
                command.data = CommandData(command.id)
            self.db_session.add(command.data)

        return self.db_commands
Exemple #2
0
 def load_by_id(self, command_id):
     self.db_session.commit()
     command = (self.db_session.query(Command).filter_by(
         id=command_id, enabled=True).one_or_none())
     if command:
         self.add_db_command_aliases(command)
         self.db_session.expunge(command)
         if command.data is None:
             log.info(f"Creating command data for {command.command}")
             command.data = CommandData(command.id)
         self.db_session.add(command.data)
Exemple #3
0
    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, ""
Exemple #4
0
    def commands_create(**options):
        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)

        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.discord_id,
        }

        valid_action_types = ["reply", "privatemessage"]
        action_type = request.form.get("type").lower()
        if action_type not in valid_action_types:
            abort(403)

        response = request.form.get("response", "")
        log.info(user.level)
        functions = (
            request.form.get("functions", "").split(" ") if user.level >= 1500 else []
        )
        log.info(functions)

        action = {"type": action_type, "message": response, "functions": functions}
        options["action"] = action

        command_manager = greenbot.managers.command.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 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.discord_id, 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)