Skip to content

Commit

Permalink
Remove run-status as it's pretty redundant, and add new flag on run
Browse files Browse the repository at this point in the history
  • Loading branch information
4Kaylum committed Jan 18, 2024
1 parent 97a6609 commit b96e471
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 48 deletions.
6 changes: 0 additions & 6 deletions docs/exts/client/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
---------------------

Expand Down
4 changes: 4 additions & 0 deletions novus/api/gateway/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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)
Expand Down
53 changes: 11 additions & 42 deletions novus/ext/client/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down

0 comments on commit b96e471

Please sign in to comment.