From 64fa1f4e3c0f6331f76a19917f511ff589995ea0 Mon Sep 17 00:00:00 2001 From: Ske Date: Thu, 12 Jul 2018 01:16:07 +0200 Subject: [PATCH] Add avatar preview on set --- bot/pluralkit/commands.py | 28 ++++++++++++++++---------- bot/pluralkit/utils.py | 42 +++++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/bot/pluralkit/commands.py b/bot/pluralkit/commands.py index 28f5274c..1f2d5f4f 100644 --- a/bot/pluralkit/commands.py +++ b/bot/pluralkit/commands.py @@ -6,8 +6,7 @@ import discord from pluralkit import db from pluralkit.bot import client, logger -from pluralkit.utils import command, generate_hid, generate_member_info_card, generate_system_info_card, member_command, parse_mention, text_input, get_system_fuzzy, get_member_fuzzy, command_map - +from pluralkit.utils import command, generate_hid, generate_member_info_card, generate_system_info_card, member_command, parse_mention, text_input, get_system_fuzzy, get_member_fuzzy, command_map, make_default_embed @command(cmd="pk;system", subcommand=None, description="Shows information about your system.") async def this_system_info(conn, message, args): @@ -316,7 +315,12 @@ async def member_avatar(conn, message, member, args): async with conn.transaction(): await db.update_member_field(conn, member_id=member["id"], field="avatar_url", value=avatar_url) - return True, "Avatar set." if avatar_url else "Avatar cleared." + + # Add the avatar you just set into the success embed + if not avatar_url: + return True, "Avatar cleared." + else: + return True, make_default_embed("Avatar set.").set_image(url=avatar_url) @member_command(cmd="pk;member", subcommand="proxy", usage="[example]", description="Updates a member's proxy settings. Needs an \"example\" proxied message containing the string \"text\" (eg. [text], |text|, etc).") @@ -394,23 +398,25 @@ async def message_info(conn, message, args): return True -@command(cmd="pk;help", subcommand=None, usage="[system|member|message]", description="Shows this help message.") -async def show_help(conn, message, args): +def make_help(cmds): embed = discord.Embed() embed.colour = discord.Colour.blue() embed.title = "PluralKit Help" embed.set_footer( text="<> denotes mandatory arguments, [] denotes optional arguments") + for cmd, subcommands in cmds: + for subcmd, (_, usage, description) in subcommands.items(): + embed.add_field(name="{} {} {}".format( + cmd, subcmd or "", usage or ""), value=description, inline=False) + return embed + +@command(cmd="pk;help", subcommand=None, usage="[system|member|message]", description="Shows this help message.") +async def show_help(conn, message, args): if len(args) > 0 and ("pk;" + args[0]) in command_map: cmds = ["", ("pk;" + args[0], command_map["pk;" + args[0]])] else: cmds = command_map.items() - for cmd, subcommands in cmds: - for subcmd, (_, usage, description) in subcommands.items(): - embed.add_field(name="{} {} {}".format( - cmd, subcmd or "", usage or ""), value=description, inline=False) - - await client.send_message(message.channel, embed=embed) + await client.send_message(message.channel, embed=make_help(cmds)) return True diff --git a/bot/pluralkit/utils.py b/bot/pluralkit/utils.py index bae1851d..f2918738 100644 --- a/bot/pluralkit/utils.py +++ b/bot/pluralkit/utils.py @@ -1,6 +1,7 @@ import random import re import string +import time import asyncio import asyncpg @@ -59,6 +60,18 @@ async def get_member_fuzzy(conn, system_id: int, key: str, system_only=True) -> if member is not None: return member +def make_default_embed(message): + embed = discord.Embed() + embed.colour = discord.Colour.blue() + embed.description = message + return embed + +def make_error_embed(message): + embed = discord.Embed() + embed.colour = discord.Colour.dark_red() + embed.description = message + return embed + command_map = {} # Command wrapper @@ -69,7 +82,10 @@ command_map = {} def command(cmd, subcommand, usage=None, description=None): def wrap(func): async def wrapper(conn, message, args): + before = time.perf_counter() res = await func(conn, message, args) + after = time.perf_counter() + time_ms = (after - before) * 1000 if res is not None: if not isinstance(res, tuple): @@ -79,25 +95,21 @@ def command(cmd, subcommand, usage=None, description=None): if not success and not msg: # Failure, no message, print usage - usage_embed = discord.Embed() - usage_embed.colour = discord.Colour.blue() - usage_embed.add_field( - name="Usage", value=usage, inline=False) - - await client.send_message(message.channel, embed=usage_embed) + usage_str = "**Usage:** {} {} {}".format(cmd, subcommand or "", usage or "") + await client.send_message(message.channel, embed=make_default_embed(usage_str)) elif not success: # Failure, print message - error_embed = discord.Embed() - error_embed.colour = discord.Colour.dark_red() - error_embed.description = msg - await client.send_message(message.channel, embed=error_embed) + embed = msg if isinstance(msg, discord.Embed) else make_error_embed(msg) + # embed.set_footer(text="{:.02f} ms".format(time_ms)) + await client.send_message(message.channel, embed=embed) elif msg: # Success, print message - success_embed = discord.Embed() - success_embed.colour = discord.Colour.blue() - success_embed.description = msg - await client.send_message(message.channel, embed=success_embed) + embed = msg if isinstance(msg, discord.Embed) else make_default_embed(msg) + # embed.set_footer(text="{:.02f} ms".format(time_ms)) + await client.send_message(message.channel, embed=embed) # Success, don't print anything + + # Put command in map if cmd not in command_map: command_map[cmd] = {} if subcommand not in command_map[cmd]: @@ -133,7 +145,7 @@ def member_command(cmd, subcommand, usage=None, description=None, system_only=Tr return False, "Can't find member \"{}\".".format(args[0]) return await func(conn, message, member, args[1:]) - return command(cmd=cmd, subcommand=subcommand, usage=usage, description=description)(wrapper) + return command(cmd=cmd, subcommand=subcommand, usage=" {}".format(usage or ""), description=description)(wrapper) return wrap