From 58cdb04ab9bb4a9e3ce8ee6f7019cb01d2a977ad Mon Sep 17 00:00:00 2001 From: Ske Date: Mon, 23 Dec 2019 01:58:10 +0100 Subject: [PATCH] Practice better disposal hygiene --- PluralKit.Bot/Services/ProxyService.cs | 2 +- .../Services/WebhookExecutorService.cs | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/PluralKit.Bot/Services/ProxyService.cs b/PluralKit.Bot/Services/ProxyService.cs index 98e5baad..5573f24b 100644 --- a/PluralKit.Bot/Services/ProxyService.cs +++ b/PluralKit.Bot/Services/ProxyService.cs @@ -95,7 +95,7 @@ namespace PluralKit.Bot if (!await EnsureBotPermissions(channel)) return; // Can't proxy a message with no content and no attachment - if (match.InnerText.Trim().Length == 0 && message.Attachments.Count == 0) + if (match.InnerText.Trim().Length == 0/* && message.Attachments.Count == 0*/) // TODO: undo when adding attachments back return; // Get variables in order and all diff --git a/PluralKit.Bot/Services/WebhookExecutorService.cs b/PluralKit.Bot/Services/WebhookExecutorService.cs index 5ced39c4..86294fe7 100644 --- a/PluralKit.Bot/Services/WebhookExecutorService.cs +++ b/PluralKit.Bot/Services/WebhookExecutorService.cs @@ -53,11 +53,13 @@ namespace PluralKit.Bot IReadOnlyCollection attachments, bool hasRetried = false) { - var mfd = new MultipartFormDataContent(); - mfd.Add(new StringContent(content.Truncate(2000)), "content"); - mfd.Add(new StringContent(FixClyde(name).Truncate(80)), "username"); + using var mfd = new MultipartFormDataContent + { + {new StringContent(content.Truncate(2000)), "content"}, + {new StringContent(FixClyde(name).Truncate(80)), "username"} + }; if (avatarUrl != null) mfd.Add(new StringContent(avatarUrl), "avatar_url"); - + /*var attachmentChunks = ChunkAttachmentsOrThrow(attachments, 8 * 1024 * 1024); if (attachmentChunks.Count > 0) { @@ -65,9 +67,10 @@ namespace PluralKit.Bot await AddAttachmentsToMultipart(mfd, attachmentChunks.First()); }*/ - HttpResponseMessage response; - using (_metrics.Measure.Timer.Time(BotMetrics.WebhookResponseTime)) - response = await _client.PostAsync($"{DiscordConfig.APIUrl}webhooks/{webhook.Id}/{webhook.Token}?wait=true", mfd); + + var timerCtx = _metrics.Measure.Timer.Time(BotMetrics.WebhookResponseTime); + using var response = await _client.PostAsync($"{DiscordConfig.APIUrl}webhooks/{webhook.Id}/{webhook.Token}?wait=true", mfd); + timerCtx.Dispose(); // TODO: are there cases where an error won't also return a parseable JSON object? var responseJson = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());