From 4327741ebc304adae48c5a2529cf8d26286c3451 Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sun, 24 Sep 2017 08:56:36 +0200 Subject: [PATCH] Fixed .crca .crdm and .crad for global custom reactions on multiple shards. --- .../CustomReactions/CustomReactions.cs | 22 ++---- .../Services/CustomReactionsService.cs | 70 ++++++++++++++++++- 2 files changed, 74 insertions(+), 18 deletions(-) diff --git a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs index 33af0699..c3fa8383 100644 --- a/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs +++ b/src/NadekoBot/Modules/CustomReactions/CustomReactions.cs @@ -296,12 +296,8 @@ namespace NadekoBot.Modules.CustomReactions } var setValue = reaction.ContainsAnywhere = !reaction.ContainsAnywhere; - - using (var uow = _db.UnitOfWork) - { - uow.CustomReactions.Get(id).ContainsAnywhere = setValue; - uow.Complete(); - } + + await _service.SetCrCaAsync(reaction.Id, setValue).ConfigureAwait(false); if (setValue) { @@ -348,11 +344,7 @@ namespace NadekoBot.Modules.CustomReactions var setValue = reaction.DmResponse = !reaction.DmResponse; - using (var uow = _db.UnitOfWork) - { - uow.CustomReactions.Get(id).DmResponse = setValue; - uow.Complete(); - } + await _service.SetCrDmAsync(reaction.Id, setValue).ConfigureAwait(false); if (setValue) { @@ -398,12 +390,8 @@ namespace NadekoBot.Modules.CustomReactions } var setValue = reaction.AutoDeleteTrigger = !reaction.AutoDeleteTrigger; - - using (var uow = _db.UnitOfWork) - { - uow.CustomReactions.Get(id).AutoDeleteTrigger = setValue; - uow.Complete(); - } + + await _service.SetCrAdAsync(reaction.Id, setValue).ConfigureAwait(false); if (setValue) { diff --git a/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs b/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs index 3f965dda..f9b1d4d8 100644 --- a/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs +++ b/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs @@ -59,6 +59,31 @@ namespace NadekoBot.Modules.CustomReactions.Services var id = int.Parse(msg); GlobalReactions = GlobalReactions.Where(cr => cr?.Id != id).ToArray(); }, StackExchange.Redis.CommandFlags.FireAndForget); + sub.Subscribe(_client.CurrentUser.Id + "_crad.toggle", (ch, msg) => + { + var obj = new { Id = 0, Value = false }; + obj = JsonConvert.DeserializeAnonymousType(msg, obj); + var gcr = GlobalReactions.FirstOrDefault(x => x.Id == obj.Id); + if (gcr != null) + gcr.AutoDeleteTrigger = obj.Value; + }, StackExchange.Redis.CommandFlags.FireAndForget); + sub.Subscribe(_client.CurrentUser.Id + "_crdm.toggle", (ch, msg) => + { + var obj = new { Id = 0, Value = false }; + obj = JsonConvert.DeserializeAnonymousType(msg, obj); + var gcr = GlobalReactions.FirstOrDefault(x => x.Id == obj.Id); + if(gcr != null) + gcr.DmResponse = obj.Value; + }, StackExchange.Redis.CommandFlags.FireAndForget); + sub.Subscribe(_client.CurrentUser.Id + "_crca.toggle", (ch, msg) => + { + var obj = new { Id = 0, Value = false }; + obj = JsonConvert.DeserializeAnonymousType(msg, obj); + var gcr = GlobalReactions.FirstOrDefault(x => x.Id == obj.Id); + if (gcr != null) + gcr.ContainsAnywhere = obj.Value; + }, StackExchange.Redis.CommandFlags.FireAndForget); + var items = uow.CustomReactions.GetAll(); @@ -123,7 +148,11 @@ namespace NadekoBot.Modules.CustomReactions.Services return false; var hasTarget = cr.Response.ToLowerInvariant().Contains("%target%"); var trigger = cr.TriggerWithContext(umsg, _client).Trim().ToLowerInvariant(); - return ((hasTarget && content.StartsWith(trigger + " ")) || (_bc.BotConfig.CustomReactionsStartWith && content.StartsWith(trigger + " ")) || content == trigger); + return ((cr.ContainsAnywhere && + (content.GetWordPosition(trigger) != WordPosition.None)) + || (hasTarget && content.StartsWith(trigger + " ")) + || (_bc.BotConfig.CustomReactionsStartWith && content.StartsWith(trigger + " ")) + || content == trigger); }).ToArray(); if (grs.Length == 0) return null; @@ -171,5 +200,44 @@ namespace NadekoBot.Modules.CustomReactions.Services } return false; } + + public Task SetCrDmAsync(int id, bool setValue) + { + using (var uow = _db.UnitOfWork) + { + uow.CustomReactions.Get(id).DmResponse = setValue; + uow.Complete(); + } + + var sub = _cache.Redis.GetSubscriber(); + var data = new { Id = id, Value = setValue }; + return sub.PublishAsync(_client.CurrentUser.Id + "_crdm.toggle", JsonConvert.SerializeObject(data)); + } + + public Task SetCrAdAsync(int id, bool setValue) + { + using (var uow = _db.UnitOfWork) + { + uow.CustomReactions.Get(id).AutoDeleteTrigger = setValue; + uow.Complete(); + } + + var sub = _cache.Redis.GetSubscriber(); + var data = new { Id = id, Value = setValue }; + return sub.PublishAsync(_client.CurrentUser.Id + "_crad.toggle", JsonConvert.SerializeObject(data)); + } + + public Task SetCrCaAsync(int id, bool setValue) + { + using (var uow = _db.UnitOfWork) + { + uow.CustomReactions.Get(id).ContainsAnywhere = setValue; + uow.Complete(); + } + + var sub = _cache.Redis.GetSubscriber(); + var data = new { Id = id, Value = setValue }; + return sub.PublishAsync(_client.CurrentUser.Id + "_crca.toggle", JsonConvert.SerializeObject(data)); + } } }