Skip to content

Commit

Permalink
udpate config command
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Baji committed Jul 1, 2024
1 parent 845cab5 commit 2e8da78
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 68 deletions.
41 changes: 10 additions & 31 deletions discordai/command_line/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@ def discordai():
parser, is_parent=True
)

bot_cmd = command.add_parser(
"bot", description="Commands related to your Discord bot"
)
config_cmd = command.add_parser("config", description="View and modify your config")
bot_cmd = command.add_parser("bot", help="Manage your Discord bot")
config_cmd = command.add_parser("config", help="View your DiscordAI config")

bot_subcommand = bot_cmd.add_subparsers(dest="subcommand")
bot_subsubcommand = bot_subcommand.add_parser(
"command", description="Manage your Discord bot's slash commands"
"command", help="Manage your Discord bot's slash commands"
).add_subparsers(dest="subsubcommand")
config_subcommand = config_cmd.add_subparsers(dest="subcommand")

subparsers.setup_bot_start(bot_subcommand)
subparsers.setup_add_bot_command(bot_subsubcommand)
subparsers.setup_delete_bot_command(bot_subsubcommand)
subparsers.setup_update_bot_token(config_subcommand)
subparsers.setup_update_openai_key(config_subcommand)
subparsers.setup_update_config(config_subcommand)

args = parser.parse_args()
config = configuration.get()
Expand Down Expand Up @@ -75,31 +72,13 @@ def discordai():
"Must choose a command from `start` or `commands`",
)
elif args.command == "config":
if args.subcommand == "bot-token":
if args.new_token:
print(f"Old discord bot token: {config['DISCORD_BOT_TOKEN']}")
configuration.save(
dict(
DISCORD_BOT_TOKEN=args.new_token,
OPENAI_API_KEY=config["OPENAI_API_KEY"],
)
)
if args.subcommand == "update":
if args.key in config:
config[args.key] = args.value
configuration.save(config)
config = configuration.get()
print(f"Current discord bot token: {config['DISCORD_BOT_TOKEN']}\n")
elif args.subcommand == "openai-key":
if args.new_key:
print(f"Old openAi API key: {config['OPENAI_API_KEY']}")
configuration.save(
dict(
DISCORD_BOT_TOKEN=config["DISCORD_BOT_TOKEN"],
OPENAI_API_KEY=args.new_key,
)
)
config = configuration.get()
print(f"Current openAi API key: {config['OPENAI_API_KEY']}\n")
else:
print(f"Current discord bot token: {config['DISCORD_BOT_TOKEN']}")
print(f"Current openAi API key: {config['OPENAI_API_KEY']}\n")
print("Config:")
command_line.display(config)


if __name__ == "__main__":
Expand Down
46 changes: 9 additions & 37 deletions discordai/command_line/subparsers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
def setup_bot_start(bot_subcommand):
bot_cmd_start = bot_subcommand.add_parser(
"start", description="Start your discord bot"
)
bot_cmd_start = bot_subcommand.add_parser("start", help="Start your discord bot")
bot_cmd_optional_named = bot_cmd_start.add_argument_group(
"optional named arguments"
)
Expand All @@ -18,7 +16,7 @@ def setup_bot_start(bot_subcommand):
def setup_add_bot_command(bot_cmd_commands_subcommand):
add_cmd = bot_cmd_commands_subcommand.add_parser(
"add",
description="Add a new slash command for your bot to use a customized model",
help="Add a new slash command for your bot to use a customized model",
)
add_cmd_required_named = add_cmd.add_argument_group("required named arguments")
add_cmd_optional_named = add_cmd.add_argument_group("optional named arguments")
Expand Down Expand Up @@ -101,7 +99,7 @@ def setup_add_bot_command(bot_cmd_commands_subcommand):

def setup_delete_bot_command(bot_cmd_commands_subcommand):
delete_cmd = bot_cmd_commands_subcommand.add_parser(
"delete", description="Delete a slash command from your bot"
"delete", help="Delete a slash command from your bot"
)
delete_cmd_required_named = delete_cmd.add_argument_group(
"required named arguments"
Expand All @@ -117,37 +115,11 @@ def setup_delete_bot_command(bot_cmd_commands_subcommand):
)


def setup_update_bot_token(config_subcommand):
config_bot_token = config_subcommand.add_parser(
"bot-token", description="Get or set your discord bot token"
)
config_bot_token_optional_named = config_bot_token.add_argument_group(
"optional named arguments"
)

config_bot_token_optional_named.add_argument(
"-t",
"--set-token",
type=str,
required=False,
dest="new_token",
help="A new Discord bot token to update the config with",
)


def setup_update_openai_key(config_subcommand):
config_openai_key = config_subcommand.add_parser(
"openai-key", description="Get or set your openaAI API key"
)
config_openai_key_optional_named = config_openai_key.add_argument_group(
"optional named arguments"
def setup_update_config(config_subcommand):
config_command = config_subcommand.add_parser(
"update", help="Update your DiscordAI config"
)

config_openai_key_optional_named.add_argument(
"-k",
"--set-key",
type=str,
required=False,
dest="new_key",
help="A new OpenAI API key to update the config with",
config_command.add_argument(
"key", help="The key to update: `DISCORD_BOT_TOKEN` | `OPENAI_API_KEY`"
)
config_command.add_argument("value", help="The new value for the key")

0 comments on commit 2e8da78

Please sign in to comment.