From d3e3cb0abc682f7e7961394ad2bbfe46dbd4f080 Mon Sep 17 00:00:00 2001 From: spiral Date: Wed, 14 Apr 2021 13:56:41 +0100 Subject: [PATCH] feat: add avatar to member via attachment on 'pk;member new' command --- PluralKit.Bot/Commands/Member.cs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/PluralKit.Bot/Commands/Member.cs b/PluralKit.Bot/Commands/Member.cs index ffed8d3b..0ebf72c6 100644 --- a/PluralKit.Bot/Commands/Member.cs +++ b/PluralKit.Bot/Commands/Member.cs @@ -1,7 +1,9 @@ +using System; using System.Threading.Tasks; using System.Net; using System.Net.Http; using System.Web; +using System.Linq; using Dapper; @@ -53,13 +55,30 @@ namespace PluralKit.Bot // Create the member var member = await _repo.CreateMember(conn, ctx.System.Id, memberName); memberCount++; - + + // Try to match an image attached to the message + var avatarArg = ctx.Message.Attachments.FirstOrDefault(); + Exception imageMatchError = null; + if (avatarArg != null) + { + try { + await AvatarUtils.VerifyAvatarOrThrow(avatarArg.Url); + await _db.Execute(conn => _repo.UpdateMember(conn, member.Id, new MemberPatch { AvatarUrl = avatarArg.Url })); + } catch (Exception e) { + imageMatchError = e; + } + } + // Send confirmation and space hint await ctx.Reply($"{Emojis.Success} Member \"{memberName}\" (`{member.Hid}`) registered! Check out the getting started page for how to get a member up and running: https://pluralkit.me/start#create-a-member"); if (await _db.Execute(conn => conn.QuerySingleAsync("select has_private_members(@System)", new {System = ctx.System.Id}))) //if has private members await ctx.Reply($"{Emojis.Warn} This member is currently **public**. To change this, use `pk;member {member.Hid} private`."); - + if (avatarArg != null) + if (imageMatchError == null) + await ctx.Reply($"{Emojis.Success} Member avatar set to attached image.\n{Emojis.Warn} If you delete the message containing the attachment, the avatar will stop working."); + else + await ctx.Reply($"{Emojis.Error} Couldn't set avatar: {imageMatchError.Message}"); if (memberName.Contains(" ")) await ctx.Reply($"{Emojis.Note} Note that this member's name contains spaces. You will need to surround it with \"double quotes\" when using commands referring to it, or just use the member's 5-character ID (which is `{member.Hid}`)."); if (memberCount >= memberLimit)