From b1c719d2ee577ed389f2cca3fc8fbb0a8ce39b79 Mon Sep 17 00:00:00 2001 From: stepech Date: Mon, 4 Dec 2023 01:15:14 +0100 Subject: [PATCH] feat(stickercounter): Add sticker counter #523 --- .../Commands/EmoteCommands.cs | 65 ++++++++++++++++++- .../EventHandlers/StickerCounterService.cs | 40 ++++++++++++ src/HonzaBotner.Discord/DiscordBot.cs | 1 + src/HonzaBotner/Startup.cs | 1 + 4 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 src/HonzaBotner.Discord.Services/EventHandlers/StickerCounterService.cs diff --git a/src/HonzaBotner.Discord.Services/Commands/EmoteCommands.cs b/src/HonzaBotner.Discord.Services/Commands/EmoteCommands.cs index 111a77a9..04b4a1a7 100644 --- a/src/HonzaBotner.Discord.Services/Commands/EmoteCommands.cs +++ b/src/HonzaBotner.Discord.Services/Commands/EmoteCommands.cs @@ -21,12 +21,14 @@ public class EmoteCommands : ApplicationCommandModule public enum DisplayTypes { - [ChoiceName("all")] + [ChoiceName("emotes")] All, [ChoiceName("animated")] Animated, [ChoiceName("still")] - Still + Still, + [ChoiceName("stickers")] + Stickers } public EmoteCommands(IEmojiCounterService emojiCounterService) @@ -46,6 +48,16 @@ public async Task EmoteStatsCommandAsync( ? results.OrderByDescending(emoji => emoji.Used) : results.OrderByDescending(emoji => emoji.UsagePerDay); + if (type == DisplayTypes.Stickers) await DisplayStickersAsync(ctx, total, orderedResults); + else await DisplayEmotesAsync(ctx, total, type, orderedResults); + } + + private async Task DisplayEmotesAsync( + InteractionContext ctx, + bool total, + DisplayTypes type, + IOrderedEnumerable orderedResults) + { StringBuilder builder = new("\n"); int emojisAppended = 0; @@ -103,4 +115,53 @@ public async Task EmoteStatsCommandAsync( await ctx.CreateResponseAsync("No emotes to show", true); } } + + private async Task DisplayStickersAsync( + InteractionContext ctx, + bool total, + IOrderedEnumerable orderedResults) + { + StringBuilder builder = new("\n"); + + int stickersAppended = 0; + + IReadOnlyDictionary stickers = ctx.Guild.Stickers; + + foreach (CountedEmoji result in orderedResults) + { + if (!stickers.Keys.Contains(result.Id)) + { + continue; + } + string label = total ? "×" : "×/day"; + + builder.Append(stickers[result.Id].Name) + .Append(" `") + .Append( + (total ? result.Used.ToString() : $"{result.UsagePerDay:0.00}").PadLeft(10, ' ')) + .Append(label) + .Append(" `\n"); + + stickersAppended++; + } + + if (stickersAppended > 0) + { + InteractivityExtension? interactivity = ctx.Client.GetInteractivity(); + DiscordEmbedBuilder embedBuilder = new() + { + Author = new DiscordEmbedBuilder.EmbedAuthor + { + IconUrl = ctx.Member.AvatarUrl, Name = ctx.Member.DisplayName + }, + Title = "Custom sticker usage stats" + }; + IEnumerable pages = interactivity.GeneratePagesInEmbed(builder.ToString(), SplitType.Line, embedBuilder); + await interactivity.SendPaginatedResponseAsync(ctx.Interaction, false, ctx.User, pages); + } + else + { + await ctx.CreateResponseAsync("No stickers to show", true); + } + } } diff --git a/src/HonzaBotner.Discord.Services/EventHandlers/StickerCounterService.cs b/src/HonzaBotner.Discord.Services/EventHandlers/StickerCounterService.cs new file mode 100644 index 00000000..48d6fb9c --- /dev/null +++ b/src/HonzaBotner.Discord.Services/EventHandlers/StickerCounterService.cs @@ -0,0 +1,40 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.EventArgs; +using HonzaBotner.Discord.EventHandler; +using HonzaBotner.Discord.Services.Options; +using HonzaBotner.Services.Contract; +using Microsoft.Extensions.Options; + +namespace HonzaBotner.Discord.Services.EventHandlers; + +public class StickerCounterService : IEventHandler +{ + private readonly IEmojiCounterService _emojiCounterService; + private readonly ulong[] _ignoreChannels; + + public StickerCounterService(IEmojiCounterService emojiCounterService, IOptions options) + { + _emojiCounterService = emojiCounterService; + _ignoreChannels = options.Value.ReactionIgnoreChannels ?? Array.Empty(); + } + + public async Task Handle(MessageCreateEventArgs args) + { + if (args.Message.Stickers.Count == 0 || _ignoreChannels.Contains(args.Channel.Id)) + { + return EventHandlerResult.Continue; + } + + DiscordMessageSticker sticker = args.Message.Stickers[0]; + + if (sticker.Type == StickerType.Guild && args.Guild.Stickers.Keys.Contains(sticker.Id)) + { + await _emojiCounterService.IncrementAsync(sticker.Id); + } + + return EventHandlerResult.Continue; + } +} diff --git a/src/HonzaBotner.Discord/DiscordBot.cs b/src/HonzaBotner.Discord/DiscordBot.cs index 400f0afe..43af933f 100644 --- a/src/HonzaBotner.Discord/DiscordBot.cs +++ b/src/HonzaBotner.Discord/DiscordBot.cs @@ -56,6 +56,7 @@ public async Task Run(CancellationToken cancellationToken) Client.GuildMemberUpdated += Client_GuildMemberUpdated; Client.ChannelCreated += Client_ChannelCreated; Client.ThreadCreated += Client_ThreadCreated; + Client.MessageCreated += (_, args) => _eventHandler.Handle(args); _commandsConfigurator.Config(Commands); diff --git a/src/HonzaBotner/Startup.cs b/src/HonzaBotner/Startup.cs index 8be36b52..aa6c6cc4 100644 --- a/src/HonzaBotner/Startup.cs +++ b/src/HonzaBotner/Startup.cs @@ -70,6 +70,7 @@ public void ConfigureServices(IServiceCollection services) // .AddEventHandler() .AddEventHandler() .AddEventHandler() + .AddEventHandler() ; }, commands => {