fix(bot): add AvatarSource.HostedCdn

This commit is contained in:
Iris System 2024-02-14 04:41:02 +13:00
parent 5499a20a2c
commit 6547f1ca20
7 changed files with 21 additions and 11 deletions

View file

@ -61,5 +61,6 @@ public enum AvatarSource
{
Url,
User,
Attachment
Attachment,
HostedCdn
}

View file

@ -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));

View file

@ -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(" "))

View file

@ -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);

View file

@ -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));

View file

@ -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));

View file

@ -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;