From 0f58b10ca24c0ad76ded4e50f80ce70c6caeaac7 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Thu, 15 Dec 2016 05:22:12 +0100 Subject: [PATCH] .crstats, .crstatsclear added --- .../CustomReactions/CustomReactions.cs | 60 ++++++++++++++++--- .../Resources/CommandStrings.Designer.cs | 54 +++++++++++++++++ src/NadekoBot/Resources/CommandStrings.resx | 18 ++++++ 3 files changed, 124 insertions(+), 8 deletions(-) diff --git a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs index b7241f12..d7c28f91 100644 --- a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs +++ b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs @@ -10,12 +10,14 @@ using NadekoBot.Extensions; namespace NadekoBot.Modules.CustomReactions { - [NadekoModule("CustomReactions",".")] + [NadekoModule("CustomReactions", ".")] public class CustomReactions : DiscordModule { public static ConcurrentHashSet GlobalReactions { get; } = new ConcurrentHashSet(); public static ConcurrentDictionary> GuildReactions { get; } = new ConcurrentDictionary>(); - + + public static ConcurrentDictionary ReactionStats { get; } = new ConcurrentDictionary(); + static CustomReactions() { using (var uow = DbHandler.UnitOfWork()) @@ -29,6 +31,8 @@ namespace NadekoBot.Modules.CustomReactions { } + public void ClearStats() => ReactionStats.Clear(); + public static async Task TryExecuteCustomReaction(IUserMessage umsg) { var channel = umsg.Channel as ITextChannel; @@ -40,15 +44,18 @@ namespace NadekoBot.Modules.CustomReactions GuildReactions.TryGetValue(channel.Guild.Id, out reactions); if (reactions != null && reactions.Any()) { - var reaction = reactions.Where(cr => { + var reaction = reactions.Where(cr => + { var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%"); var trigger = cr.TriggerWithContext(umsg).Trim().ToLowerInvariant(); return ((hasTarget && content.StartsWith(trigger + " ")) || content == trigger); }).Shuffle().FirstOrDefault(); if (reaction != null) { - if(reaction.Response != "-") + if (reaction.Response != "-") try { await channel.SendMessageAsync(reaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { } + + ReactionStats.AddOrUpdate(reaction.Trigger, 1, (k, old) => ++old); return true; } } @@ -62,6 +69,7 @@ namespace NadekoBot.Modules.CustomReactions if (greaction != null) { try { await channel.SendMessageAsync(greaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { } + ReactionStats.AddOrUpdate(greaction.Trigger, 1, (k, old) => ++old); return true; } return false; @@ -190,9 +198,9 @@ namespace NadekoBot.Modules.CustomReactions if (customReactions == null || !customReactions.Any()) await imsg.Channel.SendErrorAsync("No custom reactions found").ConfigureAwait(false); else - await imsg.Channel.SendConfirmAsync($"Page {page} of custom reactions (grouped):", + await imsg.Channel.SendConfirmAsync($"Page {page} of custom reactions (grouped):", string.Join("\r\n", customReactions - .GroupBy(cr=>cr.Trigger) + .GroupBy(cr => cr.Trigger) .OrderBy(cr => cr.Key) .Skip((page - 1) * 20) .Take(20) @@ -220,7 +228,7 @@ namespace NadekoBot.Modules.CustomReactions await imsg.Channel.EmbedAsync(new EmbedBuilder().WithColor(NadekoBot.OkColor) .WithDescription($"#{id}") .AddField(efb => efb.WithName("Trigger").WithValue(found.Trigger)) - .AddField(efb => efb.WithName("Response").WithValue(found.Response + "\n```css\n" + found.Response + "```" )) + .AddField(efb => efb.WithName("Response").WithValue(found.Response + "\n```css\n" + found.Response + "```")) .Build()).ConfigureAwait(false); } } @@ -256,7 +264,7 @@ namespace NadekoBot.Modules.CustomReactions GuildReactions.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet()).RemoveWhere(cr => cr.Id == toDelete.Id); success = true; } - if(success) + if (success) await uow.CompleteAsync().ConfigureAwait(false); } @@ -265,5 +273,41 @@ namespace NadekoBot.Modules.CustomReactions else await imsg.Channel.SendErrorAsync("Failed to find that custom reaction.").ConfigureAwait(false); } + + [NadekoCommand, Usage, Description, Aliases] + public async Task CrStatsClear(IUserMessage imsg, string trigger = null) + { + if (string.IsNullOrWhiteSpace(trigger)) + { + ClearStats(); + await imsg.Channel.SendConfirmAsync($"Custom reaction stats cleared.").ConfigureAwait(false); + } + else + { + uint throwaway; + if (ReactionStats.TryRemove(trigger, out throwaway)) + { + await imsg.Channel.SendConfirmAsync($"Stats cleared for `{trigger}` custom reaction.").ConfigureAwait(false); + } + else + { + await imsg.Channel.SendErrorAsync("No stats for that trigger found, no action taken.").ConfigureAwait(false); + } + } + } + + [NadekoCommand, Usage, Description, Aliases] + public async Task CrStats(IUserMessage imsg, int page = 1) + { + if (page < 1) + return; + await imsg.Channel.EmbedAsync(ReactionStats.OrderByDescending(x => x.Value) + .Skip((page - 1)*9) + .Take(9) + .Aggregate(new EmbedBuilder().WithColor(NadekoBot.OkColor).WithTitle($"Custom Reaction stats page #{page}"), + (agg, cur) => agg.AddField(efb => efb.WithName(cur.Key).WithValue(cur.Value.ToString()).WithIsInline(true))) + .Build()) + .ConfigureAwait(false); + } } } diff --git a/src/NadekoBot/Resources/CommandStrings.Designer.cs b/src/NadekoBot/Resources/CommandStrings.Designer.cs index 839ce325..48ea73df 100644 --- a/src/NadekoBot/Resources/CommandStrings.Designer.cs +++ b/src/NadekoBot/Resources/CommandStrings.Designer.cs @@ -1868,6 +1868,60 @@ namespace NadekoBot.Resources { } } + /// + /// Looks up a localized string similar to crstats. + /// + public static string crstats_cmd { + get { + return ResourceManager.GetString("crstats_cmd", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Shows a list of custom reactions and the number of times they have been executed. Paginated with 10 per page. Use `{0}crstatsclear` to reset the counters.. + /// + public static string crstats_desc { + get { + return ResourceManager.GetString("crstats_desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to `{0}crstats` or `{0}crstats 3`. + /// + public static string crstats_usage { + get { + return ResourceManager.GetString("crstats_usage", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to crstatsclear. + /// + public static string crstatsclear_cmd { + get { + return ResourceManager.GetString("crstatsclear_cmd", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Resets the counters on `{0}crstats`. You can specify a trigger to clear stats only for that trigger.. + /// + public static string crstatsclear_desc { + get { + return ResourceManager.GetString("crstatsclear_desc", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to `{0}crstatsclear` or `{0}crstatsclear rng`. + /// + public static string crstatsclear_usage { + get { + return ResourceManager.GetString("crstatsclear_usage", resourceCulture); + } + } + /// /// Looks up a localized string similar to danbooru. /// diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index ef74d8e6..5648c4f6 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -2763,4 +2763,22 @@ `{0}hangman` or `{0}hangman movies` + + crstatsclear + + + Resets the counters on `{0}crstats`. You can specify a trigger to clear stats only for that trigger. + + + `{0}crstatsclear` or `{0}crstatsclear rng` + + + crstats + + + Shows a list of custom reactions and the number of times they have been executed. Paginated with 10 per page. Use `{0}crstatsclear` to reset the counters. + + + `{0}crstats` or `{0}crstats 3` + \ No newline at end of file