Fix cache error using log channel

This commit is contained in:
Ske 2020-11-15 15:07:20 +01:00
parent fdcce05da1
commit c0c4871f0b
4 changed files with 16 additions and 17 deletions

View File

@ -73,7 +73,7 @@ namespace PluralKit.Bot
return; return;
if (await TryHandleCommand(shard, evt, ctx)) if (await TryHandleCommand(shard, evt, ctx))
return; return;
await TryHandleProxy(evt, ctx); await TryHandleProxy(shard, evt, ctx);
} }
private async ValueTask<bool> TryHandleLogClean(MessageCreateEventArgs evt, MessageContext ctx) private async ValueTask<bool> TryHandleLogClean(MessageCreateEventArgs evt, MessageContext ctx)
@ -133,11 +133,11 @@ namespace PluralKit.Bot
return false; return false;
} }
private async ValueTask<bool> TryHandleProxy(MessageCreateEventArgs evt, MessageContext ctx) private async ValueTask<bool> TryHandleProxy(DiscordClient shard, MessageCreateEventArgs evt, MessageContext ctx)
{ {
try try
{ {
return await _proxy.HandleIncomingMessage(evt.Message, ctx, allowAutoproxy: true); return await _proxy.HandleIncomingMessage(shard, evt.Message, ctx, allowAutoproxy: true);
} }
catch (PKError e) catch (PKError e)
{ {

View File

@ -44,7 +44,7 @@ namespace PluralKit.Bot
await using (var conn = await _db.Obtain()) await using (var conn = await _db.Obtain())
using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime)) using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
ctx = await _repo.GetMessageContext(conn, evt.Author.Id, evt.Channel.GuildId, evt.Channel.Id); ctx = await _repo.GetMessageContext(conn, evt.Author.Id, evt.Channel.GuildId, evt.Channel.Id);
await _proxy.HandleIncomingMessage(evt.Message, ctx, allowAutoproxy: false); await _proxy.HandleIncomingMessage(shard, evt.Message, ctx, allowAutoproxy: false);
} }
} }
} }

View File

@ -39,7 +39,7 @@ namespace PluralKit.Bot
_logger = logger.ForContext<ProxyService>(); _logger = logger.ForContext<ProxyService>();
} }
public async Task<bool> HandleIncomingMessage(DiscordMessage message, MessageContext ctx, bool allowAutoproxy) public async Task<bool> HandleIncomingMessage(DiscordClient shard, DiscordMessage message, MessageContext ctx, bool allowAutoproxy)
{ {
if (!ShouldProxy(message, ctx)) return false; if (!ShouldProxy(message, ctx)) return false;
@ -64,7 +64,7 @@ namespace PluralKit.Bot
var allowEmbeds = (senderPermissions & Permissions.EmbedLinks) != 0; var allowEmbeds = (senderPermissions & Permissions.EmbedLinks) != 0;
// Everything's in order, we can execute the proxy! // Everything's in order, we can execute the proxy!
await ExecuteProxy(conn, message, ctx, match, allowEveryone, allowEmbeds); await ExecuteProxy(shard, conn, message, ctx, match, allowEveryone, allowEmbeds);
return true; return true;
} }
@ -90,7 +90,7 @@ namespace PluralKit.Bot
return true; return true;
} }
private async Task ExecuteProxy(IPKConnection conn, DiscordMessage trigger, MessageContext ctx, private async Task ExecuteProxy(DiscordClient shard, IPKConnection conn, DiscordMessage trigger, MessageContext ctx,
ProxyMatch match, bool allowEveryone, bool allowEmbeds) ProxyMatch match, bool allowEveryone, bool allowEmbeds)
{ {
// Send the webhook // Send the webhook
@ -100,10 +100,10 @@ namespace PluralKit.Bot
match.Member.ProxyAvatar(ctx), match.Member.ProxyAvatar(ctx),
content, trigger.Attachments, allowEveryone); content, trigger.Attachments, allowEveryone);
await HandleProxyExecutedActions(conn, ctx, trigger, proxyMessage, match); await HandleProxyExecutedActions(shard, conn, ctx, trigger, proxyMessage, match);
} }
private async Task HandleProxyExecutedActions(IPKConnection conn, MessageContext ctx, private async Task HandleProxyExecutedActions(DiscordClient shard, IPKConnection conn, MessageContext ctx,
DiscordMessage triggerMessage, DiscordMessage proxyMessage, DiscordMessage triggerMessage, DiscordMessage proxyMessage,
ProxyMatch match) ProxyMatch match)
{ {
@ -117,7 +117,7 @@ namespace PluralKit.Bot
Sender = triggerMessage.Author.Id Sender = triggerMessage.Author.Id
}); });
Task LogMessageToChannel() => _logChannel.LogMessage(ctx, match, triggerMessage, proxyMessage.Id).AsTask(); Task LogMessageToChannel() => _logChannel.LogMessage(shard, ctx, match, triggerMessage, proxyMessage.Id).AsTask();
async Task DeleteProxyTriggerMessage() async Task DeleteProxyTriggerMessage()
{ {

View File

@ -15,23 +15,21 @@ namespace PluralKit.Bot {
private readonly IDatabase _db; private readonly IDatabase _db;
private readonly ModelRepository _repo; private readonly ModelRepository _repo;
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly DiscordRestClient _rest;
public LogChannelService(EmbedService embed, ILogger logger, DiscordRestClient rest, IDatabase db, ModelRepository repo) public LogChannelService(EmbedService embed, ILogger logger, IDatabase db, ModelRepository repo)
{ {
_embed = embed; _embed = embed;
_rest = rest;
_db = db; _db = db;
_repo = repo; _repo = repo;
_logger = logger.ForContext<LogChannelService>(); _logger = logger.ForContext<LogChannelService>();
} }
public async ValueTask LogMessage(MessageContext ctx, ProxyMatch proxy, DiscordMessage trigger, ulong hookMessage) public async ValueTask LogMessage(DiscordClient client, MessageContext ctx, ProxyMatch proxy, DiscordMessage trigger, ulong hookMessage)
{ {
if (ctx.SystemId == null || ctx.LogChannel == null || ctx.InLogBlacklist) return; if (ctx.SystemId == null || ctx.LogChannel == null || ctx.InLogBlacklist) return;
// Find log channel and check if valid // Find log channel and check if valid
var logChannel = await FindLogChannel(trigger.Channel.GuildId, ctx.LogChannel.Value); var logChannel = await FindLogChannel(client, trigger.Channel.GuildId, ctx.LogChannel.Value);
if (logChannel == null || logChannel.Type != ChannelType.Text) return; if (logChannel == null || logChannel.Type != ChannelType.Text) return;
// Check bot permissions // Check bot permissions
@ -52,9 +50,10 @@ namespace PluralKit.Bot {
await logChannel.SendMessageFixedAsync(content: url, embed: embed); await logChannel.SendMessageFixedAsync(content: url, embed: embed);
} }
private async Task<DiscordChannel> FindLogChannel(ulong guild, ulong channel) private async Task<DiscordChannel> FindLogChannel(DiscordClient client, ulong guild, ulong channel)
{ {
var obj = await _rest.GetChannel(channel); // MUST use this client here, otherwise we get strange cache issues where the guild doesn't exist... >.>
var obj = await client.GetChannel(channel);
if (obj == null) if (obj == null)
{ {