diff --git a/Myriad/Cache/DiscordCacheExtensions.cs b/Myriad/Cache/DiscordCacheExtensions.cs index 08e2e971..0168a904 100644 --- a/Myriad/Cache/DiscordCacheExtensions.cs +++ b/Myriad/Cache/DiscordCacheExtensions.cs @@ -103,7 +103,7 @@ public static class DiscordCacheExtensions public static async Task BotPermissionsIn(this IDiscordCache cache, ulong guildId, ulong channelId) { if (cache is HttpDiscordCache) - return await ((HttpDiscordCache)cache).BotPermissions(guildId, channelId); + return await ((HttpDiscordCache)cache).BotChannelPermissions(guildId, channelId); var channel = await cache.GetRootChannel(guildId, channelId); diff --git a/Myriad/Cache/HTTPDiscordCache.cs b/Myriad/Cache/HTTPDiscordCache.cs index 57d4e9b3..b8d3f21f 100644 --- a/Myriad/Cache/HTTPDiscordCache.cs +++ b/Myriad/Cache/HTTPDiscordCache.cs @@ -11,16 +11,18 @@ public class HttpDiscordCache: IDiscordCache { private readonly ILogger _logger; private readonly HttpClient _client; - private readonly string _cacheEndpoint; + private readonly Uri _cacheEndpoint; + private readonly int _shardCount; private readonly ulong _ownUserId; private readonly JsonSerializerOptions _jsonSerializerOptions; - public HttpDiscordCache(ILogger logger, HttpClient client, string cacheEndpoint, ulong ownUserId) + public HttpDiscordCache(ILogger logger, HttpClient client, string cacheEndpoint, int shardCount, ulong ownUserId) { _logger = logger; _client = client; - _cacheEndpoint = cacheEndpoint; + _cacheEndpoint = new Uri(cacheEndpoint); + _shardCount = shardCount; _ownUserId = ownUserId; _jsonSerializerOptions = new JsonSerializerOptions().ConfigureForMyriad(); } @@ -38,10 +40,14 @@ public class HttpDiscordCache: IDiscordCache public ulong GetOwnUser() => _ownUserId; - // todo: cluster - private async Task QueryCache(string endpoint) + private async Task QueryCache(string endpoint, ulong guildId) { - var response = await _client.GetAsync($"{_cacheEndpoint}{endpoint}"); + var cluster = _cacheEndpoint.Authority; + if (cluster.Contains(".service.consul")) + // int(((guild_id >> 22) % shard_count) / 16) + cluster = $"cluster{(int)(((guildId >> 22) % (ulong)_shardCount) / 16)}.{cluster}"; + + var response = await _client.GetAsync($"{_cacheEndpoint.Scheme}://{cluster}{endpoint}"); if (response.StatusCode == HttpStatusCode.NotFound) return default; @@ -54,10 +60,10 @@ public class HttpDiscordCache: IDiscordCache } public Task TryGetGuild(ulong guildId) - => QueryCache($"/guilds/{guildId}"); + => QueryCache($"/guilds/{guildId}", guildId); public Task TryGetChannel(ulong guildId, ulong channelId) - => QueryCache($"/guilds/{guildId}/channels/{channelId}"); + => QueryCache($"/guilds/{guildId}/channels/{channelId}", guildId); // this should be a GetUserCached method on nirn-proxy (it's always called as GetOrFetchUser) // so just return nothing @@ -65,11 +71,11 @@ public class HttpDiscordCache: IDiscordCache => Task.FromResult(null); public Task TryGetSelfMember(ulong guildId) - => QueryCache($"/guilds/{guildId}/members/@me"); + => QueryCache($"/guilds/{guildId}/members/@me", guildId); - public Task BotPermissions(ulong guildId, ulong channelId) - => QueryCache($"/guilds/{guildId}/channels/{channelId}/permissions/@me"); + public Task BotChannelPermissions(ulong guildId, ulong channelId) + => QueryCache($"/guilds/{guildId}/channels/{channelId}/permissions/@me", guildId); public Task> GetGuildChannels(ulong guildId) - => QueryCache>($"/guilds/{guildId}/channels"); + => QueryCache>($"/guilds/{guildId}/channels", guildId); } \ No newline at end of file diff --git a/PluralKit.Bot/Modules.cs b/PluralKit.Bot/Modules.cs index f3cca508..07faf18a 100644 --- a/PluralKit.Bot/Modules.cs +++ b/PluralKit.Bot/Modules.cs @@ -50,7 +50,7 @@ public class BotModule: Module if (botConfig.HttpCacheUrl != null) return new HttpDiscordCache(c.Resolve(), - c.Resolve(), botConfig.HttpCacheUrl, botConfig.ClientId); + c.Resolve(), botConfig.HttpCacheUrl, botConfig.Cluster?.TotalShards ?? 1, botConfig.ClientId); return new MemoryDiscordCache(botConfig.ClientId); }).AsSelf().SingleInstance();