mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-15 10:10:12 +00:00
Refactor error handling slightly and don't use embeds for basic status/error messages. Closes #28.
This commit is contained in:
parent
869f686bd5
commit
8e504fa879
11 changed files with 74 additions and 81 deletions
|
|
@ -15,7 +15,7 @@ async def switch_member(ctx: CommandContext):
|
|||
system = await ctx.ensure_system()
|
||||
|
||||
if not ctx.has_next():
|
||||
return CommandError("You must pass at least one member name or ID to register a switch to.",
|
||||
raise CommandError("You must pass at least one member name or ID to register a switch to.",
|
||||
help=help.switch_register)
|
||||
|
||||
members: List[Member] = []
|
||||
|
|
@ -23,7 +23,7 @@ async def switch_member(ctx: CommandContext):
|
|||
# Find the member
|
||||
member = await utils.get_member_fuzzy(ctx.conn, system.id, member_name)
|
||||
if not member:
|
||||
return CommandError("Couldn't find member \"{}\".".format(member_name))
|
||||
raise CommandError("Couldn't find member \"{}\".".format(member_name))
|
||||
members.append(member)
|
||||
|
||||
# Compare requested switch IDs and existing fronter IDs to check for existing switches
|
||||
|
|
@ -32,12 +32,12 @@ async def switch_member(ctx: CommandContext):
|
|||
fronter_ids = (await pluralkit.utils.get_fronter_ids(ctx.conn, system.id))[0]
|
||||
if member_ids == fronter_ids:
|
||||
if len(members) == 1:
|
||||
return CommandError("{} is already fronting.".format(members[0].name))
|
||||
return CommandError("Members {} are already fronting.".format(", ".join([m.name for m in members])))
|
||||
raise CommandError("{} is already fronting.".format(members[0].name))
|
||||
raise CommandError("Members {} are already fronting.".format(", ".join([m.name for m in members])))
|
||||
|
||||
# Also make sure there aren't any duplicates
|
||||
if len(set(member_ids)) != len(member_ids):
|
||||
return CommandError("Duplicate members in member list.")
|
||||
raise CommandError("Duplicate members in member list.")
|
||||
|
||||
# Log the switch
|
||||
async with ctx.conn.transaction():
|
||||
|
|
@ -46,9 +46,9 @@ async def switch_member(ctx: CommandContext):
|
|||
await db.add_switch_member(ctx.conn, switch_id=switch_id, member_id=member.id)
|
||||
|
||||
if len(members) == 1:
|
||||
return CommandSuccess("Switch registered. Current fronter is now {}.".format(members[0].name))
|
||||
await ctx.reply_ok("Switch registered. Current fronter is now {}.".format(members[0].name))
|
||||
else:
|
||||
return CommandSuccess(
|
||||
await ctx.reply_ok(
|
||||
"Switch registered. Current fronters are now {}.".format(", ".join([m.name for m in members])))
|
||||
|
||||
|
||||
|
|
@ -58,17 +58,17 @@ async def switch_out(ctx: CommandContext):
|
|||
# Get current fronters
|
||||
fronters, _ = await pluralkit.utils.get_fronter_ids(ctx.conn, system_id=system.id)
|
||||
if not fronters:
|
||||
return CommandError("There's already no one in front.")
|
||||
raise CommandError("There's already no one in front.")
|
||||
|
||||
# Log it, and don't log any members
|
||||
await db.add_switch(ctx.conn, system_id=system.id)
|
||||
return CommandSuccess("Switch-out registered.")
|
||||
await ctx.reply_ok("Switch-out registered.")
|
||||
|
||||
|
||||
async def switch_move(ctx: CommandContext):
|
||||
system = await ctx.ensure_system()
|
||||
if not ctx.has_next():
|
||||
return CommandError("You must pass a time to move the switch to.", help=help.switch_move)
|
||||
raise CommandError("You must pass a time to move the switch to.", help=help.switch_move)
|
||||
|
||||
# Parse the time to move to
|
||||
new_time = dateparser.parse(ctx.remaining(), languages=["en"], settings={
|
||||
|
|
@ -76,18 +76,18 @@ async def switch_move(ctx: CommandContext):
|
|||
"RETURN_AS_TIMEZONE_AWARE": False
|
||||
})
|
||||
if not new_time:
|
||||
return CommandError("'{}' can't be parsed as a valid time.".format(ctx.remaining()), help=help.switch_move)
|
||||
raise CommandError("'{}' can't be parsed as a valid time.".format(ctx.remaining()), help=help.switch_move)
|
||||
|
||||
# Make sure the time isn't in the future
|
||||
if new_time > datetime.utcnow():
|
||||
return CommandError("Can't move switch to a time in the future.", help=help.switch_move)
|
||||
raise CommandError("Can't move switch to a time in the future.", help=help.switch_move)
|
||||
|
||||
# Make sure it all runs in a big transaction for atomicity
|
||||
async with ctx.conn.transaction():
|
||||
# Get the last two switches to make sure the switch to move isn't before the second-last switch
|
||||
last_two_switches = await pluralkit.utils.get_front_history(ctx.conn, system.id, count=2)
|
||||
if len(last_two_switches) == 0:
|
||||
return CommandError("There are no registered switches for this system.")
|
||||
raise CommandError("There are no registered switches for this system.")
|
||||
|
||||
last_timestamp, last_fronters = last_two_switches[0]
|
||||
if len(last_two_switches) > 1:
|
||||
|
|
@ -95,7 +95,7 @@ async def switch_move(ctx: CommandContext):
|
|||
|
||||
if new_time < second_last_timestamp:
|
||||
time_str = humanize.naturaltime(pluralkit.utils.fix_time(second_last_timestamp))
|
||||
return CommandError(
|
||||
raise CommandError(
|
||||
"Can't move switch to before last switch time ({}), as it would cause conflicts.".format(time_str))
|
||||
|
||||
# Display the confirmation message w/ humanized times
|
||||
|
|
@ -108,14 +108,14 @@ async def switch_move(ctx: CommandContext):
|
|||
# Confirm with user
|
||||
switch_confirm_message = "This will move the latest switch ({}) from {} ({}) to {} ({}). Is this OK?".format(members, last_absolute, last_relative, new_absolute, new_relative)
|
||||
if not await ctx.confirm_react(ctx.message.author, switch_confirm_message):
|
||||
return CommandError("Switch move cancelled.")
|
||||
raise CommandError("Switch move cancelled.")
|
||||
|
||||
# DB requires the actual switch ID which our utility method above doesn't return, do this manually
|
||||
switch_id = (await db.front_history(ctx.conn, system.id, count=1))[0]["id"]
|
||||
|
||||
# Change the switch in the DB
|
||||
await db.move_last_switch(ctx.conn, system.id, switch_id, new_time)
|
||||
return CommandSuccess("Switch moved.")
|
||||
await ctx.reply_ok("Switch moved.")
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue