Port some things, still does not compile
This commit is contained in:
@@ -3,8 +3,9 @@ using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
using DSharpPlus;
|
||||
using DSharpPlus.Entities;
|
||||
|
||||
using Serilog;
|
||||
|
||||
@@ -15,54 +16,55 @@ namespace PluralKit.Bot
|
||||
public static readonly string WebhookName = "PluralKit Proxy Webhook";
|
||||
|
||||
private DiscordShardedClient _client;
|
||||
private ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>> _webhooks;
|
||||
private ConcurrentDictionary<ulong, Lazy<Task<DiscordWebhook>>> _webhooks;
|
||||
|
||||
private ILogger _logger;
|
||||
|
||||
public WebhookCacheService(IDiscordClient client, ILogger logger)
|
||||
public WebhookCacheService(DiscordShardedClient client, ILogger logger)
|
||||
{
|
||||
_client = client as DiscordShardedClient;
|
||||
_client = client;
|
||||
_logger = logger.ForContext<WebhookCacheService>();
|
||||
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>>();
|
||||
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<DiscordWebhook>>>();
|
||||
}
|
||||
|
||||
public async Task<IWebhook> GetWebhook(ulong channelId)
|
||||
public async Task<DiscordWebhook> GetWebhook(DiscordClient client, ulong channelId)
|
||||
{
|
||||
var channel = _client.GetChannel(channelId) as ITextChannel;
|
||||
var channel = await client.GetChannelAsync(channelId);
|
||||
if (channel == null) return null;
|
||||
if (channel.Type == ChannelType.Text) return null;
|
||||
return await GetWebhook(channel);
|
||||
}
|
||||
|
||||
public async Task<IWebhook> GetWebhook(ITextChannel channel)
|
||||
public async Task<DiscordWebhook> GetWebhook(DiscordChannel 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
|
||||
// webhook from being created twice.
|
||||
var lazyWebhookValue =
|
||||
_webhooks.GetOrAdd(channel.Id, new Lazy<Task<IWebhook>>(() => GetOrCreateWebhook(channel)));
|
||||
_webhooks.GetOrAdd(channel.Id, new Lazy<Task<DiscordWebhook>>(() => GetOrCreateWebhook(channel)));
|
||||
|
||||
// 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.
|
||||
var webhook = await lazyWebhookValue.Value;
|
||||
if (webhook.ChannelId != channel.Id) return await InvalidateAndRefreshWebhook(webhook);
|
||||
if (webhook.ChannelId != channel.Id) return await InvalidateAndRefreshWebhook(channel, webhook);
|
||||
return webhook;
|
||||
}
|
||||
|
||||
public async Task<IWebhook> InvalidateAndRefreshWebhook(IWebhook webhook)
|
||||
public async Task<DiscordWebhook> InvalidateAndRefreshWebhook(DiscordChannel channel, DiscordWebhook webhook)
|
||||
{
|
||||
_logger.Information("Refreshing webhook for channel {Channel}", webhook.ChannelId);
|
||||
|
||||
_webhooks.TryRemove(webhook.ChannelId, out _);
|
||||
return await GetWebhook(webhook.Channel);
|
||||
return await GetWebhook(channel);
|
||||
}
|
||||
|
||||
private async Task<IWebhook> GetOrCreateWebhook(ITextChannel channel)
|
||||
private async Task<DiscordWebhook> GetOrCreateWebhook(DiscordChannel channel)
|
||||
{
|
||||
_logger.Debug("Webhook for channel {Channel} not found in cache, trying to fetch", channel.Id);
|
||||
return await FindExistingWebhook(channel) ?? await DoCreateWebhook(channel);
|
||||
}
|
||||
|
||||
private async Task<IWebhook> FindExistingWebhook(ITextChannel channel)
|
||||
private async Task<DiscordWebhook> FindExistingWebhook(DiscordChannel channel)
|
||||
{
|
||||
_logger.Debug("Finding webhook for channel {Channel}", channel.Id);
|
||||
try
|
||||
@@ -78,13 +80,13 @@ namespace PluralKit.Bot
|
||||
}
|
||||
}
|
||||
|
||||
private Task<IWebhook> DoCreateWebhook(ITextChannel channel)
|
||||
private Task<DiscordWebhook> DoCreateWebhook(DiscordChannel channel)
|
||||
{
|
||||
_logger.Information("Creating new webhook for channel {Channel}", channel.Id);
|
||||
return channel.CreateWebhookAsync(WebhookName);
|
||||
}
|
||||
|
||||
private bool IsWebhookMine(IWebhook arg) => arg.Creator.Id == _client.GetShardFor(arg.Guild).CurrentUser.Id && arg.Name == WebhookName;
|
||||
private bool IsWebhookMine(DiscordWebhook arg) => arg.User.Id == _client.CurrentUser.Id && arg.Name == WebhookName;
|
||||
|
||||
public int CacheSize => _webhooks.Count;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user