Add member avatar edit command
This also refactors a large portion of the DI toolchain, since I discovered that you shouldn't be reusing IDbConnection objects. Instead, most services and stores are now declared transient, and the webhook cache has been moved to a database-independent storage singleton by itself.
This commit is contained in:
@@ -31,18 +31,17 @@ namespace PluralKit.Bot
|
||||
private IDiscordClient _client;
|
||||
private IDbConnection _connection;
|
||||
private LogChannelService _logger;
|
||||
private WebhookCacheService _webhookCache;
|
||||
private MessageStore _messageStorage;
|
||||
|
||||
private ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>> _webhooks;
|
||||
|
||||
public ProxyService(IDiscordClient client, IDbConnection connection, LogChannelService logger, MessageStore messageStorage)
|
||||
public ProxyService(IDiscordClient client, WebhookCacheService webhookCache, IDbConnection connection, LogChannelService logger, MessageStore messageStorage)
|
||||
{
|
||||
this._client = client;
|
||||
this._webhookCache = webhookCache;
|
||||
this._connection = connection;
|
||||
this._logger = logger;
|
||||
this._messageStorage = messageStorage;
|
||||
|
||||
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>>();
|
||||
}
|
||||
|
||||
private ProxyMatch GetProxyTagMatch(string message, IEnumerable<ProxyDatabaseResult> potentials) {
|
||||
@@ -70,7 +69,7 @@ namespace PluralKit.Bot
|
||||
if (match == null) return;
|
||||
|
||||
// Fetch a webhook for this channel, and send the proxied message
|
||||
var webhook = await GetWebhookByChannelCaching(message.Channel as ITextChannel);
|
||||
var webhook = await _webhookCache.GetWebhook(message.Channel as ITextChannel);
|
||||
var hookMessage = await ExecuteWebhook(webhook, match.InnerText, match.ProxyName, match.Member.AvatarUrl, message.Attachments.FirstOrDefault());
|
||||
|
||||
// Store the message in the database, and log it in the log channel (if applicable)
|
||||
@@ -96,28 +95,6 @@ namespace PluralKit.Bot
|
||||
return await webhook.Channel.GetMessageAsync(messageId);
|
||||
}
|
||||
|
||||
private async Task<IWebhook> GetWebhookByChannelCaching(ITextChannel channel) {
|
||||
// We cache the webhook through a Lazy<Task<T>>, this way we make sure to only create one webhook per channel
|
||||
// TODO: make sure this is sharding-safe. Intuition says yes, since one channel is guaranteed to only be handled by one shard, but best to make sure
|
||||
var webhookFactory = _webhooks.GetOrAdd(channel.Id, new Lazy<Task<IWebhook>>(() => FindWebhookByChannel(channel)));
|
||||
return await webhookFactory.Value;
|
||||
}
|
||||
|
||||
private async Task<IWebhook> FindWebhookByChannel(ITextChannel channel) {
|
||||
IWebhook webhook;
|
||||
|
||||
webhook = (await channel.GetWebhooksAsync()).FirstOrDefault(IsWebhookMine);
|
||||
if (webhook != null) return webhook;
|
||||
|
||||
webhook = await channel.CreateWebhookAsync("PluralKit Proxy Webhook");
|
||||
return webhook;
|
||||
}
|
||||
|
||||
private bool IsWebhookMine(IWebhook arg)
|
||||
{
|
||||
return arg.Creator.Id == this._client.CurrentUser.Id && arg.Name == "PluralKit Proxy Webhook";
|
||||
}
|
||||
|
||||
public async Task HandleReactionAddedAsync(Cacheable<IUserMessage, ulong> message, ISocketMessageChannel channel, SocketReaction reaction)
|
||||
{
|
||||
// Make sure it's the right emoji (red X)
|
||||
|
48
PluralKit.Bot/Services/WebhookCacheService.cs
Normal file
48
PluralKit.Bot/Services/WebhookCacheService.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace PluralKit.Bot
|
||||
{
|
||||
public class WebhookCacheService
|
||||
{
|
||||
public static readonly string WebhookName = "PluralKit Proxy Webhook";
|
||||
|
||||
private IDiscordClient _client;
|
||||
private ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>> _webhooks;
|
||||
|
||||
public WebhookCacheService(IDiscordClient client)
|
||||
{
|
||||
this._client = client;
|
||||
_webhooks = new ConcurrentDictionary<ulong, Lazy<Task<IWebhook>>>();
|
||||
}
|
||||
|
||||
public async Task<IWebhook> GetWebhook(ulong channelId)
|
||||
{
|
||||
var channel = await _client.GetChannelAsync(channelId) as ITextChannel;
|
||||
if (channel == null) return null;
|
||||
return await GetWebhook(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
|
||||
// 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)));
|
||||
return await lazyWebhookValue.Value;
|
||||
}
|
||||
|
||||
private async Task<IWebhook> GetOrCreateWebhook(ITextChannel channel) =>
|
||||
await FindExistingWebhook(channel) ?? await GetOrCreateWebhook(channel);
|
||||
|
||||
private async Task<IWebhook> FindExistingWebhook(ITextChannel channel) => (await channel.GetWebhooksAsync()).FirstOrDefault(IsWebhookMine);
|
||||
|
||||
private async Task<IWebhook> DoCreateWebhook(ITextChannel channel) => await channel.CreateWebhookAsync(WebhookName);
|
||||
private bool IsWebhookMine(IWebhook arg) => arg.Creator.Id == _client.CurrentUser.Id && arg.Name == WebhookName;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user