From afb5f0352889c82d68893ec1a3f681ea83613c31 Mon Sep 17 00:00:00 2001 From: sabihoshi Date: Wed, 29 Dec 2021 22:22:12 +0800 Subject: [PATCH] feat: Use DominantColor in Avatar command --- Zhongli.Data/Models/Discord/Context.cs | 3 ++ .../Models/Discord/InteractionContext.cs | 32 +++++++++++++------ Zhongli.Services/Moderation/UserService.cs | 14 +++++--- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/Zhongli.Data/Models/Discord/Context.cs b/Zhongli.Data/Models/Discord/Context.cs index 20c4f17..c3b8662 100644 --- a/Zhongli.Data/Models/Discord/Context.cs +++ b/Zhongli.Data/Models/Discord/Context.cs @@ -37,6 +37,9 @@ protected Context(IDiscordClient client, IGuild guild, IMessageChannel channel, /// public IUser User { get; } + /// + public virtual Task DeferAsync(bool ephemeral = false, RequestOptions? options = null) => Task.CompletedTask; + /// Sends a message to the context. /// /// Contents of the message; optional only if is specified. diff --git a/Zhongli.Data/Models/Discord/InteractionContext.cs b/Zhongli.Data/Models/Discord/InteractionContext.cs index 7465525..bb68eb1 100644 --- a/Zhongli.Data/Models/Discord/InteractionContext.cs +++ b/Zhongli.Data/Models/Discord/InteractionContext.cs @@ -10,6 +10,9 @@ namespace Zhongli.Data.Models.Discord; [SuppressMessage("ReSharper", "InconsistentNaming")] public class InteractionContext : Context, IInteractionContext, IDiscordInteraction { + private bool _deferred; + private bool _followed; + public InteractionContext(IInteractionContext context) : base(context.Client, context.Guild, context.Channel, context.User) { @@ -22,9 +25,12 @@ public InteractionContext(IInteractionContext context) /// ulong IEntity.Id => Interaction.Id; - /// - public Task DeferAsync(bool ephemeral = false, RequestOptions? options = null) - => Interaction.DeferAsync(ephemeral, options); + /// + public override async Task DeferAsync(bool ephemeral = false, RequestOptions? options = null) + { + _deferred = true; + await Interaction.DeferAsync(ephemeral, options); + } /// public Task DeleteOriginalResponseAsync(RequestOptions? options = null) @@ -51,14 +57,17 @@ public Task RespondWithFilesAsync( embed, options); /// - public Task FollowupAsync( + public async Task FollowupAsync( string? text = null, Embed[]? embeds = null, bool isTTS = false, bool ephemeral = false, AllowedMentions? allowedMentions = null, MessageComponent? components = null, Embed? embed = null, RequestOptions? options = null) - => Interaction.FollowupAsync( + { + _followed = true; + return await Interaction.FollowupAsync( text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options); + } /// public Task FollowupWithFilesAsync( @@ -105,8 +114,13 @@ public override Task ReplyAsync( AllowedMentions? allowedMentions = null, MessageReference? messageReference = null, MessageComponent? components = null, ISticker[]? stickers = null, Embed[]? embeds = null, bool ephemeral = false) - => RespondAsync( - message, embeds, isTTS, ephemeral, - allowedMentions, components, - embed, options); + => _deferred && !_followed + ? FollowupAsync( + message, embeds, isTTS, ephemeral, + allowedMentions, components, + embed, options) + : RespondAsync( + message, embeds, isTTS, ephemeral, + allowedMentions, components, + embed, options); } \ No newline at end of file diff --git a/Zhongli.Services/Moderation/UserService.cs b/Zhongli.Services/Moderation/UserService.cs index 49a96f8..e85b0c3 100644 --- a/Zhongli.Services/Moderation/UserService.cs +++ b/Zhongli.Services/Moderation/UserService.cs @@ -13,6 +13,7 @@ using Zhongli.Data.Models.Moderation.Infractions.Reprimands; using Zhongli.Data.Models.Moderation.Logging; using Zhongli.Services.Core; +using Zhongli.Services.Image; using Zhongli.Services.Interactive.Paginator; using Zhongli.Services.Utilities; using static Discord.InteractionResponseType; @@ -22,23 +23,28 @@ namespace Zhongli.Services.Moderation; public class UserService { private readonly AuthorizationService _auth; + private readonly IImageService _image; private readonly InteractiveService _interactive; private readonly ZhongliContext _db; - public UserService(AuthorizationService auth, InteractiveService interactive, ZhongliContext db) + public UserService( + AuthorizationService auth, IImageService image, + InteractiveService interactive, ZhongliContext db) { _auth = auth; + _image = image; _interactive = interactive; _db = db; } public async Task ReplyAvatarAsync(Context context, IUser user) { + await context.DeferAsync(true); var components = await ComponentsAsync(context, user); var embed = new EmbedBuilder() .WithUserAsAuthor(user, AuthorOptions.IncludeId) - .WithImageUrl(user.GetAvatarUrl(size: 2048)) - .WithColor(Color.Green) + .WithImageUrl(user.GetDefiniteAvatarUrl(2048)) + .WithColor(await _image.GetAvatarColor(user)) .WithUserAsAuthor(context.User, AuthorOptions.UseFooter | AuthorOptions.Requested); await context.ReplyAsync(embed: embed.Build(), ephemeral: true, components: components); @@ -58,7 +64,7 @@ public async Task ReplyHistoryAsync(Context context, LogReprimandType type, IUse var details = GetReprimands(userEntity); var reprimands = history .OrderByDescending(r => r.Action?.Date) - .Select(CreateEmbed); + .Select(r => CreateEmbed(r)); var pages = details.Concat(reprimands).ToPageBuilders(8, embed); var paginator = InteractiveExtensions.CreateDefaultPaginator().WithPages(pages).Build();