run dotnet format
This commit is contained in:
@@ -21,10 +21,12 @@ using Serilog;
|
||||
|
||||
namespace PluralKit.Bot
|
||||
{
|
||||
public class WebhookExecutionErrorOnDiscordsEnd: Exception {
|
||||
public class WebhookExecutionErrorOnDiscordsEnd: Exception
|
||||
{
|
||||
}
|
||||
|
||||
public class WebhookRateLimited: WebhookExecutionErrorOnDiscordsEnd {
|
||||
|
||||
public class WebhookRateLimited: WebhookExecutionErrorOnDiscordsEnd
|
||||
{
|
||||
// Exceptions for control flow? don't mind if I do
|
||||
// TODO: rewrite both of these as a normal exceptional return value (0?) in case of error to be discarded by caller
|
||||
}
|
||||
@@ -41,7 +43,7 @@ namespace PluralKit.Bot
|
||||
public Embed[] Embeds { get; init; }
|
||||
public bool AllowEveryone { get; init; }
|
||||
}
|
||||
|
||||
|
||||
public class WebhookExecutorService
|
||||
{
|
||||
private readonly IDiscordCache _cache;
|
||||
@@ -64,31 +66,32 @@ namespace PluralKit.Bot
|
||||
public async Task<Message> ExecuteWebhook(ProxyRequest req)
|
||||
{
|
||||
_logger.Verbose("Invoking webhook in channel {Channel}", req.ChannelId);
|
||||
|
||||
|
||||
// Get a webhook, execute it
|
||||
var webhook = await _webhookCache.GetWebhook(req.ChannelId);
|
||||
var webhookMessage = await ExecuteWebhookInner(webhook, req);
|
||||
|
||||
|
||||
// Log the relevant metrics
|
||||
_metrics.Measure.Meter.Mark(BotMetrics.MessagesProxied);
|
||||
_logger.Information("Invoked webhook {Webhook} in channel {Channel} (thread {ThreadId})", webhook.Id,
|
||||
req.ChannelId, req.ThreadId);
|
||||
|
||||
|
||||
return webhookMessage;
|
||||
}
|
||||
|
||||
public async Task<Message> EditWebhookMessage(ulong channelId, ulong messageId, string newContent)
|
||||
{
|
||||
var webhook = await _webhookCache.GetWebhook(channelId);
|
||||
var allowedMentions = newContent.ParseMentions() with {
|
||||
var allowedMentions = newContent.ParseMentions() with
|
||||
{
|
||||
Roles = Array.Empty<ulong>(),
|
||||
Parse = Array.Empty<AllowedMentions.ParseType>()
|
||||
};
|
||||
|
||||
return await _rest.EditWebhookMessage(webhook.Id, webhook.Token, messageId,
|
||||
new WebhookMessageEditRequest {Content = newContent, AllowedMentions = allowedMentions});
|
||||
new WebhookMessageEditRequest { Content = newContent, AllowedMentions = allowedMentions });
|
||||
}
|
||||
|
||||
|
||||
private async Task<Message> ExecuteWebhookInner(Webhook webhook, ProxyRequest req, bool hasRetried = false)
|
||||
{
|
||||
var guild = _cache.GetGuild(req.GuildId);
|
||||
@@ -96,7 +99,8 @@ namespace PluralKit.Bot
|
||||
|
||||
var allowedMentions = content.ParseMentions();
|
||||
if (!req.AllowEveryone)
|
||||
allowedMentions = allowedMentions.RemoveUnmentionableRoles(guild) with {
|
||||
allowedMentions = allowedMentions.RemoveUnmentionableRoles(guild) with
|
||||
{
|
||||
// also clear @everyones
|
||||
Parse = Array.Empty<AllowedMentions.ParseType>()
|
||||
};
|
||||
@@ -109,18 +113,19 @@ namespace PluralKit.Bot
|
||||
AvatarUrl = !string.IsNullOrWhiteSpace(req.AvatarUrl) ? req.AvatarUrl : null,
|
||||
Embeds = req.Embeds
|
||||
};
|
||||
|
||||
|
||||
MultipartFile[] files = null;
|
||||
var attachmentChunks = ChunkAttachmentsOrThrow(req.Attachments, 8 * 1024 * 1024);
|
||||
if (attachmentChunks.Count > 0)
|
||||
{
|
||||
_logger.Information("Invoking webhook with {AttachmentCount} attachments totalling {AttachmentSize} MiB in {AttachmentChunks} chunks",
|
||||
_logger.Information("Invoking webhook with {AttachmentCount} attachments totalling {AttachmentSize} MiB in {AttachmentChunks} chunks",
|
||||
req.Attachments.Length, req.Attachments.Select(a => a.Size).Sum() / 1024 / 1024, attachmentChunks.Count);
|
||||
files = await GetAttachmentFiles(attachmentChunks[0]);
|
||||
}
|
||||
|
||||
|
||||
Message webhookMessage;
|
||||
using (_metrics.Measure.Timer.Time(BotMetrics.WebhookResponseTime)) {
|
||||
using (_metrics.Measure.Timer.Time(BotMetrics.WebhookResponseTime))
|
||||
{
|
||||
try
|
||||
{
|
||||
webhookMessage = await _rest.ExecuteWebhook(webhook.Id, webhook.Token, webhookReq, files, req.ThreadId);
|
||||
@@ -139,14 +144,14 @@ namespace PluralKit.Bot
|
||||
// but is still in our cache. Invalidate, refresh, try again
|
||||
_logger.Warning("Error invoking webhook {Webhook} in channel {Channel} (thread {ThreadId})",
|
||||
webhook.Id, webhook.ChannelId, req.ThreadId);
|
||||
|
||||
|
||||
var newWebhook = await _webhookCache.InvalidateAndRefreshWebhook(req.ChannelId, webhook);
|
||||
return await ExecuteWebhookInner(newWebhook, req, hasRetried: true);
|
||||
}
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We don't care about whether the sending succeeds, and we don't want to *wait* for it, so we just fork it off
|
||||
var _ = TrySendRemainingAttachments(webhook, req.Name, req.AvatarUrl, attachmentChunks, req.ThreadId);
|
||||
@@ -161,11 +166,11 @@ namespace PluralKit.Bot
|
||||
for (var i = 1; i < attachmentChunks.Count; i++)
|
||||
{
|
||||
var files = await GetAttachmentFiles(attachmentChunks[i]);
|
||||
var req = new ExecuteWebhookRequest {Username = name, AvatarUrl = avatarUrl};
|
||||
var req = new ExecuteWebhookRequest { Username = name, AvatarUrl = avatarUrl };
|
||||
await _rest.ExecuteWebhook(webhook.Id, webhook.Token!, req, files, threadId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private async Task<MultipartFile[]> GetAttachmentFiles(IReadOnlyCollection<Message.Attachment> attachments)
|
||||
{
|
||||
async Task<MultipartFile> GetStream(Message.Attachment attachment)
|
||||
@@ -184,7 +189,7 @@ namespace PluralKit.Bot
|
||||
// If any individual attachment is larger than 8MB, will throw an error
|
||||
var chunks = new List<List<Message.Attachment>>();
|
||||
var list = new List<Message.Attachment>();
|
||||
|
||||
|
||||
foreach (var attachment in attachments)
|
||||
{
|
||||
if (attachment.Size >= sizeThreshold) throw Errors.AttachmentTooLarge;
|
||||
@@ -194,7 +199,7 @@ namespace PluralKit.Bot
|
||||
chunks.Add(list);
|
||||
list = new List<Message.Attachment>();
|
||||
}
|
||||
|
||||
|
||||
list.Add(attachment);
|
||||
}
|
||||
|
||||
@@ -212,7 +217,7 @@ namespace PluralKit.Bot
|
||||
// since Discord blocks webhooks containing the word "Clyde"... for some reason. /shrug
|
||||
return Regex.Replace(name, "(c)(lyde)", Replacement, RegexOptions.IgnoreCase);
|
||||
}
|
||||
|
||||
|
||||
private string FixSingleCharacterName(string proxyName)
|
||||
{
|
||||
if (proxyName.Length == 1)
|
||||
|
Reference in New Issue
Block a user