From 6c0f682e5468e710793ac31f7593ce40a92f7c44 Mon Sep 17 00:00:00 2001 From: Bluenix Date: Wed, 26 Jan 2022 21:22:46 +0100 Subject: [PATCH 1/3] Add 'addbot' manage.py command --- manage/cmd/users.py | 27 ++++++++++++++++++++++++++- manage/main.py | 7 ++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/manage/cmd/users.py b/manage/cmd/users.py index 10aefd1..b5ff230 100644 --- a/manage/cmd/users.py +++ b/manage/cmd/users.py @@ -94,6 +94,22 @@ async def adduser(ctx, args): print(f'\tdiscrim: {user["discriminator"]}') +async def addbot(ctx, args): + uid, _ = await create_user(args.username, args.email, args.password) + + await ctx.db.execute( + """ + UPDATE users + SET bot=True + WHERE id = $1 + """, + uid, + ) + + args.user_id = uid + + return await generate_bot_token(ctx, args) + async def set_flag(ctx, args): """Setting a 'staff' flag gives the user access to the Admin API. Beware of that. @@ -138,7 +154,8 @@ async def generate_bot_token(ctx, args): ) if not password_hash: - return print("cannot find a bot with specified id") + print("cannot find a bot with specified id") + return 1 print(make_token(args.user_id, password_hash)) @@ -198,6 +215,14 @@ def setup(subparser): setup_test_parser.set_defaults(func=adduser) + addbot_parser = subparser.add_parser("addbot", help="create a bot") + + addbot_parser.add_argument("username", help="username of the bot") + addbot_parser.add_argument("email", help="email of the bot") + addbot_parser.add_argument("password", help="password of the bot") + + addbot_parser.set_defaults(func=addbot) + setflag_parser = subparser.add_parser( "setflag", help="set a flag for a user", description=set_flag.__doc__ ) diff --git a/manage/main.py b/manage/main.py index f65d430..a5060ac 100644 --- a/manage/main.py +++ b/manage/main.py @@ -20,7 +20,7 @@ along with this program. If not, see . import asyncio import argparse import inspect -from sys import argv +from sys import argv, exit from dataclasses import dataclass from quart import Quart @@ -93,7 +93,7 @@ def main(config): async def _ctx_wrapper(fake_app, args): app = fake_app.make_app() async with app.app_context(): - await args.func(fake_app, args) + return await args.func(fake_app, args) try: if len(argv) < 2: @@ -107,8 +107,9 @@ def main(config): init_app_managers(app, init_voice=False) args = parser.parse_args() - loop.run_until_complete(_ctx_wrapper(app, args)) + return loop.run_until_complete(_ctx_wrapper(app, args)) except Exception: log.exception("error while running command") + return 1 finally: loop.run_until_complete(app.db.close()) From 9347bc2a268f42bebefb6e96cc9b543881012eef Mon Sep 17 00:00:00 2001 From: Bluenix Date: Thu, 27 Jan 2022 14:37:43 +0100 Subject: [PATCH 2/3] Fix incorrect number of newlines (black) --- __init__.py | 1 - manage/cmd/users.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/__init__.py b/__init__.py index f0e4609..3878d03 100644 --- a/__init__.py +++ b/__init__.py @@ -16,4 +16,3 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ - diff --git a/manage/cmd/users.py b/manage/cmd/users.py index b5ff230..d1ac694 100644 --- a/manage/cmd/users.py +++ b/manage/cmd/users.py @@ -110,6 +110,7 @@ async def addbot(ctx, args): return await generate_bot_token(ctx, args) + async def set_flag(ctx, args): """Setting a 'staff' flag gives the user access to the Admin API. Beware of that. From 61ca9ab7bcf378eda526ba1462dcc3f4329620cf Mon Sep 17 00:00:00 2001 From: Bluenix Date: Thu, 27 Jan 2022 14:56:01 +0100 Subject: [PATCH 3/3] Remove unused `manage/main.py` import --- manage/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manage/main.py b/manage/main.py index a5060ac..457673f 100644 --- a/manage/main.py +++ b/manage/main.py @@ -20,7 +20,7 @@ along with this program. If not, see . import asyncio import argparse import inspect -from sys import argv, exit +from sys import argv from dataclasses import dataclass from quart import Quart