mirror of https://gitlab.com/litecord/litecord.git
Fix migration on windows + make generate_token generate only bot ids
This commit is contained in:
parent
efdcbf9937
commit
2542084c73
|
|
@ -1,4 +1,5 @@
|
||||||
import inspect
|
import inspect
|
||||||
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
@ -22,14 +23,13 @@ class MigrationContext:
|
||||||
@property
|
@property
|
||||||
def latest(self):
|
def latest(self):
|
||||||
"""Return the latest migration ID."""
|
"""Return the latest migration ID."""
|
||||||
return max(self.scripts.keys())
|
return 0 if len(self.scripts) == 0 else max(self.scripts.keys())
|
||||||
|
|
||||||
|
|
||||||
def make_migration_ctx() -> MigrationContext:
|
def make_migration_ctx() -> MigrationContext:
|
||||||
"""Create the MigrationContext instance."""
|
"""Create the MigrationContext instance."""
|
||||||
# taken from https://stackoverflow.com/a/6628348
|
# taken from https://stackoverflow.com/a/6628348
|
||||||
script_path = inspect.stack()[0][1]
|
script_path = inspect.stack()[0][1]
|
||||||
script_folder = '/'.join(script_path.split('/')[:-1])
|
script_folder = os.sep.join(script_path.split(os.sep)[:-1])
|
||||||
script_folder = Path(script_folder)
|
script_folder = Path(script_folder)
|
||||||
|
|
||||||
migration_folder = script_folder / 'scripts'
|
migration_folder = script_folder / 'scripts'
|
||||||
|
|
@ -40,7 +40,7 @@ def make_migration_ctx() -> MigrationContext:
|
||||||
mig_path_str = str(mig_path)
|
mig_path_str = str(mig_path)
|
||||||
|
|
||||||
# extract migration script id and name
|
# extract migration script id and name
|
||||||
mig_filename = mig_path_str.split('/')[-1].split('.')[0]
|
mig_filename = mig_path_str.split(os.sep)[-1].split('.')[0]
|
||||||
name_fragments = mig_filename.split('_')
|
name_fragments = mig_filename.split('_')
|
||||||
|
|
||||||
mig_id = int(name_fragments[0])
|
mig_id = int(name_fragments[0])
|
||||||
|
|
|
||||||
|
|
@ -12,14 +12,6 @@ async def find_user(username, discrim, ctx):
|
||||||
WHERE username = $1 AND discriminator = $2
|
WHERE username = $1 AND discriminator = $2
|
||||||
""", username, discrim)
|
""", username, discrim)
|
||||||
|
|
||||||
async def get_password_hash(id, ctx):
|
|
||||||
return await ctx.db.fetchval("""
|
|
||||||
SELECT password_hash
|
|
||||||
FROM users
|
|
||||||
WHERE id = $1
|
|
||||||
""", id)
|
|
||||||
|
|
||||||
|
|
||||||
async def set_user_staff(user_id, ctx):
|
async def set_user_staff(user_id, ctx):
|
||||||
"""Give a single user staff status."""
|
"""Give a single user staff status."""
|
||||||
old_flags = await ctx.db.fetchval("""
|
old_flags = await ctx.db.fetchval("""
|
||||||
|
|
@ -62,15 +54,19 @@ async def make_staff(ctx, args):
|
||||||
await set_user_staff(uid, ctx)
|
await set_user_staff(uid, ctx)
|
||||||
print('OK: set staff')
|
print('OK: set staff')
|
||||||
|
|
||||||
async def generate_token(ctx, args):
|
async def generate_bot_token(ctx, args):
|
||||||
"""Generate a token for specified user."""
|
"""Generate a token for specified bot."""
|
||||||
uid = await find_user(args.username, args.discrim, ctx)
|
|
||||||
|
|
||||||
if not uid:
|
password_hash = await ctx.db.fetchval("""
|
||||||
return print('user not found')
|
SELECT password_hash
|
||||||
|
FROM users
|
||||||
password_hash = await get_password_hash(uid, ctx)
|
WHERE id = $1 AND bot = 'true'
|
||||||
print(make_token(uid, password_hash))
|
""", int(args.user_id))
|
||||||
|
|
||||||
|
if not password_hash:
|
||||||
|
return print('cannot find a bot with specified id')
|
||||||
|
|
||||||
|
print(make_token(args.user_id, password_hash))
|
||||||
|
|
||||||
|
|
||||||
def setup(subparser):
|
def setup(subparser):
|
||||||
|
|
@ -105,15 +101,12 @@ def setup(subparser):
|
||||||
|
|
||||||
token_parser = subparser.add_parser(
|
token_parser = subparser.add_parser(
|
||||||
'generate_token',
|
'generate_token',
|
||||||
help='generate a token for specified user',
|
help='generate a token for specified bot',
|
||||||
description=generate_token.__doc__
|
description=generate_bot_token.__doc__
|
||||||
)
|
)
|
||||||
|
|
||||||
token_parser.add_argument(
|
token_parser.add_argument(
|
||||||
'username'
|
'user_id'
|
||||||
)
|
|
||||||
token_parser.add_argument(
|
|
||||||
'discrim', help='the discriminator of the user'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
token_parser.set_defaults(func=generate_token)
|
token_parser.set_defaults(func=generate_bot_token)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue