示例#1
0
def refresh(bot_input, bot_output):
    if bot_output.master.lower() in bot_input.nick.lower():
        bot_output.say("Refreshing Plugins")
        reload.reload(bot_input.bot)
        config.config(bot_input.bot)
        bot_output.say("I'm all shiny and new now")
    else:
        bot_output.say("I'm gonna refresh my fist into your face!")
示例#2
0
def run_bot():

    bot = Bot()
    bot._config_mtime = 0

    parser = argparse.ArgumentParser(description="It's a Bot.  Nuff Said")
    parser.add_argument('-a','--adapter', help='Adapter (console or flowbot).  Default is console.')

    args = parser.parse_args()

    adapter_name = args.adapter

    if not adapter_name or not hasattr(adapters, adapter_name.lower()):
        logger.log("Adapter not found.  Try console or flowbot.  Using console")
        adapter_name = "console"

    adapter_class = getattr(adapters, adapter_name.lower())

    sys.path += ['plugins']  # so 'import hook' works without duplication
    os.chdir(sys.path[0] or '.')  # do stuff relative to the install directory

    print('Loading plugins')
    config.config(bot)
    reload.reload(bot, init=True)

    if not hasattr(bot, 'config'):
        logger.log("no config found for bot", logging.ERROR)
        exit()

    logger.log("Connecting")

    bot.conns = {}
    bot.credentials = {}
    try:
        if adapter_name in bot.config['adapters']:
            for room, conf in bot.config['adapters'][adapter_name]["rooms"].items():
                conf["responses"] = personality.load_personality(conf["personality"].lower())
                bot.conns[room] = adapter_class.BotOutput(conf)
        else:
            error_message = "Adapter not found in config: {0}".format(adapter_name)
            print(error_message)
            logger.error(error_message)
            sys.exit()
        for name, conf in bot.config['credentials'].items():
            bot.credentials[name] = conf
    except Exception as e:
        logger.log("malformed config file %s" % e, logging.ERROR)
        sys.exit()

    bot.persist_dir = os.path.abspath('persist')
    if not os.path.exists(bot.persist_dir):
        os.mkdir(bot.persist_dir)

    logger.log("Running main loop")

    last_error = datetime(2000,1,1)
    last_run = datetime.now()

    while (last_error - last_run).seconds > 10:
        reload.reload(bot)  # these functions only do things
        config.config(bot)  # if changes have occurred

        for conn, adapter in bot.conns.items():
            try:
                last_run = datetime.now()
                adapter.run(bot)
            except SystemExit as ex:
                last_error = last_run
            except Exception as e:
                logger.error(traceback.format_exc())
                for info in sys.exc_info():
                    logger.log("error info: " + str(info))
                #logger.log("Unexpected error: %s" % sys.exc_info())
                last_error = datetime.now()
                logger.log("So tired... sleeping for 5 seconds")
                time.sleep(5)