manage: add setflag and unsetflag commands

This commit is contained in:
Luna 2021-09-02 21:43:46 -03:00
parent 111d747c38
commit 1a0fe767c3
1 changed files with 69 additions and 24 deletions

View File

@ -36,8 +36,7 @@ async def find_user(username, discrim, ctx) -> int:
) )
async def set_user_staff(user_id, ctx): async def set_any_user_flag(ctx, user_id, flag_name):
"""Give a single user staff status."""
old_flags = await ctx.db.fetchval( old_flags = await ctx.db.fetchval(
""" """
SELECT flags SELECT flags
@ -47,7 +46,30 @@ async def set_user_staff(user_id, ctx):
user_id, user_id,
) )
new_flags = old_flags | UserFlags.staff new_flags = old_flags | getattr(UserFlags, flag_name)
await ctx.db.execute(
"""
UPDATE users
SET flags=$1
WHERE id = $2
""",
new_flags,
user_id,
)
async def unset_any_user_flag(ctx, user_id, flag_name):
old_flags = await ctx.db.fetchval(
"""
SELECT flags
FROM users
WHERE id = $1
""",
user_id,
)
new_flags = old_flags ^ getattr(UserFlags, flag_name)
await ctx.db.execute( await ctx.db.execute(
""" """
@ -72,21 +94,35 @@ async def adduser(ctx, args):
print(f'\tdiscrim: {user["discriminator"]}') print(f'\tdiscrim: {user["discriminator"]}')
async def make_staff(ctx, args): async def set_flag(ctx, args):
"""Give a single user the staff flag. """Setting a 'staff' flag gives the user access to the Admin API.
Beware of that.
This will grant them access to the Admin API. Flag changes only apply to a user after a server restart so that
all connected clients get to refresh their state.
The flag changes will only apply after a
server restart.
""" """
uid = await find_user(args.username, args.discrim, ctx) uid = await find_user(args.username, args.discrim, ctx)
if not uid: if not uid:
return print("user not found") return print("user not found")
await set_user_staff(uid, ctx) await set_any_user_flag(ctx, uid, args.flag_name)
print("OK: set staff") print(f"OK: set {args.flag_name}")
async def unset_flag(ctx, args):
"""Unsetting a 'staff' flag revokes the user's access to the Admin API.
Flag changes only apply to a user after a server restart so that
all connected clients get to refresh their state.
"""
uid = await find_user(args.username, args.discrim, ctx)
if not uid:
return print("user not found")
await unset_any_user_flag(ctx, uid, args.flag_name)
print(f"OK: unset {args.flag_name}")
async def generate_bot_token(ctx, args): async def generate_bot_token(ctx, args):
@ -162,14 +198,23 @@ def setup(subparser):
setup_test_parser.set_defaults(func=adduser) setup_test_parser.set_defaults(func=adduser)
staff_parser = subparser.add_parser( setflag_parser = subparser.add_parser(
"make_staff", help="make a user staff", description=make_staff.__doc__ "setflag", help="set a flag for a user", description=set_flag.__doc__
) )
setflag_parser.add_argument("username")
setflag_parser.add_argument("discrim", help="the discriminator of the user")
setflag_parser.add_argument("flag_name", help="flag to set"),
staff_parser.add_argument("username") setflag_parser.set_defaults(func=set_flag)
staff_parser.add_argument("discrim", help="the discriminator of the user")
staff_parser.set_defaults(func=make_staff) unsetflag_parser = subparser.add_parser(
"unsetflag", help="unset a flag for a user", description=unset_flag.__doc__
)
unsetflag_parser.add_argument("username")
unsetflag_parser.add_argument("discrim", help="the discriminator of the user")
unsetflag_parser.add_argument("flag_name", help="flag to unset"),
unsetflag_parser.set_defaults(func=unset_flag)
del_user_parser = subparser.add_parser("deluser", help="delete a single user") del_user_parser = subparser.add_parser("deluser", help="delete a single user")