Skip to content

Commit

Permalink
feat(stickercounter): Add sticker counter
Browse files Browse the repository at this point in the history
  • Loading branch information
stepech committed Dec 4, 2023
1 parent 8f41411 commit b1c719d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
65 changes: 63 additions & 2 deletions src/HonzaBotner.Discord.Services/Commands/EmoteCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<CountedEmoji> orderedResults)
{
StringBuilder builder = new("\n");

int emojisAppended = 0;
Expand Down Expand Up @@ -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<CountedEmoji> orderedResults)
{
StringBuilder builder = new("\n");

int stickersAppended = 0;

IReadOnlyDictionary<ulong, DiscordMessageSticker> 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<Page> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<MessageCreateEventArgs>
{
private readonly IEmojiCounterService _emojiCounterService;
private readonly ulong[] _ignoreChannels;

public StickerCounterService(IEmojiCounterService emojiCounterService, IOptions<CommonCommandOptions> options)
{
_emojiCounterService = emojiCounterService;
_ignoreChannels = options.Value.ReactionIgnoreChannels ?? Array.Empty<ulong>();
}

public async Task<EventHandlerResult> 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;
}
}
1 change: 1 addition & 0 deletions src/HonzaBotner.Discord/DiscordBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions src/HonzaBotner/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void ConfigureServices(IServiceCollection services)
// .AddEventHandler<BadgeRoleHandler>()
.AddEventHandler<ThreadHandler>()
.AddEventHandler<StandupButtonHandler>()
.AddEventHandler<StickerCounterService>()
;
}, commands =>
{
Expand Down

0 comments on commit b1c719d

Please sign in to comment.