Skip to content

Commit

Permalink
feat: Use DominantColor in Avatar command
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed Dec 29, 2021
1 parent 544f278 commit afb5f03
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Zhongli.Data/Models/Discord/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ protected Context(IDiscordClient client, IGuild guild, IMessageChannel channel,
/// </summary>
public IUser User { get; }

/// <inheritdoc cref="IDiscordInteraction.DeferAsync" />
public virtual Task DeferAsync(bool ephemeral = false, RequestOptions? options = null) => Task.CompletedTask;

/// <summary>Sends a message to the context.</summary>
/// <param name="message">
/// Contents of the message; optional only if <paramref name="embed" /> is specified.
Expand Down
32 changes: 23 additions & 9 deletions Zhongli.Data/Models/Discord/InteractionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -22,9 +25,12 @@ public InteractionContext(IInteractionContext context)
/// <inheritdoc />
ulong IEntity<ulong>.Id => Interaction.Id;

/// <inheritdoc />
public Task DeferAsync(bool ephemeral = false, RequestOptions? options = null)
=> Interaction.DeferAsync(ephemeral, options);
/// <inheritdoc cref="IDiscordInteraction.DeferAsync" />
public override async Task DeferAsync(bool ephemeral = false, RequestOptions? options = null)
{
_deferred = true;
await Interaction.DeferAsync(ephemeral, options);
}

/// <inheritdoc />
public Task DeleteOriginalResponseAsync(RequestOptions? options = null)
Expand All @@ -51,14 +57,17 @@ public Task RespondWithFilesAsync(
embed, options);

/// <inheritdoc />
public Task<IUserMessage> FollowupAsync(
public async Task<IUserMessage> 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);
}

/// <inheritdoc />
public Task<IUserMessage> FollowupWithFilesAsync(
Expand Down Expand Up @@ -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);
}
14 changes: 10 additions & 4 deletions Zhongli.Services/Moderation/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit afb5f03

Please sign in to comment.