From b96e471ecaf88f616b7bf05ab006892fbedcee8a Mon Sep 17 00:00:00 2001 From: Kae Bartlett Date: Thu, 18 Jan 2024 03:37:23 +0000 Subject: [PATCH] Remove run-status as it's pretty redundant, and add new flag on run --- docs/exts/client/cli.rst | 6 ---- novus/api/gateway/dispatch.py | 4 +++ novus/ext/client/cli/__main__.py | 53 +++++++------------------------- 3 files changed, 15 insertions(+), 48 deletions(-) diff --git a/docs/exts/client/cli.rst b/docs/exts/client/cli.rst index 19e58a4b..fcc6958e 100644 --- a/docs/exts/client/cli.rst +++ b/docs/exts/client/cli.rst @@ -16,12 +16,6 @@ Run a bot webserver instance (interactions only) with config either passed in as CLI arguments, or from a config file (either found in the current directory, or passed as a CLI arg). -``novus run-status`` --------------------- - -Create a websocket to Discord, connecting only to keep track of guild count and -to display a status on your bot user. - ``novus config-dump`` --------------------- diff --git a/novus/api/gateway/dispatch.py b/novus/api/gateway/dispatch.py index 97e05119..dacf2cf0 100644 --- a/novus/api/gateway/dispatch.py +++ b/novus/api/gateway/dispatch.py @@ -160,6 +160,8 @@ async def handle_dispatch(self, event_name: str, data: dict) -> None: self.cache.application_id = try_snowflake(data['application']['id']) self.shard.resume_url = data['resume_gateway_url'] self.shard.session_id = data['session_id'] + for g in data["guilds"]: + asyncio.create_task(self.handle_dispatch("GUILD_CREATE", g)) self.dispatch("READY") return None elif event_name == "RESUMED": @@ -197,6 +199,8 @@ async def _handle_interaction( async def _handle_guild_create( self, data: payloads.GatewayGuild) -> None: + if data.get("unavailable"): + return # Don't do anything for unavailable guilds guild = Guild(state=self.parent, data=data) self.cache.add_guilds(guild) await guild._sync(data=data) diff --git a/novus/ext/client/cli/__main__.py b/novus/ext/client/cli/__main__.py index d2bf2de2..ec9356c1 100644 --- a/novus/ext/client/cli/__main__.py +++ b/novus/ext/client/cli/__main__.py @@ -37,28 +37,12 @@ class GuildLogger(client.Plugin): + @client.loop(60) async def print_loop(self, sleep_time: float = 60.0) -> None: self.log.info( - "Starting guild logger loop; printing guild count every %s seconds", - sleep_time, + "There are currently %s guild IDs in cache", + len(self.bot.state.cache.guild_ids), ) - while True: - self.log.info( - "There are currently %s guild IDs in cache", - len(self.bot.state.cache.guild_ids), - ) - try: - await asyncio.sleep(sleep_time) - except asyncio.CancelledError: - break - - async def on_load(self) -> None: - await self.bot.wait_until_ready() - self.print_loop_task = asyncio.create_task(self.print_loop()) - - async def on_unload(self) -> None: - if not self.print_loop_task.done(): - self.print_loop_task.cancel() @client.event("GUILD_CREATE") async def guild_added(self, guild: novus.Guild) -> None: @@ -100,6 +84,7 @@ def get_parser() -> ArgumentParser: rap.add_argument("--no-intent", nargs="*", type=str, default=None) rap.add_argument("--plugins", nargs="?", type=str, const="", default=None) rap.add_argument("--plugin", nargs="*", type=str, default=None) + rap.add_argument("--guild-id-cache-only", type=bool, default=False, action="store_true") rwsap = ap.add_parser("run-webserver") rwsap.add_argument("--config", nargs="?", const=None, default=None) @@ -111,14 +96,6 @@ def get_parser() -> ArgumentParser: rwsap.add_argument("--plugins", nargs="?", type=str, const="", default=None) rwsap.add_argument("--plugin", nargs="*", type=str, default=None) - rsap = ap.add_parser("run-status") - rsap.add_argument("--config", nargs="?", const=None, default=None) - rsap.add_argument("--loglevel", default='info', choices=logger_choices) - rsap.add_argument("--token", nargs="?", type=str, default=None) - rsap.add_argument("--shard-id", nargs="*", type=str, default=None) - rsap.add_argument("--shard-ids", nargs="?", type=str, const="", default=None) - rsap.add_argument("--shard-count", nargs="?", type=str, default=None) - cap = ap.add_parser("config-dump") cap.add_argument("type", choices=["json", "yaml", "toml"]) cap.add_argument("--token", nargs="?", type=str, default=None) @@ -230,6 +207,13 @@ async def main(args: Namespace, unknown: list[str]) -> None: root.setLevel(level) config.merge_namespace(args, unknown) bot = client.Client(config) + if args.guild_id_cache_only: + bot.state.cache = GuildIDCache(bot.state) + bot.state.gateway.guild_ids_only = True + try: + bot.add_plugin(GuildLogger) + except Exception: + pass await asyncio.gather( bot.run(sync=not args.no_sync), create_console(bot).interact( @@ -254,21 +238,6 @@ async def main(args: Namespace, unknown: list[str]) -> None: create_console(bot).interact(banner="Created console :)", stop=False), ) - case "run-status": - config = client.Config.from_file(args.config) - if "loglevel" in args: - root = logging.Logger.root - level = getattr(logging, args.loglevel.upper()) - root.setLevel(level) - config.merge_namespace(args, unknown) - config.plugins = [] - config.intents = novus.Intents(guilds=True) - bot = client.Client(config) - bot.state.cache = GuildIDCache(bot.state) - bot.state.gateway.guild_ids_only = True - bot.add_plugin(GuildLogger) - await bot.run(sync=False) - case "config-dump": config = client.Config() logging.Logger.root.setLevel(logging.ERROR)