Пример #1
0
    def create_bot(self, name, botType, autoRun, redditAuth):
        con = database.get_con()
        cur = database.get_cur(con)

        query = (
            "INSERT INTO rb_bots (name,botType,autoRun,redditAuth) values (?,?,?,?);",
            (name, botType, autoRun, redditAuth),
        )
        result = database.db_qry(query, con=con, cur=cur)
        if isinstance(result, str) and result.find("ERROR") != -1:
            con.commit()
            con.close()
            return result
        else:
            insert_id = database.db_qry("SELECT last_insert_rowid() as id;",
                                        con=con,
                                        cur=cur)[0]["id"]
            log.info("Created bot with id: {}. Inserting default settings...".
                     format(insert_id))
            self.id = insert_id
            self.name = name
            self.botType = botType
            self.autoRun = autoRun
            self.redditAuth = redditAuth

            config.add_default_bot_config(insert_id, con, cur)

            botTypeInfo = config.get_botTypes(botType)
            if (botTypeInfo["defaultSettings"]
                    and len(botTypeInfo["defaultSettings"]) > 2):
                defaultSettings = botTypeInfo["defaultSettings"]
                result = config.add_bot_config(
                    botId=insert_id,
                    multi=defaultSettings,
                    replace=True,
                    con=con,
                    cur=cur,
                    commit=False,
                    closeAfter=False,
                )

            # Create privileges and grant to creator
            q = """INSERT INTO rb_privileges ('privilege', 'description')
                VALUES
                ('rb_bot_{}_ro', 'Read-only access to bot id {}.'),
                ('rb_bot_{}_startstop', 'Access to start and stop bot id {}.'),
                ('rb_bot_{}_rw', 'Full access to bot id {}.')
            ;""".format(insert_id, insert_id, insert_id, insert_id, insert_id,
                        insert_id)
            database.db_qry(q, con=con, cur=cur)

            con.commit()
            con.close()

            return insert_id
Пример #2
0
    def start(self):
        if self.isRunning():
            log.info("Bot {} (id={}) already running.".format(self.name, self.id))
        else:
            botType = config.get_botTypes(self.botType)
            try:
                log.debug(
                    "Attempting to import bot module: {}...".format(
                        botType["moduleName"]
                    )
                )
                self.botMod = importlib.import_module(
                    "bots.{}".format(botType["moduleName"]), "redball"
                )
            except ImportError as e:
                log.debug(
                    "Failed to import from bots directory, trying global import... (Error: {})".format(
                        e
                    )
                )
                try:
                    self.botMod = importlib.import_module(botType["moduleName"])
                    log.debug("Successfully imported bot module.")
                except Exception as e:
                    log.error("Error importing global bot module: {}".format(e))
                    return False

            log.info("Starting bot {} (id={}).".format(self.name, self.id))
            botArgs = self.get_config()
            self.thread = threading.Thread(
                target=self.botMod.run,
                args=(self, botArgs),
                name="bot-{}-{}".format(self.id, self.name.replace(" ", "-")),
                daemon=True,
            )
            self.STOP = False
            self.thread.start()

        return True
Пример #3
0
    def create_bot(self, name, botType, autoRun, redditAuth):
        con = database.get_con()
        cur = database.get_cur(con)

        query = (
            "INSERT INTO rb_bots (name,botType,autoRun,redditAuth) values (?,?,?,?);",
            (name, botType, autoRun, redditAuth),
        )
        result = database.db_qry(query, con=con, cur=cur)
        if isinstance(result, str) and result.find("ERROR") != -1:
            con.commit()
            con.close()
            return result
        else:
            insert_id = database.db_qry("SELECT last_insert_rowid() as id;",
                                        con=con,
                                        cur=cur)[0]["id"]
            log.info("Created bot with id: {}. Inserting default settings...".
                     format(insert_id))
            self.id = insert_id
            self.name = name
            self.botType = botType
            self.autoRun = autoRun
            self.redditAuth = redditAuth

            config.add_default_bot_config(insert_id, con, cur)

            botTypeInfo = config.get_botTypes(botType)
            defaultSettingsFile = os.path.join(
                redball.BOT_PATH, f"{botTypeInfo['moduleName']}_config.json")
            log.debug(
                f"Default settings file for botType {botTypeInfo['name']}: {defaultSettingsFile}"
            )
            if os.path.isfile(defaultSettingsFile):
                try:
                    with open(defaultSettingsFile) as f:
                        defaultSettings = json.load(f)
                except Exception as e:
                    log.error(
                        f"Error occurred while loading default config for [{botTypeInfo['description']}] bot type from json file [{defaultSettingsFile}]: {e}."
                    )
                    defaultSettings = {}

                if len(defaultSettings):
                    log.debug(
                        f"Loaded default settings for botType {botTypeInfo['name']}: {defaultSettings}. Adding to bot {insert_id}..."
                    )
                    result = config.add_bot_config(
                        botId=insert_id,
                        multi=defaultSettings,
                        replace=True,
                        con=con,
                        cur=cur,
                        commit=False,
                        closeAfter=False,
                    )
                else:
                    log.debug(
                        f"No default settings found in the json file for botType {botTypeInfo['name']}."
                    )
            else:
                log.warning(
                    f"No default settings json file found for [{botTypeInfo['description']}] bot type."
                )

            # Create privileges and grant to creator
            q = """INSERT INTO rb_privileges ('privilege', 'description')
                VALUES
                ('rb_bot_{}_ro', 'Read-only access to bot id {}.'),
                ('rb_bot_{}_startstop', 'Access to start and stop bot id {}.'),
                ('rb_bot_{}_rw', 'Full access to bot id {}.')
            ;""".format(insert_id, insert_id, insert_id, insert_id, insert_id,
                        insert_id)
            database.db_qry(q, con=con, cur=cur)

            con.commit()
            con.close()

            return insert_id