Fixed game, and other redis events when multiple bots are hosted on the same machine

This commit is contained in:
Master Kwoth 2017-09-23 00:42:15 +02:00
parent 61496a7c19
commit 3dfe5b8d55
6 changed files with 46 additions and 40 deletions

View File

@ -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<CustomReaction>(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();

View File

@ -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]

View File

@ -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);

View File

@ -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<string> 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<string> GetChuckNorrisJoke()
{
var response = await Http.GetStringAsync("http://api.icndb.com/jokes/random/").ConfigureAwait(false);
return JObject.Parse(response)["value"]["joke"].ToString() + " 😆";
}
}
public struct UserChannelPair

View File

@ -152,7 +152,7 @@ namespace NadekoBot
.AddManual<IEnumerable<GuildConfig>>(AllGuildConfigs) //todo wrap this
.AddManual<NadekoBot>(this)
.AddManual<IUnitOfWork>(uow)
.AddManual<IDataCache>(new RedisCache())
.AddManual<IDataCache>(new RedisCache(Client.CurrentUser.Id))
.LoadFrom(Assembly.GetEntryAssembly())
.Build();
@ -167,7 +167,6 @@ namespace NadekoBot
CommandService.AddTypeReader<ModuleOrCrInfo>(new ModuleOrCrTypeReader(CommandService));
CommandService.AddTypeReader<IGuild>(new GuildTypeReader(Client));
CommandService.AddTypeReader<GuildDateTime>(new GuildDateTimeTypeReader());
}
}
@ -330,7 +329,7 @@ namespace NadekoBot
private void HandleStatusChanges()
{
var sub = Services.GetService<IDataCache>().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<IDataCache>().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<IDataCache>().Redis.GetSubscriber();
return sub.PublishAsync("status.game_set", JsonConvert.SerializeObject(obj));
return sub.PublishAsync(Client.CurrentUser.Id + "_status.game_set", JsonConvert.SerializeObject(obj));
}
}
}

View File

@ -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();