Fix line endings in WebhookCacheService
This commit is contained in:
parent
aed38f37f7
commit
6538c5d054
@ -1,92 +1,92 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Webhook;
|
using Discord.Webhook;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
|
||||||
namespace PluralKit.Bot
|
namespace PluralKit.Bot
|
||||||
{
|
{
|
||||||
public class WebhookCacheService
|
public class WebhookCacheService
|
||||||
{
|
{
|
||||||
public static readonly string WebhookName = "PluralKit Proxy Webhook";
|
public static readonly string WebhookName = "PluralKit Proxy Webhook";
|
||||||
|
|
||||||
private DiscordShardedClient _client;
|
private DiscordShardedClient _client;
|
||||||
private ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>> _webhooks;
|
private ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>> _webhooks;
|
||||||
|
|
||||||
private ILogger _logger;
|
private ILogger _logger;
|
||||||
|
|
||||||
public WebhookCacheService(IDiscordClient client, ILogger logger)
|
public WebhookCacheService(IDiscordClient client, ILogger logger)
|
||||||
{
|
{
|
||||||
_client = client as DiscordShardedClient;
|
_client = client as DiscordShardedClient;
|
||||||
_logger = logger.ForContext<WebhookCacheService>();
|
_logger = logger.ForContext<WebhookCacheService>();
|
||||||
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>>();
|
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IWebhook> GetWebhook(ulong channelId)
|
public async Task<IWebhook> GetWebhook(ulong channelId)
|
||||||
{
|
{
|
||||||
var channel = _client.GetChannel(channelId) as ITextChannel;
|
var channel = _client.GetChannel(channelId) as ITextChannel;
|
||||||
if (channel == null) return null;
|
if (channel == null) return null;
|
||||||
return await GetWebhook(channel);
|
return await GetWebhook(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IWebhook> GetWebhook(ITextChannel channel)
|
public async Task<IWebhook> GetWebhook(ITextChannel channel)
|
||||||
{
|
{
|
||||||
// We cache the webhook through a Lazy<Task<T>>, this way we make sure to only create one webhook per channel
|
// We cache the webhook through a Lazy<Task<T>>, this way we make sure to only create one webhook per channel
|
||||||
// If the webhook is requested twice before it's actually been found, the Lazy<T> wrapper will stop the
|
// If the webhook is requested twice before it's actually been found, the Lazy<T> wrapper will stop the
|
||||||
// webhook from being created twice.
|
// webhook from being created twice.
|
||||||
var lazyWebhookValue =
|
var lazyWebhookValue =
|
||||||
_webhooks.GetOrAdd(channel.Id, new Lazy<Task<IWebhook>>(() => GetOrCreateWebhook(channel)));
|
_webhooks.GetOrAdd(channel.Id, new Lazy<Task<IWebhook>>(() => GetOrCreateWebhook(channel)));
|
||||||
|
|
||||||
// It's possible to "move" a webhook to a different channel after creation
|
// It's possible to "move" a webhook to a different channel after creation
|
||||||
// Here, we ensure it's actually still pointing towards the proper channel, and if not, wipe and refetch one.
|
// Here, we ensure it's actually still pointing towards the proper channel, and if not, wipe and refetch one.
|
||||||
var webhook = await lazyWebhookValue.Value;
|
var webhook = await lazyWebhookValue.Value;
|
||||||
if (webhook.ChannelId != channel.Id) return await InvalidateAndRefreshWebhook(webhook);
|
if (webhook.ChannelId != channel.Id) return await InvalidateAndRefreshWebhook(webhook);
|
||||||
return webhook;
|
return webhook;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IWebhook> InvalidateAndRefreshWebhook(IWebhook webhook)
|
public async Task<IWebhook> InvalidateAndRefreshWebhook(IWebhook webhook)
|
||||||
{
|
{
|
||||||
_logger.Information("Refreshing webhook for channel {Channel}", webhook.ChannelId);
|
_logger.Information("Refreshing webhook for channel {Channel}", webhook.ChannelId);
|
||||||
|
|
||||||
_webhooks.TryRemove(webhook.ChannelId, out _);
|
_webhooks.TryRemove(webhook.ChannelId, out _);
|
||||||
return await GetWebhook(webhook.Channel);
|
return await GetWebhook(webhook.Channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<IWebhook> GetOrCreateWebhook(ITextChannel channel)
|
private async Task<IWebhook> GetOrCreateWebhook(ITextChannel channel)
|
||||||
{
|
{
|
||||||
_logger.Debug("Webhook for channel {Channel} not found in cache, trying to fetch", channel.Id);
|
_logger.Debug("Webhook for channel {Channel} not found in cache, trying to fetch", channel.Id);
|
||||||
return await FindExistingWebhook(channel) ?? await DoCreateWebhook(channel);
|
return await FindExistingWebhook(channel) ?? await DoCreateWebhook(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<IWebhook> FindExistingWebhook(ITextChannel channel)
|
private async Task<IWebhook> FindExistingWebhook(ITextChannel channel)
|
||||||
{
|
{
|
||||||
_logger.Debug("Finding webhook for channel {Channel}", channel.Id);
|
_logger.Debug("Finding webhook for channel {Channel}", channel.Id);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return (await channel.GetWebhooksAsync()).FirstOrDefault(IsWebhookMine);
|
return (await channel.GetWebhooksAsync()).FirstOrDefault(IsWebhookMine);
|
||||||
}
|
}
|
||||||
catch (HttpRequestException e)
|
catch (HttpRequestException e)
|
||||||
{
|
{
|
||||||
_logger.Warning(e, "Error occurred while fetching webhook list");
|
_logger.Warning(e, "Error occurred while fetching webhook list");
|
||||||
// This happens sometimes when Discord returns a malformed request for the webhook list
|
// This happens sometimes when Discord returns a malformed request for the webhook list
|
||||||
// Nothing we can do than just assume that none exist and return null.
|
// Nothing we can do than just assume that none exist and return null.
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task<IWebhook> DoCreateWebhook(ITextChannel channel)
|
private Task<IWebhook> DoCreateWebhook(ITextChannel channel)
|
||||||
{
|
{
|
||||||
_logger.Information("Creating new webhook for channel {Channel}", channel.Id);
|
_logger.Information("Creating new webhook for channel {Channel}", channel.Id);
|
||||||
return channel.CreateWebhookAsync(WebhookName);
|
return channel.CreateWebhookAsync(WebhookName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsWebhookMine(IWebhook arg) => arg.Creator.Id == _client.GetShardFor(arg.Guild).CurrentUser.Id && arg.Name == WebhookName;
|
private bool IsWebhookMine(IWebhook arg) => arg.Creator.Id == _client.GetShardFor(arg.Guild).CurrentUser.Id && arg.Name == WebhookName;
|
||||||
|
|
||||||
public int CacheSize => _webhooks.Count;
|
public int CacheSize => _webhooks.Count;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user