From 6547f1ca206d2d2fa4b35c62ec69165c0e05db8b Mon Sep 17 00:00:00 2001 From: Iris System Date: Wed, 14 Feb 2024 04:41:02 +1300 Subject: [PATCH] fix(bot): add AvatarSource.HostedCdn --- PluralKit.Bot/CommandSystem/Context/ContextAvatarExt.cs | 3 ++- PluralKit.Bot/Commands/Groups.cs | 6 ++++-- PluralKit.Bot/Commands/Member.cs | 5 +++-- PluralKit.Bot/Commands/MemberAvatar.cs | 4 +++- PluralKit.Bot/Commands/MemberEdit.cs | 3 ++- PluralKit.Bot/Commands/SystemEdit.cs | 9 ++++++--- PluralKit.Bot/Services/AvatarHostingService.cs | 2 +- 7 files changed, 21 insertions(+), 11 deletions(-) diff --git a/PluralKit.Bot/CommandSystem/Context/ContextAvatarExt.cs b/PluralKit.Bot/CommandSystem/Context/ContextAvatarExt.cs index 83bb1028..ca2ff912 100644 --- a/PluralKit.Bot/CommandSystem/Context/ContextAvatarExt.cs +++ b/PluralKit.Bot/CommandSystem/Context/ContextAvatarExt.cs @@ -61,5 +61,6 @@ public enum AvatarSource { Url, User, - Attachment + Attachment, + HostedCdn } \ No newline at end of file diff --git a/PluralKit.Bot/Commands/Groups.cs b/PluralKit.Bot/Commands/Groups.cs index dad4190c..cbe128dc 100644 --- a/PluralKit.Bot/Commands/Groups.cs +++ b/PluralKit.Bot/Commands/Groups.cs @@ -273,13 +273,14 @@ public class Groups 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.HostedCdn => $"{Emojis.Success} Group icon changed to attached image.", 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; + var hasEmbed = img.Source != AvatarSource.Attachment && img.Source != AvatarSource.HostedCdn; await (hasEmbed ? ctx.Reply(msg, new EmbedBuilder().Image(new Embed.EmbedImage(img.Url)).Build()) : ctx.Reply(msg)); @@ -337,6 +338,7 @@ public class Groups var msg = img.Source switch { AvatarSource.Url => $"{Emojis.Success} Group banner image changed to the image at the given URL.", + AvatarSource.HostedCdn => $"{Emojis.Success} Group banner image changed to attached image.", AvatarSource.Attachment => $"{Emojis.Success} Group banner image changed to attached image.\n{Emojis.Warn} If you delete the message containing the attachment, the banner image will stop working.", AvatarSource.User => throw new PKError("Cannot set a banner image to an user's avatar."), @@ -344,7 +346,7 @@ public class Groups }; // The attachment's already right there, no need to preview it. - var hasEmbed = img.Source != AvatarSource.Attachment; + var hasEmbed = img.Source != AvatarSource.Attachment && img.Source != AvatarSource.HostedCdn; await (hasEmbed ? ctx.Reply(msg, new EmbedBuilder().Image(new Embed.EmbedImage(img.Url)).Build()) : ctx.Reply(msg)); diff --git a/PluralKit.Bot/Commands/Member.cs b/PluralKit.Bot/Commands/Member.cs index dad5e1cc..14055127 100644 --- a/PluralKit.Bot/Commands/Member.cs +++ b/PluralKit.Bot/Commands/Member.cs @@ -69,13 +69,14 @@ public class Member // Try to match an image attached to the message var avatarArg = ctx.Message.Attachments.FirstOrDefault(); Exception imageMatchError = null; + ParsedImage img = new(); if (avatarArg != null) try { // XXX: discord attachment URLs are unable to be validated without their query params // keep both the URL with query (for validation) and the clean URL (for storage) around var uriBuilder = new UriBuilder(avatarArg.ProxyUrl); - ParsedImage img = new ParsedImage { Url = uriBuilder.Uri.AbsoluteUri, Source = AvatarSource.Attachment }; + img = new ParsedImage { Url = uriBuilder.Uri.AbsoluteUri, Source = AvatarSource.Attachment }; uriBuilder.Query = ""; img.CleanUrl = uriBuilder.Uri.AbsoluteUri; @@ -109,7 +110,7 @@ public class Member 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."); + $"{Emojis.Success} Member avatar set to attached image." + (img.Source == AvatarSource.Attachment ? $"\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(" ")) diff --git a/PluralKit.Bot/Commands/MemberAvatar.cs b/PluralKit.Bot/Commands/MemberAvatar.cs index a0654a25..f1e012b8 100644 --- a/PluralKit.Bot/Commands/MemberAvatar.cs +++ b/PluralKit.Bot/Commands/MemberAvatar.cs @@ -174,13 +174,15 @@ public class MemberAvatar $"{Emojis.Success} Member {location.Name()} changed to {avatar.SourceUser?.Username}'s avatar!{serverFrag}\n{Emojis.Warn} If {avatar.SourceUser?.Username} changes their avatar, the member's avatar will need to be re-set.", AvatarSource.Url => $"{Emojis.Success} Member {location.Name()} changed to the image at the given URL.{serverFrag}", + AvatarSource.HostedCdn => + $"{Emojis.Success} Member {location.Name()} changed to attached image.{serverFrag}", AvatarSource.Attachment => $"{Emojis.Success} Member {location.Name()} changed to attached image.{serverFrag}\n{Emojis.Warn} If you delete the message containing the attachment, the avatar will stop working.", _ => throw new ArgumentOutOfRangeException() }; // The attachment's already right there, no need to preview it. - var hasEmbed = avatar.Source != AvatarSource.Attachment; + var hasEmbed = avatar.Source != AvatarSource.Attachment && avatar.Source != AvatarSource.HostedCdn; return hasEmbed ? ctx.Reply(msg, new EmbedBuilder().Image(new Embed.EmbedImage(avatar.Url)).Build()) : ctx.Reply(msg); diff --git a/PluralKit.Bot/Commands/MemberEdit.cs b/PluralKit.Bot/Commands/MemberEdit.cs index d2e943b3..328fdc74 100644 --- a/PluralKit.Bot/Commands/MemberEdit.cs +++ b/PluralKit.Bot/Commands/MemberEdit.cs @@ -190,6 +190,7 @@ public class MemberEdit var msg = img.Source switch { AvatarSource.Url => $"{Emojis.Success} Member banner image changed to the image at the given URL.", + AvatarSource.HostedCdn => $"{Emojis.Success} Member banner image changed to attached image.", AvatarSource.Attachment => $"{Emojis.Success} Member banner image changed to attached image.\n{Emojis.Warn} If you delete the message containing the attachment, the banner image will stop working.", AvatarSource.User => throw new PKError("Cannot set a banner image to an user's avatar."), @@ -197,7 +198,7 @@ public class MemberEdit }; // The attachment's already right there, no need to preview it. - var hasEmbed = img.Source != AvatarSource.Attachment; + var hasEmbed = img.Source != AvatarSource.Attachment && img.Source != AvatarSource.HostedCdn; await (hasEmbed ? ctx.Reply(msg, new EmbedBuilder().Image(new Embed.EmbedImage(img.Url)).Build()) : ctx.Reply(msg)); diff --git a/PluralKit.Bot/Commands/SystemEdit.cs b/PluralKit.Bot/Commands/SystemEdit.cs index 243ccaa6..30617f64 100644 --- a/PluralKit.Bot/Commands/SystemEdit.cs +++ b/PluralKit.Bot/Commands/SystemEdit.cs @@ -485,13 +485,14 @@ public class SystemEdit AvatarSource.User => $"{Emojis.Success} System icon changed to {img.SourceUser?.Username}'s avatar!\n{Emojis.Warn} If {img.SourceUser?.Username} changes their avatar, the system icon will need to be re-set.", AvatarSource.Url => $"{Emojis.Success} System icon changed to the image at the given URL.", + AvatarSource.HostedCdn => $"{Emojis.Success} System icon changed to attached image.", AvatarSource.Attachment => $"{Emojis.Success} System icon changed to attached image.\n{Emojis.Warn} If you delete the message containing the attachment, the system icon will stop working.", _ => throw new ArgumentOutOfRangeException() }; // The attachment's already right there, no need to preview it. - var hasEmbed = img.Source != AvatarSource.Attachment; + var hasEmbed = img.Source != AvatarSource.Attachment && img.Source != AvatarSource.HostedCdn; await (hasEmbed ? ctx.Reply(msg, new EmbedBuilder().Image(new Embed.EmbedImage(img.Url)).Build()) : ctx.Reply(msg)); @@ -555,13 +556,14 @@ public class SystemEdit $"{Emojis.Success} System icon for this server changed to {img.SourceUser?.Username}'s avatar! It will now be used for anything that uses system avatar in this server.\n{Emojis.Warn} If {img.SourceUser?.Username} changes their avatar, the system icon for this server will need to be re-set.", AvatarSource.Url => $"{Emojis.Success} System icon for this server changed to the image at the given URL. It will now be used for anything that uses system avatar in this server.", + AvatarSource.HostedCdn => $"{Emojis.Success} System icon for this server changed to attached image.", AvatarSource.Attachment => $"{Emojis.Success} System icon for this server changed to attached image. It will now be used for anything that uses system avatar in this server.\n{Emojis.Warn} If you delete the message containing the attachment, the system icon for this server will stop working.", _ => throw new ArgumentOutOfRangeException() }; // The attachment's already right there, no need to preview it. - var hasEmbed = img.Source != AvatarSource.Attachment; + var hasEmbed = img.Source != AvatarSource.Attachment && img.Source != AvatarSource.HostedCdn; await (hasEmbed ? ctx.Reply(msg, new EmbedBuilder().Image(new Embed.EmbedImage(img.Url)).Build()) : ctx.Reply(msg)); @@ -650,6 +652,7 @@ public class SystemEdit var msg = img.Source switch { AvatarSource.Url => $"{Emojis.Success} System banner image changed to the image at the given URL.", + AvatarSource.HostedCdn => $"{Emojis.Success} System banner image changed to attached image.", AvatarSource.Attachment => $"{Emojis.Success} System banner image changed to attached image.\n{Emojis.Warn} If you delete the message containing the attachment, the banner image will stop working.", AvatarSource.User => throw new PKError("Cannot set a banner image to an user's avatar."), @@ -657,7 +660,7 @@ public class SystemEdit }; // The attachment's already right there, no need to preview it. - var hasEmbed = img.Source != AvatarSource.Attachment; + var hasEmbed = img.Source != AvatarSource.Attachment && img.Source != AvatarSource.HostedCdn; await (hasEmbed ? ctx.Reply(msg, new EmbedBuilder().Image(new Embed.EmbedImage(img.Url)).Build()) : ctx.Reply(msg)); diff --git a/PluralKit.Bot/Services/AvatarHostingService.cs b/PluralKit.Bot/Services/AvatarHostingService.cs index 68a512ee..799b5eb1 100644 --- a/PluralKit.Bot/Services/AvatarHostingService.cs +++ b/PluralKit.Bot/Services/AvatarHostingService.cs @@ -20,7 +20,7 @@ public class AvatarHostingService if (uploaded != null) { // todo: make new image type called Cdn? - return new ParsedImage { Url = uploaded, Source = AvatarSource.Url }; + return new ParsedImage { Url = uploaded, Source = AvatarSource.HostedCdn }; } return input;