async def initialize(bot): #lock event handling while we get ready bot.locked = True try: #database GearbotLogging.info("Connecting to the database.") DatabaseConnector.init() bot.database_connection = DatabaseConnector.connection GearbotLogging.info("Database connection established.") GearbotLogging.initialize_pump(bot) Emoji.initialize(bot) Pages.initialize(bot) Utils.initialize(bot) Translator.initialize(bot) InfractionUtils.initialize(bot) bot.data = { "forced_exits": set(), "unbans": set(), "message_deletes": set() } await GearbotLogging.initialize( bot, Configuration.get_master_var("BOT_LOG_CHANNEL")) if bot.redis_pool is None or not hasattr( bot, 'redis_raid_pool') or bot.redis_raid_pool is None: try: bot.redis_pool = await aioredis.create_redis_pool( (Configuration.get_master_var('REDIS_HOST', "localhost"), Configuration.get_master_var('REDIS_PORT', 6379)), encoding="utf-8", db=0) bot.redis_raid_pool = await aioredis.create_redis_pool( (Configuration.get_master_var('REDIS_HOST', "localhost"), Configuration.get_master_var('REDIS_PORT', 6379)), encoding="utf-8", db=1) except OSError: GearbotLogging.error( "==============Failed to connect to redis==============") await GearbotLogging.bot_log( f"{Emoji.get_chat_emoji('NO')} Failed to connect to redis, caching and anti-raid connections unavailable" ) else: GearbotLogging.info("Redis connection established") await GearbotLogging.bot_log( f"{Emoji.get_chat_emoji('YES')} Redis connection established, caching and anti-raid connections established" ) if bot.aiosession is None: bot.aiosession = aiohttp.ClientSession() bot.being_cleaned.clear() await Configuration.initialize(bot) except Exception as ex: #make sure we always unlock, even when something went wrong! bot.locked = False raise ex bot.locked = False
async def on_ready(bot): if not bot.STARTUP_COMPLETE: GearbotLogging.initialize_pump(bot) await GearbotLogging.onReady( bot, Configuration.get_master_var("BOT_LOG_CHANNEL")) info = await bot.application_info() await GearbotLogging.bot_log(message="Spinning up the gears!") await Util.readyBot(bot) Emoji.on_ready(bot) Utils.on_ready(bot) Translator.on_ready(bot) bot.loop.create_task( keepDBalive(bot)) # ping DB every hour so it doesn't run off #shutdown handler for clean exit on linux try: for signame in ('SIGINT', 'SIGTERM'): asyncio.get_event_loop().add_signal_handler( getattr(signal, signame), lambda: asyncio.ensure_future( Utils.cleanExit(bot, signame))) except Exception: pass #doesn't work on windows bot.aiosession = aiohttp.ClientSession() bot.start_time = datetime.datetime.utcnow() GearbotLogging.info("Loading cogs...") for extension in Configuration.get_master_var("COGS"): try: bot.load_extension("Cogs." + extension) except Exception as e: GearbotLogging.exception( f"Failed to load extention {extension}", e) GearbotLogging.info("Cogs loaded") if Configuration.get_master_var("CROWDIN_KEY") is not None: bot.loop.create_task(translation_task(bot)) await DocUtils.update_docs(bot) bot.STARTUP_COMPLETE = True await GearbotLogging.bot_log( message=f"All gears turning at full speed, {info.name} ready to go!" ) await bot.change_presence( activity=discord.Activity(type=3, name='the gears turn')) else: await bot.change_presence( activity=discord.Activity(type=3, name='the gears turn'))
async def initialize(bot, startup=False): #lock event handling while we get ready bot.locked = True try: #database GearbotLogging.info("Connecting to the database.") DatabaseConnector.init() bot.database_connection = DatabaseConnector.connection GearbotLogging.info("Database connection established.") GearbotLogging.initialize_pump(bot) Emoji.initialize(bot) Utils.initialize(bot) InfractionUtils.initialize(bot) bot.data = { "forced_exits": set(), "unbans": set(), "message_deletes": set() } await GearbotLogging.initialize( bot, Configuration.get_master_var("BOT_LOG_CHANNEL")) if startup: c = await Utils.get_commit() bot.version = c GearbotLogging.info(f"GearBot spinning up version {c}") await GearbotLogging.bot_log( f"{Emoji.get_chat_emoji('ALTER')} GearBot spinning up version {c}" ) if bot.redis_pool is None: try: socket = Configuration.get_master_var("REDIS_SOCKET", "") if socket == "": bot.redis_pool = await aioredis.create_redis_pool( (Configuration.get_master_var('REDIS_HOST', "localhost"), Configuration.get_master_var('REDIS_PORT', 6379)), encoding="utf-8", db=0) else: bot.redis_pool = await aioredis.create_redis_pool( socket, encoding="utf-8", db=0) except OSError: GearbotLogging.error( "==============Failed to connect to redis==============") await GearbotLogging.bot_log( f"{Emoji.get_chat_emoji('NO')} Failed to connect to redis, caching unavailable" ) else: GearbotLogging.info("Redis connection established") await GearbotLogging.bot_log( f"{Emoji.get_chat_emoji('YES')} Redis connection established, let's go full speed!" ) if bot.aiosession is None: bot.aiosession = aiohttp.ClientSession() await Translator.initialize(bot) bot.being_cleaned.clear() await Configuration.initialize(bot) except Exception as ex: #make sure we always unlock, even when something went wrong! bot.locked = False raise ex bot.locked = False