def __init__(self, bot: Tux, version_info): self.bot = bot self.version_info = version_info self._batch_lock = asyncio.Lock() self._data_batch: List[Dict[str, Any]] = [] self._gateway_queue: asyncio.Queue = asyncio.Queue() self.gateway_worker.start() # pylint: disable=no-member self.__config: LogsConfig = ConfigFile( str(cogs_data_path("Logs") / "config.yaml"), LogsConfig, ).config self._resumes: List[datetime.datetime] = [] self._identifies: DefaultDict[Any, list] = defaultdict(list) self.old_on_error = bot.on_error bot.on_error = self.on_error if self.bot.instance_name != "dev": # pylint: disable=abstract-class-instantiated sentry_sdk.init( dsn=self.__config.sentryKey, traces_sample_rate=1.0, environment=self.bot.instance_name, debug=False, attach_stacktrace=True, )
def run() -> None: """Main function""" tux = None cli_flags = parse_cli_flags(sys.argv[1:]) if cli_flags.debug: debug_info() elif cli_flags.version: print(f"Tuxbot V{version_info.major}") print(f"Complete Version: {__version__}") sys.exit(os.EX_OK) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) try: log.info("Starting TuxBot...") tux = Tux( cli_flags=cli_flags, description="Tuxbot, made from and for OpenSource", dm_help=None, ) loop.run_until_complete(run_bot(tux, cli_flags)) except KeyboardInterrupt: console.print( " [red]Please use <prefix>quit instead of Ctrl+C to Shutdown!" ) log.warning("Please use <prefix>quit instead of Ctrl+C to Shutdown!") log.info("Received KeyboardInterrupt") console.print("[i]Trying to shutdown...") if tux is not None: loop.run_until_complete(shutdown_handler(tux, signal.SIGINT)) except SystemExit as exc: log.info("Shutting down with exit code: %s", exc.code) if tux is not None: loop.run_until_complete(shutdown_handler(tux, None, exc.code)) raise except Exception as exc: log.error("Unexpected exception (%s): ", type(exc)) console.print_exception( width=console.width, show_locals=True, word_wrap=True, extra_lines=5, ) if tux is not None: loop.run_until_complete(shutdown_handler(tux, None, 1)) finally: loop.run_until_complete(loop.shutdown_asyncgens()) log.info("Please wait, cleaning up a bit more") loop.run_until_complete(asyncio.sleep(1)) asyncio.set_event_loop(None) loop.stop() loop.close() exit_code = ExitCodes.CRITICAL if tux is None else tux.shutdown_code sys.exit(exit_code)
def __init__(self, bot: Tux, version_info): self.bot = bot self.version_info = version_info self.__config: HelpConfig = ConfigFile( str(cogs_data_path("Help") / "config.yaml"), HelpConfig, ).config self.old_help_command = bot.help_command bot.help_command = HelpCommand(self.__config) bot.help_command.cog = self
async def shutdown_handler(tux: Tux, signal_type, exit_code=None) -> None: """Handler when the bot shutdown It cancels all running task. Parameters ---------- tux:Tux Object for the bot. signal_type:int, None Exiting signal code. exit_code:None|int Code to show when exiting. """ if signal_type: log.info("%s received. Quitting...", signal_type) elif exit_code is None: log.info("Shutting down from unhandled exception") tux.shutdown_code = ExitCodes.CRITICAL if exit_code is not None: tux.shutdown_code = exit_code await tux.shutdown()
def setup(bot: Tux): bot.add_cog(Network(bot, version_info))
def setup(bot: Tux): bot.add_cog(Stats(bot, version_info))
def setup(bot: Tux): bot.add_cog(Mod(bot, version_info))
def setup(bot: Tux): cog = Logs(bot, version_info) bot.add_cog(cog) handler = GatewayHandler(cog) logging.getLogger().addHandler(handler)
def setup(bot: Tux): bot.add_cog(Utils(bot, version_info))
def setup(bot: Tux): bot.add_cog(Admin(bot, version_info))
def setup(bot: Tux): bot.add_cog(Custom(bot, version_info))
def setup(bot: Tux): bot.add_cog(Test(bot, version_info))
def setup(bot: Tux): bot.add_cog(Linux(bot, version_info))