PluralKit/PluralKit.Bot/Commands/MemberAvatar.cs

286 lines
No EOL
13 KiB
C#

#nullable enable
using Myriad.Builders;
using Myriad.Types;
using PluralKit.Core;
namespace PluralKit.Bot;
public class MemberAvatar
{
private readonly HttpClient _client;
private readonly AvatarHostingService _avatarHosting;
public MemberAvatar(HttpClient client, AvatarHostingService avatarHosting)
{
_client = client;
_avatarHosting = avatarHosting;
}
private async Task AvatarClear(MemberAvatarLocation location, Context ctx, PKMember target, MemberGuildSettings? mgs, bool confirmYes)
{
ctx.CheckSystem().CheckOwnMember(target);
await ctx.ConfirmClear("this member's " + location.Name(), confirmYes);
await UpdateAvatar(location, ctx, target, null);
if (location == MemberAvatarLocation.Server)
{
if (target.AvatarUrl != null)
await ctx.Reply(
$"{Emojis.Success} Member server avatar cleared. This member will now use the global avatar in this server (**{ctx.Guild.Name}**).");
else
await ctx.Reply($"{Emojis.Success} Member server avatar cleared. This member now has no avatar.");
}
else if (location == MemberAvatarLocation.MemberWebhook)
{
if (mgs?.AvatarUrl != null)
await ctx.Reply(
$"{Emojis.Success} Member proxy avatar cleared. Note that this member has a server-specific avatar set here, type `{ctx.DefaultPrefix}member {target.Reference(ctx)} serveravatar clear` if you wish to clear that too.");
else
await ctx.Reply($"{Emojis.Success} Member proxy avatar cleared. This member will now use the main avatar for proxied messages.");
}
else
{
if (mgs?.AvatarUrl != null)
await ctx.Reply(
$"{Emojis.Success} Member avatar cleared. Note that this member has a server-specific avatar set here, type `{ctx.DefaultPrefix}member {target.Reference(ctx)} serveravatar clear` if you wish to clear that too.");
else
await ctx.Reply($"{Emojis.Success} Member avatar cleared.");
}
}
private async Task AvatarShow(MemberAvatarLocation location, Context ctx, PKMember target,
MemberGuildSettings? guildData, ReplyFormat format)
{
// todo: this privacy code is really confusing
// for now, we skip privacy flag/config parsing for this, but it would be good to fix that at some point
var currentValue = location switch
{
MemberAvatarLocation.Server => guildData?.AvatarUrl,
MemberAvatarLocation.MemberWebhook => target.WebhookAvatarUrl,
MemberAvatarLocation.Member => target.AvatarUrl,
_ => throw new ArgumentOutOfRangeException(nameof(location))
};
var canAccess = location == MemberAvatarLocation.Server ||
target.AvatarPrivacy.CanAccess(ctx.DirectLookupContextFor(target.System));
if (string.IsNullOrEmpty(currentValue) || !canAccess)
{
if (location == MemberAvatarLocation.Member)
{
if (target.System == ctx.System?.Id)
throw new PKSyntaxError(
"This member does not have an avatar set. Set one by attaching an image to this command, or by passing an image URL or @mention.");
throw new PKError("This member does not have an avatar set.");
}
if (location == MemberAvatarLocation.MemberWebhook)
throw new PKError(
$"This member does not have a proxy avatar set. Type `{ctx.DefaultPrefix}member {target.Reference(ctx)} avatar` to see their global avatar.");
if (location == MemberAvatarLocation.Server)
throw new PKError(
$"This member does not have a server avatar set. Type `{ctx.DefaultPrefix}member {target.Reference(ctx)} avatar` to see their global avatar.");
}
var field = location.Name();
if (location == MemberAvatarLocation.Server)
field += $" (for {ctx.Guild.Name})";
if (format == ReplyFormat.Raw)
{
await ctx.Reply($"`{currentValue?.TryGetCleanCdnUrl()}`");
}
else if (format == ReplyFormat.Plaintext)
{
var eb = new EmbedBuilder()
.Description($"Showing {field} link for member {target.NameFor(ctx)} (`{target.DisplayHid(ctx.Config)}`)");
await ctx.Reply($"<{currentValue?.TryGetCleanCdnUrl()}>", embed: eb.Build());
return;
}
else if (format == ReplyFormat.Standard)
{
var eb = new EmbedBuilder()
.Title($"{target.NameFor(ctx)}'s {field}")
.Image(new Embed.EmbedImage(currentValue?.TryGetCleanCdnUrl()));
if (target.System == ctx.System?.Id)
eb.Description($"To clear, use `{ctx.DefaultPrefix}member {target.Reference(ctx)} {location.Command()} clear`.");
await ctx.Reply(embed: eb.Build());
}
else throw new PKError("Format Not Recognized");
}
private async Task AvatarChange(MemberAvatarLocation location, Context ctx, PKMember target,
MemberGuildSettings? guildData, ParsedImage avatar)
{
ctx.CheckSystem().CheckOwnMember(target);
avatar = await _avatarHosting.TryRehostImage(avatar, AvatarHostingService.RehostedImageType.Avatar, ctx.Author.Id, ctx.System);
await _avatarHosting.VerifyAvatarOrThrow(avatar.Url);
await UpdateAvatar(location, ctx, target, avatar.CleanUrl ?? avatar.Url);
await PrintResponse(location, ctx, target, avatar, guildData);
}
private Task<MemberGuildSettings> GetServerAvatarGuildData(Context ctx, PKMember target)
{
ctx.CheckGuildContext();
return ctx.Repository.GetMemberGuild(ctx.Guild.Id, target.Id);
}
private async Task<MemberGuildSettings?> GetAvatarGuildData(Context ctx, PKMember target)
{
return ctx.Guild != null
? await ctx.Repository.GetMemberGuild(ctx.Guild.Id, target.Id)
: null;
}
private async Task<MemberGuildSettings?> GetWebhookAvatarGuildData(Context ctx, PKMember target)
{
return ctx.Guild != null
? await ctx.Repository.GetMemberGuild(ctx.Guild.Id, target.Id)
: null;
}
public async Task ShowServerAvatar(Context ctx, PKMember target, ReplyFormat format)
{
var guildData = await GetServerAvatarGuildData(ctx, target);
await AvatarShow(MemberAvatarLocation.Server, ctx, target, guildData, format);
}
public async Task ClearServerAvatar(Context ctx, PKMember target, bool confirmYes)
{
var guildData = await GetServerAvatarGuildData(ctx, target);
await AvatarClear(MemberAvatarLocation.Server, ctx, target, guildData, confirmYes);
}
public async Task ChangeServerAvatar(Context ctx, PKMember target, ParsedImage avatar)
{
var guildData = await GetServerAvatarGuildData(ctx, target);
await AvatarChange(MemberAvatarLocation.Server, ctx, target, guildData, avatar);
}
public async Task ShowAvatar(Context ctx, PKMember target, ReplyFormat format)
{
var guildData = await GetAvatarGuildData(ctx, target);
await AvatarShow(MemberAvatarLocation.Member, ctx, target, guildData, format);
}
public async Task ClearAvatar(Context ctx, PKMember target, bool confirmYes)
{
var guildData = await GetAvatarGuildData(ctx, target);
await AvatarClear(MemberAvatarLocation.Member, ctx, target, guildData, confirmYes);
}
public async Task ChangeAvatar(Context ctx, PKMember target, ParsedImage avatar)
{
var guildData = await GetAvatarGuildData(ctx, target);
await AvatarChange(MemberAvatarLocation.Member, ctx, target, guildData, avatar);
}
public async Task ShowWebhookAvatar(Context ctx, PKMember target, ReplyFormat format)
{
var guildData = await GetWebhookAvatarGuildData(ctx, target);
await AvatarShow(MemberAvatarLocation.MemberWebhook, ctx, target, guildData, format);
}
public async Task ClearWebhookAvatar(Context ctx, PKMember target, bool confirmYes)
{
var guildData = await GetWebhookAvatarGuildData(ctx, target);
await AvatarClear(MemberAvatarLocation.MemberWebhook, ctx, target, guildData, confirmYes);
}
public async Task ChangeWebhookAvatar(Context ctx, PKMember target, ParsedImage avatar)
{
var guildData = await GetWebhookAvatarGuildData(ctx, target);
await AvatarChange(MemberAvatarLocation.MemberWebhook, ctx, target, guildData, avatar);
}
private Task PrintResponse(MemberAvatarLocation location, Context ctx, PKMember target, ParsedImage avatar,
MemberGuildSettings? targetGuildData)
{
var serverFrag = location switch
{
MemberAvatarLocation.Server =>
$" This avatar will now be used when proxying in this server (**{ctx.Guild.Name}**).",
MemberAvatarLocation.MemberWebhook when targetGuildData?.AvatarUrl != null =>
$" This avatar will now be used for this member's proxied messages, instead of their main avatar.\n{Emojis.Note} Note that this member *also* has a server-specific avatar set in this server (**{ctx.Guild.Name}**), and thus changing the global avatar will have no effect here.",
MemberAvatarLocation.MemberWebhook =>
$" This avatar will now be used for this member's proxied messages, instead of their main avatar.",
MemberAvatarLocation.Member when (targetGuildData?.AvatarUrl != null && target.WebhookAvatarUrl != null) =>
$"\n{Emojis.Note} Note that this member *also* has a server-specific avatar set in this server (**{ctx.Guild.Name}**), and thus changing the global avatar will have no effect here." +
$"\n{Emojis.Note} Note that this member *also* has a proxy avatar set, and thus the global avatar will also have no effect on proxied messages in servers without server-specific avatars.",
MemberAvatarLocation.Member when targetGuildData?.AvatarUrl != null =>
$"\n{Emojis.Note} Note that this member *also* has a server-specific avatar set in this server (**{ctx.Guild.Name}**), and thus changing the global avatar will have no effect here.",
MemberAvatarLocation.Member when target.WebhookAvatarUrl != null =>
$"\n{Emojis.Note} Note that this member *also* has a proxy avatar set, and thus changing the global avatar will have no effect on proxied messages.",
_ => ""
};
var msg = avatar.Source switch
{
AvatarSource.User =>
$"{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 && avatar.Source != AvatarSource.HostedCdn;
return hasEmbed
? ctx.Reply(msg, new EmbedBuilder().Image(new Embed.EmbedImage(avatar.Url)).Build())
: ctx.Reply(msg);
}
private Task UpdateAvatar(MemberAvatarLocation location, Context ctx, PKMember target, string? url)
{
switch (location)
{
case MemberAvatarLocation.Server:
return ctx.Repository.UpdateMemberGuild(target.Id, ctx.Guild.Id, new MemberGuildPatch { AvatarUrl = url });
case MemberAvatarLocation.Member:
return ctx.Repository.UpdateMember(target.Id, new MemberPatch { AvatarUrl = url });
case MemberAvatarLocation.MemberWebhook:
return ctx.Repository.UpdateMember(target.Id, new MemberPatch { WebhookAvatarUrl = url });
default:
throw new ArgumentOutOfRangeException($"Unknown avatar location {location}");
}
}
}
internal enum MemberAvatarLocation
{
Member,
MemberWebhook,
Server,
}
internal static class MemberAvatarLocationExt
{
public static string Name(this MemberAvatarLocation location)
{
return location switch
{
MemberAvatarLocation.Server => "server avatar",
MemberAvatarLocation.MemberWebhook => "proxy avatar",
MemberAvatarLocation.Member => "avatar",
_ => throw new ArgumentOutOfRangeException(nameof(location))
};
}
public static string Command(this MemberAvatarLocation location)
{
return location switch
{
MemberAvatarLocation.Server => "serveravatar",
MemberAvatarLocation.MemberWebhook => "proxyavatar",
MemberAvatarLocation.Member => "avatar",
_ => throw new ArgumentOutOfRangeException(nameof(location))
};
}
}