.crstats, .crstatsclear added

This commit is contained in:
Kwoth 2016-12-15 05:22:12 +01:00
parent 35df37c418
commit 0f58b10ca2
3 changed files with 124 additions and 8 deletions

View File

@ -10,12 +10,14 @@ using NadekoBot.Extensions;
namespace NadekoBot.Modules.CustomReactions namespace NadekoBot.Modules.CustomReactions
{ {
[NadekoModule("CustomReactions",".")] [NadekoModule("CustomReactions", ".")]
public class CustomReactions : DiscordModule public class CustomReactions : DiscordModule
{ {
public static ConcurrentHashSet<CustomReaction> GlobalReactions { get; } = new ConcurrentHashSet<CustomReaction>(); public static ConcurrentHashSet<CustomReaction> GlobalReactions { get; } = new ConcurrentHashSet<CustomReaction>();
public static ConcurrentDictionary<ulong, ConcurrentHashSet<CustomReaction>> GuildReactions { get; } = new ConcurrentDictionary<ulong, ConcurrentHashSet<CustomReaction>>(); public static ConcurrentDictionary<ulong, ConcurrentHashSet<CustomReaction>> GuildReactions { get; } = new ConcurrentDictionary<ulong, ConcurrentHashSet<CustomReaction>>();
public static ConcurrentDictionary<string, uint> ReactionStats { get; } = new ConcurrentDictionary<string, uint>();
static CustomReactions() static CustomReactions()
{ {
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
@ -29,6 +31,8 @@ namespace NadekoBot.Modules.CustomReactions
{ {
} }
public void ClearStats() => ReactionStats.Clear();
public static async Task<bool> TryExecuteCustomReaction(IUserMessage umsg) public static async Task<bool> TryExecuteCustomReaction(IUserMessage umsg)
{ {
var channel = umsg.Channel as ITextChannel; var channel = umsg.Channel as ITextChannel;
@ -40,15 +44,18 @@ namespace NadekoBot.Modules.CustomReactions
GuildReactions.TryGetValue(channel.Guild.Id, out reactions); GuildReactions.TryGetValue(channel.Guild.Id, out reactions);
if (reactions != null && reactions.Any()) if (reactions != null && reactions.Any())
{ {
var reaction = reactions.Where(cr => { var reaction = reactions.Where(cr =>
{
var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%"); var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%");
var trigger = cr.TriggerWithContext(umsg).Trim().ToLowerInvariant(); var trigger = cr.TriggerWithContext(umsg).Trim().ToLowerInvariant();
return ((hasTarget && content.StartsWith(trigger + " ")) || content == trigger); return ((hasTarget && content.StartsWith(trigger + " ")) || content == trigger);
}).Shuffle().FirstOrDefault(); }).Shuffle().FirstOrDefault();
if (reaction != null) if (reaction != null)
{ {
if(reaction.Response != "-") if (reaction.Response != "-")
try { await channel.SendMessageAsync(reaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { } try { await channel.SendMessageAsync(reaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }
ReactionStats.AddOrUpdate(reaction.Trigger, 1, (k, old) => ++old);
return true; return true;
} }
} }
@ -62,6 +69,7 @@ namespace NadekoBot.Modules.CustomReactions
if (greaction != null) if (greaction != null)
{ {
try { await channel.SendMessageAsync(greaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { } try { await channel.SendMessageAsync(greaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }
ReactionStats.AddOrUpdate(greaction.Trigger, 1, (k, old) => ++old);
return true; return true;
} }
return false; return false;
@ -190,9 +198,9 @@ namespace NadekoBot.Modules.CustomReactions
if (customReactions == null || !customReactions.Any()) if (customReactions == null || !customReactions.Any())
await imsg.Channel.SendErrorAsync("No custom reactions found").ConfigureAwait(false); await imsg.Channel.SendErrorAsync("No custom reactions found").ConfigureAwait(false);
else 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 string.Join("\r\n", customReactions
.GroupBy(cr=>cr.Trigger) .GroupBy(cr => cr.Trigger)
.OrderBy(cr => cr.Key) .OrderBy(cr => cr.Key)
.Skip((page - 1) * 20) .Skip((page - 1) * 20)
.Take(20) .Take(20)
@ -220,7 +228,7 @@ namespace NadekoBot.Modules.CustomReactions
await imsg.Channel.EmbedAsync(new EmbedBuilder().WithColor(NadekoBot.OkColor) await imsg.Channel.EmbedAsync(new EmbedBuilder().WithColor(NadekoBot.OkColor)
.WithDescription($"#{id}") .WithDescription($"#{id}")
.AddField(efb => efb.WithName("Trigger").WithValue(found.Trigger)) .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); .Build()).ConfigureAwait(false);
} }
} }
@ -256,7 +264,7 @@ namespace NadekoBot.Modules.CustomReactions
GuildReactions.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<CustomReaction>()).RemoveWhere(cr => cr.Id == toDelete.Id); GuildReactions.GetOrAdd(channel.Guild.Id, new ConcurrentHashSet<CustomReaction>()).RemoveWhere(cr => cr.Id == toDelete.Id);
success = true; success = true;
} }
if(success) if (success)
await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);
} }
@ -265,5 +273,41 @@ namespace NadekoBot.Modules.CustomReactions
else else
await imsg.Channel.SendErrorAsync("Failed to find that custom reaction.").ConfigureAwait(false); 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);
}
} }
} }

View File

@ -1868,6 +1868,60 @@ namespace NadekoBot.Resources {
} }
} }
/// <summary>
/// Looks up a localized string similar to crstats.
/// </summary>
public static string crstats_cmd {
get {
return ResourceManager.GetString("crstats_cmd", resourceCulture);
}
}
/// <summary>
/// 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..
/// </summary>
public static string crstats_desc {
get {
return ResourceManager.GetString("crstats_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}crstats` or `{0}crstats 3`.
/// </summary>
public static string crstats_usage {
get {
return ResourceManager.GetString("crstats_usage", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to crstatsclear.
/// </summary>
public static string crstatsclear_cmd {
get {
return ResourceManager.GetString("crstatsclear_cmd", resourceCulture);
}
}
/// <summary>
/// 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..
/// </summary>
public static string crstatsclear_desc {
get {
return ResourceManager.GetString("crstatsclear_desc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to `{0}crstatsclear` or `{0}crstatsclear rng`.
/// </summary>
public static string crstatsclear_usage {
get {
return ResourceManager.GetString("crstatsclear_usage", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to danbooru. /// Looks up a localized string similar to danbooru.
/// </summary> /// </summary>

View File

@ -2763,4 +2763,22 @@
<data name="hangman_usage" xml:space="preserve"> <data name="hangman_usage" xml:space="preserve">
<value>`{0}hangman` or `{0}hangman movies`</value> <value>`{0}hangman` or `{0}hangman movies`</value>
</data> </data>
<data name="crstatsclear_cmd" xml:space="preserve">
<value>crstatsclear</value>
</data>
<data name="crstatsclear_desc" xml:space="preserve">
<value>Resets the counters on `{0}crstats`. You can specify a trigger to clear stats only for that trigger.</value>
</data>
<data name="crstatsclear_usage" xml:space="preserve">
<value>`{0}crstatsclear` or `{0}crstatsclear rng`</value>
</data>
<data name="crstats_cmd" xml:space="preserve">
<value>crstats</value>
</data>
<data name="crstats_desc" xml:space="preserve">
<value>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.</value>
</data>
<data name="crstats_usage" xml:space="preserve">
<value>`{0}crstats` or `{0}crstats 3`</value>
</data>
</root> </root>