images reload and guild leave will now use new pubsub

This commit is contained in:
Kwoth
2021-07-03 00:02:26 +02:00
parent 65b4c1fab7
commit 35d5260538
2 changed files with 40 additions and 47 deletions

View File

@@ -518,7 +518,7 @@ namespace NadekoBot.Modules.Administration
[OwnerOnly] [OwnerOnly]
public async Task ImagesReload() public async Task ImagesReload()
{ {
_service.ReloadImages(); await _service.ReloadImagesAsync();
await ReplyConfirmLocalizedAsync("images_loading", 0).ConfigureAwait(false); await ReplyConfirmLocalizedAsync("images_loading", 0).ConfigureAwait(false);
} }

View File

@@ -21,7 +21,6 @@ namespace NadekoBot.Modules.Administration.Services
{ {
public sealed class SelfService : ILateExecutor, IReadyExecutor, INService public sealed class SelfService : ILateExecutor, IReadyExecutor, INService
{ {
private readonly ConnectionMultiplexer _redis;
private readonly CommandHandler _cmdHandler; private readonly CommandHandler _cmdHandler;
private readonly DbService _db; private readonly DbService _db;
private readonly IBotStrings _strings; private readonly IBotStrings _strings;
@@ -35,7 +34,6 @@ namespace NadekoBot.Modules.Administration.Services
private ConcurrentDictionary<ulong?, ConcurrentDictionary<int, Timer>> _autoCommands = private ConcurrentDictionary<ulong?, ConcurrentDictionary<int, Timer>> _autoCommands =
new ConcurrentDictionary<ulong?, ConcurrentDictionary<int, Timer>>(); new ConcurrentDictionary<ulong?, ConcurrentDictionary<int, Timer>>();
private readonly IDataCache _cache;
private readonly IImageCache _imgs; private readonly IImageCache _imgs;
private readonly IHttpClientFactory _httpFactory; private readonly IHttpClientFactory _httpFactory;
private readonly BotConfigService _bss; private readonly BotConfigService _bss;
@@ -43,63 +41,64 @@ namespace NadekoBot.Modules.Administration.Services
//keys //keys
private readonly TypedKey<ActivityPubData> _activitySetKey; private readonly TypedKey<ActivityPubData> _activitySetKey;
private readonly TypedKey<bool> _imagesReloadKey;
private readonly TypedKey<string> _guildLeaveKey;
public SelfService(DiscordSocketClient client, CommandHandler cmdHandler, DbService db, public SelfService(
IBotStrings strings, IBotCredentials creds, IDataCache cache, IHttpClientFactory factory, DiscordSocketClient client,
BotConfigService bss, IPubSub pubSub) CommandHandler cmdHandler,
DbService db,
IBotStrings strings,
IBotCredentials creds,
IDataCache cache,
IHttpClientFactory factory,
BotConfigService bss,
IPubSub pubSub)
{ {
_redis = cache.Redis;
_cmdHandler = cmdHandler; _cmdHandler = cmdHandler;
_db = db; _db = db;
_strings = strings; _strings = strings;
_client = client; _client = client;
_creds = creds; _creds = creds;
_cache = cache;
_imgs = cache.LocalImages; _imgs = cache.LocalImages;
_httpFactory = factory; _httpFactory = factory;
_bss = bss; _bss = bss;
_pubSub = pubSub; _pubSub = pubSub;
_activitySetKey = new("activity.set"); _activitySetKey = new("activity.set");
_imagesReloadKey = new("images.reload");
_guildLeaveKey = new("guild.leave");
HandleStatusChanges(); HandleStatusChanges();
var sub = _redis.GetSubscriber();
if (_client.ShardId == 0) if (_client.ShardId == 0)
{ {
sub.Subscribe(_creds.RedisKey() + "_reload_images", _pubSub.Sub(_imagesReloadKey, async _ => await _imgs.Reload());
delegate { _imgs.Reload(); }, CommandFlags.FireAndForget);
} }
sub.Subscribe(_creds.RedisKey() + "_leave_guild", async (ch, v) => _pubSub.Sub(_guildLeaveKey, async input =>
{ {
try var guildStr = input.ToString().Trim().ToUpperInvariant();
{ if (string.IsNullOrWhiteSpace(guildStr))
var guildStr = v.ToString()?.Trim().ToUpperInvariant(); return;
if (string.IsNullOrWhiteSpace(guildStr))
return;
var server = _client.Guilds.FirstOrDefault(g => g.Id.ToString() == guildStr) ??
_client.Guilds.FirstOrDefault(g => g.Name.Trim().ToUpperInvariant() == guildStr);
if (server is null) var server = _client.Guilds.FirstOrDefault(g => g.Id.ToString() == guildStr
{ || g.Name.Trim().ToUpperInvariant() == guildStr);
return; if (server is null)
}
if (server.OwnerId != _client.CurrentUser.Id)
{
await server.LeaveAsync().ConfigureAwait(false);
Log.Information($"Left server {server.Name} [{server.Id}]");
}
else
{
await server.DeleteAsync().ConfigureAwait(false);
Log.Information($"Deleted server {server.Name} [{server.Id}]");
}
}
catch
{ {
return;
} }
}, CommandFlags.FireAndForget);
if (server.OwnerId != _client.CurrentUser.Id)
{
await server.LeaveAsync().ConfigureAwait(false);
Log.Information($"Left server {server.Name} [{server.Id}]");
}
else
{
await server.DeleteAsync().ConfigureAwait(false);
Log.Information($"Deleted server {server.Name} [{server.Id}]");
}
});
} }
public async Task OnReadyAsync() public async Task OnReadyAsync()
@@ -228,11 +227,8 @@ namespace NadekoBot.Modules.Administration.Services
Log.Information($"Created {ownerChannels.Count} out of {_creds.OwnerIds.Count} owner message channels."); Log.Information($"Created {ownerChannels.Count} out of {_creds.OwnerIds.Count} owner message channels.");
} }
public Task LeaveGuild(string guildStr) public Task LeaveGuild(string guildStr)
{ => _pubSub.Pub(_guildLeaveKey, guildStr);
var sub = _cache.Redis.GetSubscriber();
return sub.PublishAsync(_creds.RedisKey() + "_leave_guild", guildStr);
}
// forwards dms // forwards dms
public async Task LateExecute(IGuild guild, IUserMessage msg) public async Task LateExecute(IGuild guild, IUserMessage msg)
@@ -373,11 +369,8 @@ namespace NadekoBot.Modules.Administration.Services
} }
} }
public void ReloadImages() public Task ReloadImagesAsync()
{ => _pubSub.Pub(_imagesReloadKey, true);
var sub = _cache.Redis.GetSubscriber();
sub.Publish(_creds.RedisKey() + "_reload_images", "");
}
public bool ForwardMessages() public bool ForwardMessages()
{ {