From 3dfe5b8d55b35073d0d8e360c9778be3d070f48a Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Sat, 23 Sep 2017 00:42:15 +0200 Subject: [PATCH] Fixed game, and other redis events when multiple bots are hosted on the same machine --- .../Services/CustomReactionsService.cs | 8 ++--- .../Modules/Searches/JokeCommands.cs | 33 +++---------------- .../Modules/Searches/Services/FeedsService.cs | 2 +- .../Searches/Services/SearchesService.cs | 27 +++++++++++++++ src/NadekoBot/NadekoBot.cs | 11 +++---- src/NadekoBot/Services/Impl/RedisCache.cs | 5 ++- 6 files changed, 46 insertions(+), 40 deletions(-) diff --git a/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs b/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs index 4cada529..3f965dda 100644 --- a/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs +++ b/src/NadekoBot/Modules/CustomReactions/Services/CustomReactionsService.cs @@ -49,12 +49,12 @@ namespace NadekoBot.Modules.CustomReactions.Services _cache = cache; var sub = _cache.Redis.GetSubscriber(); - sub.Subscribe("gcr.added", (ch, msg) => + sub.Subscribe(_client.CurrentUser.Id + "_gcr.added", (ch, msg) => { Array.Resize(ref GlobalReactions, GlobalReactions.Length + 1); GlobalReactions[GlobalReactions.Length - 1] = JsonConvert.DeserializeObject(msg); }, StackExchange.Redis.CommandFlags.FireAndForget); - sub.Subscribe("gcr.deleted", (ch, msg) => + sub.Subscribe(_client.CurrentUser.Id + "_gcr.deleted", (ch, msg) => { var id = int.Parse(msg); GlobalReactions = GlobalReactions.Where(cr => cr?.Id != id).ToArray(); @@ -69,13 +69,13 @@ namespace NadekoBot.Modules.CustomReactions.Services public Task AddGcr(CustomReaction cr) { var sub = _cache.Redis.GetSubscriber(); - return sub.PublishAsync("gcr.added", JsonConvert.SerializeObject(cr)); + return sub.PublishAsync(_client.CurrentUser.Id + "_gcr.added", JsonConvert.SerializeObject(cr)); } public Task DelGcr(int id) { var sub = _cache.Redis.GetSubscriber(); - return sub.PublishAsync("gcr.deleted", id); + return sub.PublishAsync(_client.CurrentUser.Id + "_gcr.deleted", id); } public void ClearStats() => ReactionStats.Clear(); diff --git a/src/NadekoBot/Modules/Searches/JokeCommands.cs b/src/NadekoBot/Modules/Searches/JokeCommands.cs index 3565b482..c7db2dbd 100644 --- a/src/NadekoBot/Modules/Searches/JokeCommands.cs +++ b/src/NadekoBot/Modules/Searches/JokeCommands.cs @@ -1,10 +1,7 @@ -using AngleSharp; -using Discord.Commands; +using Discord.Commands; using NadekoBot.Extensions; using NadekoBot.Modules.Searches.Services; -using Newtonsoft.Json.Linq; using System.Linq; -using System.Net.Http; using System.Threading.Tasks; using NadekoBot.Common; using NadekoBot.Common.Attributes; @@ -20,40 +17,20 @@ namespace NadekoBot.Modules.Searches [NadekoCommand, Usage, Description, Aliases] public async Task Yomama() { - using (var http = new HttpClient()) - { - var response = await http.GetStringAsync("http://api.yomomma.info/").ConfigureAwait(false); - await Context.Channel.SendConfirmAsync(JObject.Parse(response)["joke"].ToString() + " 😆").ConfigureAwait(false); - } + await Context.Channel.SendConfirmAsync(await _service.GetYomamaJoke()).ConfigureAwait(false); } [NadekoCommand, Usage, Description, Aliases] public async Task Randjoke() { - using (var http = new HttpClient()) - { - http.AddFakeHeaders(); - - var config = Configuration.Default.WithDefaultLoader(); - var document = await BrowsingContext.New(config).OpenAsync("http://www.goodbadjokes.com/random"); - - var html = document.QuerySelector(".post > .joke-content"); - - var part1 = html.QuerySelector("dt").TextContent; - var part2 = html.QuerySelector("dd").TextContent; - - await Context.Channel.SendConfirmAsync("", part1 + "\n\n" + part2, footer: document.BaseUri).ConfigureAwait(false); - } + var jokeInfo = await _service.GetRandomJoke(); + await Context.Channel.SendConfirmAsync("", jokeInfo.Text, footer: jokeInfo.BaseUri).ConfigureAwait(false); } [NadekoCommand, Usage, Description, Aliases] public async Task ChuckNorris() { - using (var http = new HttpClient()) - { - var response = await http.GetStringAsync("http://api.icndb.com/jokes/random/").ConfigureAwait(false); - await Context.Channel.SendConfirmAsync(JObject.Parse(response)["value"]["joke"].ToString() + " 😆").ConfigureAwait(false); - } + await Context.Channel.SendConfirmAsync(await _service.GetChuckNorrisJoke()).ConfigureAwait(false); } [NadekoCommand, Usage, Description, Aliases] diff --git a/src/NadekoBot/Modules/Searches/Services/FeedsService.cs b/src/NadekoBot/Modules/Searches/Services/FeedsService.cs index c0eae9d5..33ac0ff9 100644 --- a/src/NadekoBot/Modules/Searches/Services/FeedsService.cs +++ b/src/NadekoBot/Modules/Searches/Services/FeedsService.cs @@ -125,7 +125,7 @@ namespace NadekoBot.Modules.Searches.Services await Task.WhenAll(sendTasks).ConfigureAwait(false); } } - catch (Exception ex) { Console.WriteLine(ex); } + catch { } } await Task.Delay(10000); diff --git a/src/NadekoBot/Modules/Searches/Services/SearchesService.cs b/src/NadekoBot/Modules/Searches/Services/SearchesService.cs index ed87452a..be785bf3 100644 --- a/src/NadekoBot/Modules/Searches/Services/SearchesService.cs +++ b/src/NadekoBot/Modules/Searches/Services/SearchesService.cs @@ -15,6 +15,8 @@ using System.Linq; using Microsoft.EntityFrameworkCore; using NadekoBot.Modules.NSFW.Exceptions; using System.Net.Http; +using Newtonsoft.Json.Linq; +using AngleSharp; namespace NadekoBot.Modules.Searches.Services { @@ -195,6 +197,31 @@ namespace NadekoBot.Modules.Searches.Services c.Value?.Clear(); } } + + public async Task GetYomamaJoke() + { + var response = await Http.GetStringAsync("http://api.yomomma.info/").ConfigureAwait(false); + return JObject.Parse(response)["joke"].ToString() + " 😆"; + } + + public async Task<(string Text, string BaseUri)> GetRandomJoke() + { + var config = Configuration.Default.WithDefaultLoader(); + var document = await BrowsingContext.New(config).OpenAsync("http://www.goodbadjokes.com/random"); + + var html = document.QuerySelector(".post > .joke-content"); + + var part1 = html.QuerySelector("dt").TextContent; + var part2 = html.QuerySelector("dd").TextContent; + + return (part1 + "\n\n" + part2, document.BaseUri); + } + + public async Task GetChuckNorrisJoke() + { + var response = await Http.GetStringAsync("http://api.icndb.com/jokes/random/").ConfigureAwait(false); + return JObject.Parse(response)["value"]["joke"].ToString() + " 😆"; + } } public struct UserChannelPair diff --git a/src/NadekoBot/NadekoBot.cs b/src/NadekoBot/NadekoBot.cs index f6a87c11..946ba5c3 100644 --- a/src/NadekoBot/NadekoBot.cs +++ b/src/NadekoBot/NadekoBot.cs @@ -152,7 +152,7 @@ namespace NadekoBot .AddManual>(AllGuildConfigs) //todo wrap this .AddManual(this) .AddManual(uow) - .AddManual(new RedisCache()) + .AddManual(new RedisCache(Client.CurrentUser.Id)) .LoadFrom(Assembly.GetEntryAssembly()) .Build(); @@ -167,7 +167,6 @@ namespace NadekoBot CommandService.AddTypeReader(new ModuleOrCrTypeReader(CommandService)); CommandService.AddTypeReader(new GuildTypeReader(Client)); CommandService.AddTypeReader(new GuildDateTimeTypeReader()); - } } @@ -330,7 +329,7 @@ namespace NadekoBot private void HandleStatusChanges() { var sub = Services.GetService().Redis.GetSubscriber(); - sub.Subscribe("status.game_set", async (ch, game) => + sub.Subscribe(Client.CurrentUser.Id + "_status.game_set", async (ch, game) => { try { @@ -344,7 +343,7 @@ namespace NadekoBot } }, CommandFlags.FireAndForget); - sub.Subscribe("status.stream_set", async (ch, streamData) => + sub.Subscribe(Client.CurrentUser.Id + "_status.stream_set", async (ch, streamData) => { try { @@ -363,14 +362,14 @@ namespace NadekoBot { var obj = new { Name = game }; var sub = Services.GetService().Redis.GetSubscriber(); - return sub.PublishAsync("status.game_set", JsonConvert.SerializeObject(obj)); + return sub.PublishAsync(Client.CurrentUser.Id + "_status.game_set", JsonConvert.SerializeObject(obj)); } public Task SetStreamAsync(string name, string url) { var obj = new { Name = name, Url = url }; var sub = Services.GetService().Redis.GetSubscriber(); - return sub.PublishAsync("status.game_set", JsonConvert.SerializeObject(obj)); + return sub.PublishAsync(Client.CurrentUser.Id + "_status.game_set", JsonConvert.SerializeObject(obj)); } } } diff --git a/src/NadekoBot/Services/Impl/RedisCache.cs b/src/NadekoBot/Services/Impl/RedisCache.cs index 84083ef8..e0951120 100644 --- a/src/NadekoBot/Services/Impl/RedisCache.cs +++ b/src/NadekoBot/Services/Impl/RedisCache.cs @@ -5,11 +5,14 @@ namespace NadekoBot.Services.Impl { public class RedisCache : IDataCache { + private ulong _botid; + public ConnectionMultiplexer Redis { get; } private readonly IDatabase _db; - public RedisCache() + public RedisCache(ulong botId) { + _botid = botId; Redis = ConnectionMultiplexer.Connect("127.0.0.1"); Redis.PreserveAsyncOrder = false; _db = Redis.GetDatabase();