From 0869f94cdf7120ac6877fc8835e4903624f28ad1 Mon Sep 17 00:00:00 2001 From: Ske Date: Sun, 2 Sep 2018 19:41:14 +0200 Subject: [PATCH] Remove syntax error exception, add help pages to some errors --- src/pluralkit/bot/commands/__init__.py | 8 +------ src/pluralkit/bot/commands/member_commands.py | 21 +++++++++---------- .../bot/commands/message_commands.py | 6 +++--- src/pluralkit/bot/commands/misc_commands.py | 2 +- src/pluralkit/bot/commands/switch_commands.py | 10 ++++----- src/pluralkit/bot/commands/system_commands.py | 14 ++++++------- src/pluralkit/bot/help.py | 4 +++- 7 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/pluralkit/bot/commands/__init__.py b/src/pluralkit/bot/commands/__init__.py index 1cbe35b8..0fb141ad 100644 --- a/src/pluralkit/bot/commands/__init__.py +++ b/src/pluralkit/bot/commands/__init__.py @@ -12,9 +12,6 @@ logger = logging.getLogger("pluralkit.bot.commands") command_list = {} -class InvalidCommandSyntax(Exception): - pass - class NoSystemRegistered(Exception): pass @@ -55,9 +52,6 @@ def command(cmd, usage=None, description=None, category=None, system_required=Tr await client.send_message(message.channel, embed=embed) except NoSystemRegistered: await client.send_message(message.channel, embed=utils.make_error_embed("No system registered to this account. Use `pk;system new` to register one.")) - except InvalidCommandSyntax: - usage_str = "**Usage:** pk;{} {}".format(cmd, usage or "") - await client.send_message(message.channel, embed=utils.make_default_embed(usage_str)) except Exception: logger.exception("Exception while handling command {} (args={}, system={})".format(cmd, args, system.hid if system else "(none)")) @@ -71,7 +65,7 @@ def member_command(cmd, usage=None, description=None, category=None, system_only async def wrapper(ctx: CommandContext, args): # Return if no member param if len(args) == 0: - raise InvalidCommandSyntax() + return embeds.error("You must pass a member name or ID.") # System is allowed to be none if not system_only system_id = ctx.system.id if ctx.system else None diff --git a/src/pluralkit/bot/commands/member_commands.py b/src/pluralkit/bot/commands/member_commands.py index 2288a7ed..867e7906 100644 --- a/src/pluralkit/bot/commands/member_commands.py +++ b/src/pluralkit/bot/commands/member_commands.py @@ -1,11 +1,10 @@ -import logging import re from datetime import datetime from typing import List from urllib.parse import urlparse -from pluralkit.bot import utils, embeds from pluralkit.bot.commands import * +from pluralkit.bot import help logger = logging.getLogger("pluralkit.commands") @@ -16,7 +15,7 @@ async def member_info(ctx: MemberCommandContext, args: List[str]): @command(cmd="member new", usage="", description="Adds a new member to your system.", category="Member commands") async def new_member(ctx: MemberCommandContext, args: List[str]): if len(args) == 0: - raise InvalidCommandSyntax() + return embeds.error("You must pass a member name or ID.", help=help.add_member) name = " ".join(args) bounds_error = utils.bounds_check_member_name(name, ctx.system.tag) @@ -33,8 +32,8 @@ async def new_member(ctx: MemberCommandContext, args: List[str]): @member_command(cmd="member set", usage=" [value]", description="Edits a member property. Leave [value] blank to clear.", category="Member commands") async def member_set(ctx: MemberCommandContext, args: List[str]): - if len(args) == 0: - raise InvalidCommandSyntax() + if len(args) == 0: + return embeds.error("You must pass a property name to set.", help=help.edit_member) allowed_properties = ["name", "description", "color", "pronouns", "birthdate", "avatar"] db_properties = { @@ -48,7 +47,7 @@ async def member_set(ctx: MemberCommandContext, args: List[str]): prop = args[0] if prop not in allowed_properties: - return embeds.error("Unknown property {}. Allowed properties are {}.".format(prop, ", ".join(allowed_properties))) + return embeds.error("Unknown property {}. Allowed properties are {}.".format(prop, ", ".join(allowed_properties)), help=help.edit_member) if len(args) >= 2: value = " ".join(args[1:]) @@ -90,11 +89,11 @@ async def member_set(ctx: MemberCommandContext, args: List[str]): if u.scheme in ["http", "https"] and u.netloc and u.path: value = value else: - return embeds.error("Invalid URL.") + return embeds.error("Invalid image URL.") else: # Can't clear member name if prop == "name": - return embeds.error("Can't clear member name.") + return embeds.error("You can't clear the member name.") # Clear from DB value = None @@ -117,10 +116,10 @@ async def member_proxy(ctx: MemberCommandContext, args: List[str]): # Sanity checking example = " ".join(args) if "text" not in example: - return embeds.error("Example proxy message must contain the string 'text'.") + return embeds.error("Example proxy message must contain the string 'text'.", help=help.member_proxy) if example.count("text") != 1: - return embeds.error("Example proxy message must contain the string 'text' exactly once.") + return embeds.error("Example proxy message must contain the string 'text' exactly once.", help=help.member_proxy) # Extract prefix and suffix prefix = example[:example.index("text")].strip() @@ -147,4 +146,4 @@ async def member_delete(ctx: MemberCommandContext, args: List[str]): await db.delete_member(ctx.conn, member_id=ctx.member.id) return embeds.success("Member deleted.") else: - return embeds.success("Member deletion cancelled.") \ No newline at end of file + return embeds.error("Member deletion cancelled.") \ No newline at end of file diff --git a/src/pluralkit/bot/commands/message_commands.py b/src/pluralkit/bot/commands/message_commands.py index 4a597fd4..9570cc07 100644 --- a/src/pluralkit/bot/commands/message_commands.py +++ b/src/pluralkit/bot/commands/message_commands.py @@ -1,7 +1,7 @@ import logging from typing import List -from pluralkit.bot import utils, embeds +from pluralkit.bot import utils, embeds, help from pluralkit.bot.commands import * logger = logging.getLogger("pluralkit.commands") @@ -11,12 +11,12 @@ logger = logging.getLogger("pluralkit.commands") category="Message commands", system_required=False) async def message_info(ctx: CommandContext, args: List[str]): if len(args) == 0: - raise InvalidCommandSyntax() + return embeds.error("You must pass a message ID.", help=help.message_lookup) try: mid = int(args[0]) except ValueError: - raise InvalidCommandSyntax() + return embeds.error("You must pass a valid number as a message ID.", help=help.message_lookup) # Find the message in the DB message = await db.get_message(ctx.conn, str(mid)) diff --git a/src/pluralkit/bot/commands/misc_commands.py b/src/pluralkit/bot/commands/misc_commands.py index 3204d4b1..847b87ab 100644 --- a/src/pluralkit/bot/commands/misc_commands.py +++ b/src/pluralkit/bot/commands/misc_commands.py @@ -28,7 +28,7 @@ async def show_help(ctx: CommandContext, args: List[str]): else: embed.description = text else: - raise InvalidCommandSyntax() + return embeds.error("Unknown help page '{}'.".format(category)) return embed diff --git a/src/pluralkit/bot/commands/switch_commands.py b/src/pluralkit/bot/commands/switch_commands.py index d87e855f..1b77af14 100644 --- a/src/pluralkit/bot/commands/switch_commands.py +++ b/src/pluralkit/bot/commands/switch_commands.py @@ -7,7 +7,7 @@ import humanize import pluralkit.utils from pluralkit import Member -from pluralkit.bot import utils, embeds +from pluralkit.bot import utils, embeds, help from pluralkit.bot.commands import * logger = logging.getLogger("pluralkit.commands") @@ -15,7 +15,7 @@ logger = logging.getLogger("pluralkit.commands") @command(cmd="switch", usage=" [name|id]...", description="Registers a switch and changes the current fronter.", category="Switching commands") async def switch_member(ctx: MemberCommandContext, args: List[str]): if len(args) == 0: - raise InvalidCommandSyntax() + return embeds.error("You must pass at least one member name or ID to register a switch to.", help=help.switch_register) members: List[Member] = [] for member_name in args: @@ -36,7 +36,7 @@ async def switch_member(ctx: MemberCommandContext, args: List[str]): # Also make sure there aren't any duplicates if len(set(member_ids)) != len(member_ids): - return embeds.error("Duplicate members in switch list.") + return embeds.error("Duplicate members in member list.") # Log the switch async with ctx.conn.transaction(): @@ -54,7 +54,7 @@ async def switch_out(ctx: MemberCommandContext, args: List[str]): # Get current fronters fronters, _ = await pluralkit.utils.get_fronter_ids(ctx.conn, system_id=ctx.system.id) if not fronters: - raise embeds.error("There's already no one in front.") + return embeds.error("There's already no one in front.") # Log it, and don't log any members await db.add_switch(ctx.conn, system_id=ctx.system.id) @@ -63,7 +63,7 @@ async def switch_out(ctx: MemberCommandContext, args: List[str]): @command(cmd="switch move", usage="