custom reactions fully localizable, found several bugs too.

This commit is contained in:
Kwoth 2017-02-13 22:11:36 +01:00
parent 023df13121
commit ff193e4549
4 changed files with 233 additions and 60 deletions

View File

@ -139,7 +139,7 @@ namespace NadekoBot.Modules.CustomReactions
if ((channel == null && !NadekoBot.Credentials.IsOwner(Context.User)) || (channel != null && !((IGuildUser)Context.User).GuildPermissions.Administrator)) if ((channel == null && !NadekoBot.Credentials.IsOwner(Context.User)) || (channel != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
{ {
try { await Context.Channel.SendErrorAsync("Insufficient permissions. Requires Bot ownership for global custom reactions, and Administrator for guild custom reactions."); } catch { } await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false);
return; return;
} }
@ -166,7 +166,7 @@ namespace NadekoBot.Modules.CustomReactions
else else
{ {
var reactions = GuildReactions.AddOrUpdate(Context.Guild.Id, var reactions = GuildReactions.AddOrUpdate(Context.Guild.Id,
Array.Empty<CustomReaction>(), new CustomReaction[] { cr },
(k, old) => (k, old) =>
{ {
Array.Resize(ref old, old.Length + 1); Array.Resize(ref old, old.Length + 1);
@ -176,10 +176,10 @@ namespace NadekoBot.Modules.CustomReactions
} }
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor() await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithTitle("New Custom Reaction") .WithTitle(GetText("new_cust_react"))
.WithDescription($"#{cr.Id}") .WithDescription($"#{cr.Id}")
.AddField(efb => efb.WithName("Trigger").WithValue(key)) .AddField(efb => efb.WithName(GetText("trigger")).WithValue(key))
.AddField(efb => efb.WithName("Response").WithValue(message)) .AddField(efb => efb.WithName(GetText("response")).WithValue(message))
).ConfigureAwait(false); ).ConfigureAwait(false);
} }
@ -196,19 +196,20 @@ namespace NadekoBot.Modules.CustomReactions
customReactions = GuildReactions.GetOrAdd(Context.Guild.Id, Array.Empty<CustomReaction>()).Where(cr => cr != null).ToArray(); customReactions = GuildReactions.GetOrAdd(Context.Guild.Id, Array.Empty<CustomReaction>()).Where(cr => cr != null).ToArray();
if (customReactions == null || !customReactions.Any()) if (customReactions == null || !customReactions.Any())
await Context.Channel.SendErrorAsync("No custom reactions found").ConfigureAwait(false);
else
{ {
var lastPage = customReactions.Length / 20; await ReplyErrorLocalized("no_found").ConfigureAwait(false);
await Context.Channel.SendPaginatedConfirmAsync(page, curPage => return;
new EmbedBuilder().WithOkColor()
.WithTitle("Custom reactions")
.WithDescription(string.Join("\n", customReactions.OrderBy(cr => cr.Trigger)
.Skip((curPage - 1) * 20)
.Take(20)
.Select(cr => $"`#{cr.Id}` `Trigger:` {cr.Trigger}"))), lastPage)
.ConfigureAwait(false);
} }
var lastPage = customReactions.Length / 20;
await Context.Channel.SendPaginatedConfirmAsync(page, curPage =>
new EmbedBuilder().WithOkColor()
.WithTitle(GetText("name"))
.WithDescription(string.Join("\n", customReactions.OrderBy(cr => cr.Trigger)
.Skip((curPage - 1) * 20)
.Take(20)
.Select(cr => $"`#{cr.Id}` `{GetText("trigger")}:` {cr.Trigger}"))), lastPage)
.ConfigureAwait(false);
} }
public enum All public enum All
@ -227,20 +228,22 @@ namespace NadekoBot.Modules.CustomReactions
customReactions = GuildReactions.GetOrAdd(Context.Guild.Id, new CustomReaction[]{ }).Where(cr => cr != null).ToArray(); customReactions = GuildReactions.GetOrAdd(Context.Guild.Id, new CustomReaction[]{ }).Where(cr => cr != null).ToArray();
if (customReactions == null || !customReactions.Any()) if (customReactions == null || !customReactions.Any())
await Context.Channel.SendErrorAsync("No custom reactions found").ConfigureAwait(false);
else
{ {
var txtStream = await customReactions.GroupBy(cr => cr.Trigger) await ReplyErrorLocalized("no_found").ConfigureAwait(false);
.OrderBy(cr => cr.Key) return;
.Select(cr => new { Trigger = cr.Key, Responses = cr.Select(y => new { id = y.Id, text = y.Response }).ToList() })
.ToJson()
.ToStream()
.ConfigureAwait(false);
if (Context.Guild == null) // its a private one, just send back
await Context.Channel.SendFileAsync(txtStream, "customreactions.txt", "List of all custom reactions").ConfigureAwait(false);
else
await ((IGuildUser)Context.User).SendFileAsync(txtStream, "customreactions.txt", "List of all custom reactions").ConfigureAwait(false);
} }
var txtStream = await customReactions.GroupBy(cr => cr.Trigger)
.OrderBy(cr => cr.Key)
.Select(cr => new { Trigger = cr.Key, Responses = cr.Select(y => new { id = y.Id, text = y.Response }).ToList() })
.ToJson()
.ToStream()
.ConfigureAwait(false);
if (Context.Guild == null) // its a private one, just send back
await Context.Channel.SendFileAsync(txtStream, "customreactions.txt", GetText("list_all")).ConfigureAwait(false);
else
await ((IGuildUser)Context.User).SendFileAsync(txtStream, "customreactions.txt", GetText("list_all")).ConfigureAwait(false);
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
@ -255,7 +258,9 @@ namespace NadekoBot.Modules.CustomReactions
customReactions = GuildReactions.GetOrAdd(Context.Guild.Id, new CustomReaction[]{ }).Where(cr => cr != null).ToArray(); customReactions = GuildReactions.GetOrAdd(Context.Guild.Id, new CustomReaction[]{ }).Where(cr => cr != null).ToArray();
if (customReactions == null || !customReactions.Any()) if (customReactions == null || !customReactions.Any())
await Context.Channel.SendErrorAsync("No custom reactions found").ConfigureAwait(false); {
await ReplyErrorLocalized("no_found").ConfigureAwait(false);
}
else else
{ {
var ordered = customReactions var ordered = customReactions
@ -266,7 +271,7 @@ namespace NadekoBot.Modules.CustomReactions
var lastPage = ordered.Count / 20; var lastPage = ordered.Count / 20;
await Context.Channel.SendPaginatedConfirmAsync(page, (curPage) => await Context.Channel.SendPaginatedConfirmAsync(page, (curPage) =>
new EmbedBuilder().WithOkColor() new EmbedBuilder().WithOkColor()
.WithTitle($"Custom Reactions (grouped)") .WithTitle(GetText("name"))
.WithDescription(string.Join("\r\n", ordered .WithDescription(string.Join("\r\n", ordered
.Skip((curPage - 1) * 20) .Skip((curPage - 1) * 20)
.Take(20) .Take(20)
@ -287,13 +292,16 @@ namespace NadekoBot.Modules.CustomReactions
var found = customReactions.FirstOrDefault(cr => cr?.Id == id); var found = customReactions.FirstOrDefault(cr => cr?.Id == id);
if (found == null) if (found == null)
await Context.Channel.SendErrorAsync("No custom reaction found with that id.").ConfigureAwait(false); {
await ReplyErrorLocalized("no_found_id").ConfigureAwait(false);
return;
}
else else
{ {
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor() await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithDescription($"#{id}") .WithDescription($"#{id}")
.AddField(efb => efb.WithName("Trigger").WithValue(found.Trigger)) .AddField(efb => efb.WithName(GetText("trigger")).WithValue(found.Trigger))
.AddField(efb => efb.WithName("Response").WithValue(found.Response + "\n```css\n" + found.Response + "```")) .AddField(efb => efb.WithName(GetText("response")).WithValue(found.Response + "\n```css\n" + found.Response + "```"))
).ConfigureAwait(false); ).ConfigureAwait(false);
} }
} }
@ -303,7 +311,7 @@ namespace NadekoBot.Modules.CustomReactions
{ {
if ((Context.Guild == null && !NadekoBot.Credentials.IsOwner(Context.User)) || (Context.Guild != null && !((IGuildUser)Context.User).GuildPermissions.Administrator)) if ((Context.Guild == null && !NadekoBot.Credentials.IsOwner(Context.User)) || (Context.Guild != null && !((IGuildUser)Context.User).GuildPermissions.Administrator))
{ {
try { await Context.Channel.SendErrorAsync("Insufficient permissions. Requires Bot ownership for global custom reactions, and Administrator for guild custom reactions."); } catch { } await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false);
return; return;
} }
@ -313,32 +321,42 @@ namespace NadekoBot.Modules.CustomReactions
{ {
toDelete = uow.CustomReactions.Get(id); toDelete = uow.CustomReactions.Get(id);
if (toDelete == null) //not found if (toDelete == null) //not found
return; success = false;
else
if ((toDelete.GuildId == null || toDelete.GuildId == 0) && Context.Guild == null)
{ {
uow.CustomReactions.Remove(toDelete); if ((toDelete.GuildId == null || toDelete.GuildId == 0) && Context.Guild == null)
//todo i can dramatically improve performance of this, if Ids are ordered.
_globalReactions = GlobalReactions.Where(cr => cr?.Id != toDelete.Id).ToArray();
success = true;
}
else if ((toDelete.GuildId != null && toDelete.GuildId != 0) && Context.Guild.Id == toDelete.GuildId)
{
uow.CustomReactions.Remove(toDelete);
GuildReactions.AddOrUpdate(Context.Guild.Id, new CustomReaction[] { }, (key, old) =>
{ {
return old.Where(cr => cr?.Id != toDelete.Id).ToArray(); uow.CustomReactions.Remove(toDelete);
}); //todo i can dramatically improve performance of this, if Ids are ordered.
success = true; _globalReactions = GlobalReactions.Where(cr => cr?.Id != toDelete.Id).ToArray();
success = true;
}
else if ((toDelete.GuildId != null && toDelete.GuildId != 0) && Context.Guild.Id == toDelete.GuildId)
{
uow.CustomReactions.Remove(toDelete);
GuildReactions.AddOrUpdate(Context.Guild.Id, new CustomReaction[] { }, (key, old) =>
{
return old.Where(cr => cr?.Id != toDelete.Id).ToArray();
});
success = true;
}
if (success)
await uow.CompleteAsync().ConfigureAwait(false);
} }
if (success)
await uow.CompleteAsync().ConfigureAwait(false);
} }
if (success) if (success)
await Context.Channel.SendConfirmAsync("Deleted custom reaction", toDelete.ToString()).ConfigureAwait(false); {
await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
.WithTitle(GetText("deleted"))
.WithDescription("#" + toDelete.Id)
.AddField(efb => efb.WithName(GetText("trigger")).WithValue(toDelete.Trigger))
.AddField(efb => efb.WithName(GetText("response")).WithValue(toDelete.Response)));
}
else else
await Context.Channel.SendErrorAsync("Failed to find that custom reaction.").ConfigureAwait(false); {
await ReplyErrorLocalized("no_found_id").ConfigureAwait(false);
}
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
@ -348,18 +366,18 @@ namespace NadekoBot.Modules.CustomReactions
if (string.IsNullOrWhiteSpace(trigger)) if (string.IsNullOrWhiteSpace(trigger))
{ {
ClearStats(); ClearStats();
await Context.Channel.SendConfirmAsync($"Custom reaction stats cleared.").ConfigureAwait(false); await ReplyConfirmLocalized("all_stats_cleared").ConfigureAwait(false);
} }
else else
{ {
uint throwaway; uint throwaway;
if (ReactionStats.TryRemove(trigger, out throwaway)) if (ReactionStats.TryRemove(trigger, out throwaway))
{ {
await Context.Channel.SendConfirmAsync($"Stats cleared for `{trigger}` custom reaction.").ConfigureAwait(false); await ReplyErrorLocalized("stats_cleared", Format.Bold(trigger)).ConfigureAwait(false);
} }
else else
{ {
await Context.Channel.SendErrorAsync("No stats for that trigger found, no action taken.").ConfigureAwait(false); await ReplyErrorLocalized("stats_not_found").ConfigureAwait(false);
} }
} }
} }
@ -376,7 +394,7 @@ namespace NadekoBot.Modules.CustomReactions
await Context.Channel.SendPaginatedConfirmAsync(page, await Context.Channel.SendPaginatedConfirmAsync(page,
(curPage) => ordered.Skip((curPage - 1) * 9) (curPage) => ordered.Skip((curPage - 1) * 9)
.Take(9) .Take(9)
.Aggregate(new EmbedBuilder().WithOkColor().WithTitle($"Custom Reaction Stats"), .Aggregate(new EmbedBuilder().WithOkColor().WithTitle(GetText("stats")),
(agg, cur) => agg.AddField(efb => efb.WithName(cur.Key).WithValue(cur.Value.ToString()).WithIsInline(true))), lastPage) (agg, cur) => agg.AddField(efb => efb.WithName(cur.Key).WithValue(cur.Value.ToString()).WithIsInline(true))), lastPage)
.ConfigureAwait(false); .ConfigureAwait(false);
} }

View File

@ -266,6 +266,123 @@ namespace NadekoBot.Resources {
} }
} }
/// <summary>
/// Looks up a localized string similar to All custom reaction stats cleared..
/// </summary>
public static string customreactions_all_stats_cleared {
get {
return ResourceManager.GetString("customreactions_all_stats_cleared", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Custom Reaction deleted.
/// </summary>
public static string customreactions_deleted {
get {
return ResourceManager.GetString("customreactions_deleted", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Insufficient permissions. Requires Bot ownership for global custom reactions, and Administrator for guild custom reactions..
/// </summary>
public static string customreactions_insuff_perms {
get {
return ResourceManager.GetString("customreactions_insuff_perms", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to List of all custom reactions.
/// </summary>
public static string customreactions_list_all {
get {
return ResourceManager.GetString("customreactions_list_all", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Custom Reactions.
/// </summary>
public static string customreactions_name {
get {
return ResourceManager.GetString("customreactions_name", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to New Custom Reaction.
/// </summary>
public static string customreactions_new_cust_react {
get {
return ResourceManager.GetString("customreactions_new_cust_react", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to No custom reactions found..
/// </summary>
public static string customreactions_no_found {
get {
return ResourceManager.GetString("customreactions_no_found", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to No custom reaction found with that id..
/// </summary>
public static string customreactions_no_found_id {
get {
return ResourceManager.GetString("customreactions_no_found_id", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Response.
/// </summary>
public static string customreactions_response {
get {
return ResourceManager.GetString("customreactions_response", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Custom Reaction Stats.
/// </summary>
public static string customreactions_stats {
get {
return ResourceManager.GetString("customreactions_stats", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Stats cleared for {0} custom reaction..
/// </summary>
public static string customreactions_stats_cleared {
get {
return ResourceManager.GetString("customreactions_stats_cleared", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to No stats for that trigger found, no action taken..
/// </summary>
public static string customreactions_stats_not_found {
get {
return ResourceManager.GetString("customreactions_stats_not_found", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Trigger.
/// </summary>
public static string customreactions_trigger {
get {
return ResourceManager.GetString("customreactions_trigger", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to {0} has already fainted.. /// Looks up a localized string similar to {0} has already fainted..
/// </summary> /// </summary>

View File

@ -163,7 +163,7 @@
<value>You are not participating in that war.</value> <value>You are not participating in that war.</value>
</data> </data>
<data name="clashofclans_not_partic_or_destroyed" xml:space="preserve"> <data name="clashofclans_not_partic_or_destroyed" xml:space="preserve">
<value>@{0} You are either not participating in that war, or you have already destroyed a base.</value> <value>@{0} You are either not participating in that war, or that base is already destroyed.</value>
</data> </data>
<data name="clashofclans_no_active_wars" xml:space="preserve"> <data name="clashofclans_no_active_wars" xml:space="preserve">
<value>No active wars.</value> <value>No active wars.</value>
@ -172,7 +172,7 @@
<value>Size</value> <value>Size</value>
</data> </data>
<data name="clashofclans_war_already_started" xml:space="preserve"> <data name="clashofclans_war_already_started" xml:space="preserve">
<value>War against {0} is already started.</value> <value>War against {0} has already started.</value>
</data> </data>
<data name="clashofclans_war_created" xml:space="preserve"> <data name="clashofclans_war_created" xml:space="preserve">
<value>War against {0} created.</value> <value>War against {0} created.</value>
@ -186,6 +186,45 @@
<data name="clashofclans_war_started" xml:space="preserve"> <data name="clashofclans_war_started" xml:space="preserve">
<value>War against {0} started!</value> <value>War against {0} started!</value>
</data> </data>
<data name="customreactions_all_stats_cleared" xml:space="preserve">
<value>All custom reaction stats cleared.</value>
</data>
<data name="customreactions_deleted" xml:space="preserve">
<value>Custom Reaction deleted</value>
</data>
<data name="customreactions_insuff_perms" xml:space="preserve">
<value>Insufficient permissions. Requires Bot ownership for global custom reactions, and Administrator for guild custom reactions.</value>
</data>
<data name="customreactions_list_all" xml:space="preserve">
<value>List of all custom reactions</value>
</data>
<data name="customreactions_name" xml:space="preserve">
<value>Custom Reactions</value>
</data>
<data name="customreactions_new_cust_react" xml:space="preserve">
<value>New Custom Reaction</value>
</data>
<data name="customreactions_no_found" xml:space="preserve">
<value>No custom reactions found.</value>
</data>
<data name="customreactions_no_found_id" xml:space="preserve">
<value>No custom reaction found with that id.</value>
</data>
<data name="customreactions_response" xml:space="preserve">
<value>Response</value>
</data>
<data name="customreactions_stats" xml:space="preserve">
<value>Custom Reaction Stats</value>
</data>
<data name="customreactions_stats_cleared" xml:space="preserve">
<value>Stats cleared for {0} custom reaction.</value>
</data>
<data name="customreactions_stats_not_found" xml:space="preserve">
<value>No stats for that trigger found, no action taken.</value>
</data>
<data name="customreactions_trigger" xml:space="preserve">
<value>Trigger</value>
</data>
<data name="pokemon_already_fainted" xml:space="preserve"> <data name="pokemon_already_fainted" xml:space="preserve">
<value>{0} has already fainted.</value> <value>{0} has already fainted.</value>
</data> </data>

View File

@ -12,7 +12,6 @@ namespace NadekoBot.Services.Database.Models
public string Trigger { get; set; } public string Trigger { get; set; }
public bool IsRegex { get; set; } public bool IsRegex { get; set; }
public bool OwnerOnly { get; set; } public bool OwnerOnly { get; set; }
public override string ToString() => $"`#{Id}` `Trigger:` {Trigger}\n `Response:` {Response}";
} }
public class ReactionResponse : DbEntity public class ReactionResponse : DbEntity