Skip to content

Commit

Permalink
Add console commands for when the bot is running
Browse files Browse the repository at this point in the history
  • Loading branch information
4Kaylum committed Jul 5, 2023
1 parent 7119406 commit 6451ccc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
43 changes: 41 additions & 2 deletions novus/ext/client/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
from __future__ import annotations

import asyncio
import functools
import logging
import sys
import textwrap
from argparse import ArgumentParser, Namespace

from aioconsole import AsynchronousCli

import novus
from novus.api._cache import NothingAPICache
from novus.ext import client
Expand Down Expand Up @@ -129,6 +132,36 @@ def get_parser() -> ArgumentParser:
return p


def create_console(bot: client.Client) -> AsynchronousCli:
"""
Create a console that users are able to interact with while the bot is
running.
"""

add_plugin = ArgumentParser()
add_plugin.add_argument("plugin")
remove_plugin = ArgumentParser()
remove_plugin.add_argument("plugin")
reload_plugin = ArgumentParser()
reload_plugin.add_argument("plugin")

async def close():
await bot.close()
exit(1)

add = functools.partial(bot.add_plugin, load=True)

def reload(p):
bot.remove_plugin_file(p)
bot.add_plugin(p)

return AsynchronousCli({
"add-plugin": (add, add_plugin,),
"reload-plugin": (reload, remove_plugin,),
"remove-plugin": (bot.remove_plugin, remove_plugin,),
"exit": (close, ArgumentParser(),),
})

async def main(args: Namespace, unknown: list[str]) -> None:
"""
Main input point for our CLI.
Expand All @@ -143,7 +176,10 @@ async def main(args: Namespace, unknown: list[str]) -> None:
root.setLevel(level)
config.merge_namespace(args, unknown)
bot = client.Client(config)
await bot.run(sync=not args.no_sync)
await asyncio.gather(
bot.run(sync=not args.no_sync),
create_console(bot).interact(banner="Created console :)"),
)

case "run-webserver":
config = client.Config.from_file(args.config)
Expand All @@ -153,7 +189,10 @@ async def main(args: Namespace, unknown: list[str]) -> None:
root.setLevel(level)
config.merge_namespace(args, unknown)
bot = client.Client(config)
await bot.run_webserver(sync=not args.no_sync, port=args.port)
await asyncio.gather(
bot.run_webserver(sync=not args.no_sync, port=args.port),
create_console(bot).interact(banner="Created console :)"),
)

case "run-status":
config = client.Config.from_file(args.config)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ aiohttp>=3.8.0
emoji
typing_extensions
pynacl
aioconsole

# Recommended
python-dotenv
Expand Down

0 comments on commit 6451ccc

Please sign in to comment.