Allow to edit prefixes through redbot --edit (#3486)

* feat: allow to edit prefixes through `redbot --edit`

* enhance: allow to setup multiple prefixes

* fix: gotta break out of the loop

* fix: gotta sort prefixes in reversed order

* fix: editing prefix shouldn't save it as token

* fix: sort prefixes when using flag too

* chore(changelog): add towncrier entry

* docs: update help for `--edit` flag
This commit is contained in:
jack1142 2020-02-03 22:08:49 +01:00 committed by GitHub
parent 17234ac8fa
commit 64106c771a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 4 deletions

View File

@ -0,0 +1 @@
Allow to edit prefix from command line using ``redbot --edit``.

View File

@ -107,6 +107,7 @@ async def edit_instance(red, cli_flags):
no_prompt = cli_flags.no_prompt no_prompt = cli_flags.no_prompt
token = cli_flags.token token = cli_flags.token
owner = cli_flags.owner owner = cli_flags.owner
prefix = cli_flags.prefix
old_name = cli_flags.instance_name old_name = cli_flags.instance_name
new_name = cli_flags.edit_instance_name new_name = cli_flags.edit_instance_name
data_path = cli_flags.edit_data_path data_path = cli_flags.edit_data_path
@ -119,14 +120,20 @@ async def edit_instance(red, cli_flags):
if new_name is None and confirm_overwrite: if new_name is None and confirm_overwrite:
print("--overwrite-existing-instance can't be used without --edit-instance-name argument") print("--overwrite-existing-instance can't be used without --edit-instance-name argument")
sys.exit(1) sys.exit(1)
if no_prompt and all(to_change is None for to_change in (token, owner, new_name, data_path)): if (
no_prompt
and all(to_change is None for to_change in (token, owner, new_name, data_path))
and not prefix
):
print( print(
"No arguments to edit were provided. Available arguments (check help for more " "No arguments to edit were provided."
"information): --edit-instance-name, --edit-data-path, --copy-data, --owner, --token" " Available arguments (check help for more information):"
" --edit-instance-name, --edit-data-path, --copy-data, --owner, --token, --prefix"
) )
sys.exit(1) sys.exit(1)
await _edit_token(red, token, no_prompt) await _edit_token(red, token, no_prompt)
await _edit_prefix(red, prefix, no_prompt)
await _edit_owner(red, owner, no_prompt) await _edit_owner(red, owner, no_prompt)
data = deepcopy(data_manager.basic_config) data = deepcopy(data_manager.basic_config)
@ -152,6 +159,26 @@ async def _edit_token(red, token, no_prompt):
print("Token updated.\n") print("Token updated.\n")
async def _edit_prefix(red, prefix, no_prompt):
if prefix:
prefixes = sorted(prefix, reverse=True)
await red._config.prefix.set(prefixes)
elif not no_prompt and confirm("Would you like to change instance's prefixes?", default=False):
print(
"Enter the prefixes, separated by a space (please note "
"that prefixes containing a space will need to be added with [p]set prefix)"
)
while True:
prefixes = input("> ").strip().split()
if not prefixes:
print("You need to pass at least one prefix!")
continue
prefixes = sorted(prefixes, reverse=True)
await red._config.prefix.set(prefixes)
print("Prefixes updated.\n")
break
async def _edit_owner(red, owner, no_prompt): async def _edit_owner(red, owner, no_prompt):
if owner: if owner:
if not (15 <= len(str(owner)) <= 21): if not (15 <= len(str(owner)) <= 21):

View File

@ -90,7 +90,7 @@ def parse_cli_flags(args):
action="store_true", action="store_true",
help="Edit the instance. This can be done without console interaction " help="Edit the instance. This can be done without console interaction "
"by passing --no-prompt and arguments that you want to change (available arguments: " "by passing --no-prompt and arguments that you want to change (available arguments: "
"--edit-instance-name, --edit-data-path, --copy-data, --owner, --token).", "--edit-instance-name, --edit-data-path, --copy-data, --owner, --token, --prefix).",
) )
parser.add_argument( parser.add_argument(
"--edit-instance-name", "--edit-instance-name",