diff --git a/PluralKit.Bot/Commands/CommandTree.cs b/PluralKit.Bot/Commands/CommandTree.cs index d7261b17..4aeccb7f 100644 --- a/PluralKit.Bot/Commands/CommandTree.cs +++ b/PluralKit.Bot/Commands/CommandTree.cs @@ -55,6 +55,7 @@ namespace PluralKit.Bot public static Command GroupRemove = new Command("group remove", "group remove [member 2] [member 3...]", "Removes one or more members from a group"); public static Command GroupPrivacy = new Command("group privacy", "group privacy ", "Changes a group's privacy settings"); public static Command GroupDelete = new Command("group delete", "group delete", "Deletes a group"); + public static Command GroupIcon = new Command("group icon", "group icon [url|@mention]", "Changes a group's icon"); public static Command Switch = new Command("switch", "switch [member 2] [member 3...]", "Registers a switch"); public static Command SwitchOut = new Command("switch out", "switch out", "Registers a switch with no members"); public static Command SwitchMove = new Command("switch move", "switch move ", "Moves the latest switch in time"); @@ -352,6 +353,8 @@ namespace PluralKit.Bot await ctx.Execute(GroupPrivacy, g => g.GroupPrivacy(ctx, target, PrivacyLevel.Private)); else if (ctx.Match("delete", "remove", "destroy", "erase", "yeet")) await ctx.Execute(GroupDelete, g => g.DeleteGroup(ctx, target)); + else if (ctx.Match("avatar", "picture", "icon", "image", "pic", "pfp")) + await ctx.Execute(GroupIcon, g => g.GroupIcon(ctx, target)); else if (!ctx.HasNext()) await ctx.Execute(GroupInfo, g => g.ShowGroupCard(ctx, target)); else diff --git a/PluralKit.Bot/Commands/Groups.cs b/PluralKit.Bot/Commands/Groups.cs index 7814a6a8..1f336ce2 100644 --- a/PluralKit.Bot/Commands/Groups.cs +++ b/PluralKit.Bot/Commands/Groups.cs @@ -103,6 +103,68 @@ namespace PluralKit.Bot } } + public async Task GroupIcon(Context ctx, PKGroup target) + { + async Task ClearIcon() + { + ctx.CheckOwnGroup(target); + + await _db.Execute(c => c.UpdateGroup(target.Id, new GroupPatch {Icon = null})); + await ctx.Reply($"{Emojis.Success} Group icon cleared."); + } + + async Task SetIcon(ParsedImage img) + { + ctx.CheckOwnGroup(target); + + if (img.Url.Length > Limits.MaxUriLength) + throw Errors.InvalidUrl(img.Url); + await AvatarUtils.VerifyAvatarOrThrow(img.Url); + + await _db.Execute(c => c.UpdateGroup(target.Id, new GroupPatch {Icon = img.Url})); + + var msg = img.Source switch + { + AvatarSource.User => $"{Emojis.Success} Group icon changed to {img.SourceUser?.Username}'s avatar!\n{Emojis.Warn} If {img.SourceUser?.Username} changes their avatar, the group icon will need to be re-set.", + AvatarSource.Url => $"{Emojis.Success} Group icon changed to the image at the given URL.", + AvatarSource.Attachment => $"{Emojis.Success} Group icon changed to attached image.\n{Emojis.Warn} If you delete the message containing the attachment, the group icon will stop working.", + _ => throw new ArgumentOutOfRangeException() + }; + + // The attachment's already right there, no need to preview it. + var hasEmbed = img.Source != AvatarSource.Attachment; + await (hasEmbed + ? ctx.Reply(msg, embed: new DiscordEmbedBuilder().WithImageUrl(img.Url).Build()) + : ctx.Reply(msg)); + } + + async Task ShowIcon() + { + if ((target.Icon?.Trim() ?? "").Length > 0) + { + var eb = new DiscordEmbedBuilder() + .WithTitle("Group icon") + .WithImageUrl(target.Icon); + + if (target.System == ctx.System?.Id) + { + eb.WithDescription($"To clear, use `pk;group {GroupReference(target)} icon -clear`."); + } + + await ctx.Reply(embed: eb.Build()); + } + else + throw new PKSyntaxError("This group does not have an icon set. Set one by attaching an image to this command, or by passing an image URL or @mention."); + } + + if (ctx.MatchClear()) + await ClearIcon(); + else if (await ctx.MatchImage() is {} img) + await SetIcon(img); + else + await ShowIcon(); + } + public async Task ListSystemGroups(Context ctx, PKSystem system) { if (system == null)