Custom reactions will no longer trigger if user/server is blacklisted or if there is a filtered word or invite in it. If custom reaction is triggered, no commands will be attempted to be triggered after it (if your CR has the same name as some command, only your CR will be ran)

This commit is contained in:
Kwoth 2016-11-05 20:03:50 +01:00
parent 5c20c88253
commit a7fefead64
2 changed files with 47 additions and 40 deletions

View File

@ -19,6 +19,7 @@ namespace NadekoBot.Modules.CustomReactions
{ {
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>>();
static CustomReactions() static CustomReactions()
{ {
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
@ -30,49 +31,43 @@ namespace NadekoBot.Modules.CustomReactions
} }
public CustomReactions(ILocalization loc, CommandService cmds, ShardedDiscordClient client) : base(loc, cmds, client) public CustomReactions(ILocalization loc, CommandService cmds, ShardedDiscordClient client) : base(loc, cmds, client)
{ {
client.MessageReceived += (imsg) => }
public static async Task<bool> TryExecuteCustomReaction(IUserMessage umsg)
{
var channel = umsg.Channel as ITextChannel;
if (channel == null)
return false;
var content = umsg.Content.Trim().ToLowerInvariant();
ConcurrentHashSet<CustomReaction> reactions;
GuildReactions.TryGetValue(channel.Guild.Id, out reactions);
if (reactions != null && reactions.Any())
{ {
var umsg = imsg as IUserMessage; var reaction = reactions.Where(cr => {
if (umsg == null || imsg.Author.IsBot) var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%");
return Task.CompletedTask; var trigger = cr.TriggerWithContext(umsg).Trim().ToLowerInvariant();
return ((hasTarget && content.StartsWith(trigger + " ")) || content == trigger);
var channel = umsg.Channel as ITextChannel; }).Shuffle().FirstOrDefault();
if (channel == null) if (reaction != null)
return Task.CompletedTask;
var t = Task.Run(async () =>
{ {
var content = umsg.Content.Trim().ToLowerInvariant(); try { await channel.SendMessageAsync(reaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }
ConcurrentHashSet<CustomReaction> reactions; return true;
GuildReactions.TryGetValue(channel.Guild.Id, out reactions); }
if (reactions != null && reactions.Any()) }
{ var greaction = GlobalReactions.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)
{
try { await channel.SendMessageAsync(reaction.ResponseWithContext(umsg)).ConfigureAwait(false); } catch { }
return;
}
}
var greaction = GlobalReactions.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 (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 { }
return; return true;
} }
}); return false;
return Task.CompletedTask;
};
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]

View File

@ -18,6 +18,7 @@ using static NadekoBot.Modules.Permissions.Permissions;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using NadekoBot.Modules.Help; using NadekoBot.Modules.Help;
using static NadekoBot.Modules.Administration.Administration; using static NadekoBot.Modules.Administration.Administration;
using NadekoBot.Modules.CustomReactions;
namespace NadekoBot.Services namespace NadekoBot.Services
{ {
@ -121,6 +122,17 @@ namespace NadekoBot.Services
return; return;
} }
try
{
// maybe this message is a custom reaction
var crExecuted = await CustomReactions.TryExecuteCustomReaction(usrMsg).ConfigureAwait(false);
//if it was, don't execute the command
if (crExecuted)
return;
}
catch { }
var throwaway = Task.Run(async () => var throwaway = Task.Run(async () =>
{ {
var sw = new Stopwatch(); var sw = new Stopwatch();