From eefbaf0c1d60ff488b7d139142d0e16e736dadd1 Mon Sep 17 00:00:00 2001 From: spiral Date: Sat, 15 Apr 2023 12:10:04 -0400 Subject: [PATCH] feat(bot): correctly proxy voice messages --- Myriad/Rest/BaseRestClient.cs | 2 +- Myriad/Rest/Types/MultipartFile.cs | 2 +- Myriad/Rest/Types/Requests/ExecuteWebhookRequest.cs | 1 + Myriad/Types/Message.cs | 7 +++++-- PluralKit.Bot/Commands/ImportExport.cs | 2 +- PluralKit.Bot/Commands/Message.cs | 2 +- PluralKit.Bot/Commands/SystemEdit.cs | 2 +- PluralKit.Bot/Proxy/ProxyService.cs | 3 ++- PluralKit.Bot/Services/WebhookExecutorService.cs | 11 ++++++++--- 9 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Myriad/Rest/BaseRestClient.cs b/Myriad/Rest/BaseRestClient.cs index 1cb93a23..9b11f30f 100644 --- a/Myriad/Rest/BaseRestClient.cs +++ b/Myriad/Rest/BaseRestClient.cs @@ -150,7 +150,7 @@ public class BaseRestClient: IAsyncDisposable if (files != null) for (var i = 0; i < files.Length; i++) { - var (filename, stream, _) = files[i]; + var (filename, stream, _, _, _) = files[i]; mfd.Add(new StreamContent(stream), $"files[{i}]", filename); } diff --git a/Myriad/Rest/Types/MultipartFile.cs b/Myriad/Rest/Types/MultipartFile.cs index b20c6cb4..2f7c1d55 100644 --- a/Myriad/Rest/Types/MultipartFile.cs +++ b/Myriad/Rest/Types/MultipartFile.cs @@ -1,3 +1,3 @@ namespace Myriad.Rest.Types; -public record MultipartFile(string Filename, Stream Data, string? Description); \ No newline at end of file +public record MultipartFile(string Filename, Stream Data, string? Description, string? Waveform, float? DurationSecs); \ No newline at end of file diff --git a/Myriad/Rest/Types/Requests/ExecuteWebhookRequest.cs b/Myriad/Rest/Types/Requests/ExecuteWebhookRequest.cs index 011ff652..28316e36 100644 --- a/Myriad/Rest/Types/Requests/ExecuteWebhookRequest.cs +++ b/Myriad/Rest/Types/Requests/ExecuteWebhookRequest.cs @@ -11,4 +11,5 @@ public record ExecuteWebhookRequest public Sticker[] Stickers { get; init; } public Message.Attachment[] Attachments { get; set; } public AllowedMentions? AllowedMentions { get; init; } + public Message.MessageFlags? Flags { get; set; } } \ No newline at end of file diff --git a/Myriad/Types/Message.cs b/Myriad/Types/Message.cs index 3016ccbf..b12ed142 100644 --- a/Myriad/Types/Message.cs +++ b/Myriad/Types/Message.cs @@ -14,7 +14,8 @@ public record Message SuppressEmbeds = 1 << 2, SourceMessageDeleted = 1 << 3, Urgent = 1 << 4, - Ephemeral = 1 << 6 + Ephemeral = 1 << 6, + VoiceMessage = 1 << 13, } public enum MessageType @@ -64,7 +65,7 @@ public record Message public ulong? ApplicationId { get; init; } public MessageType Type { get; init; } public Reference? MessageReference { get; set; } - // public MessageFlags Flags { get; init; } + public MessageFlags Flags { get; init; } [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public Optional ReferencedMessage { get; init; } @@ -83,6 +84,8 @@ public record Message public int Size { get; init; } public string Url { get; init; } public string ProxyUrl { get; init; } + public string? Waveform { get; init; } + public float? DurationSecs { get; init; } // public int? Width { get; init; } // public int? Height { get; init; } } diff --git a/PluralKit.Bot/Commands/ImportExport.cs b/PluralKit.Bot/Commands/ImportExport.cs index 99b3e680..e5dbff70 100644 --- a/PluralKit.Bot/Commands/ImportExport.cs +++ b/PluralKit.Bot/Commands/ImportExport.cs @@ -121,7 +121,7 @@ public class ImportExport var msg = await ctx.Rest.CreateMessage(dm, new MessageRequest { Content = $"{Emojis.Success} Here you go!" }, - new[] { new MultipartFile("system.json", stream, null) }); + new[] { new MultipartFile("system.json", stream, null, null, null) }); await ctx.Rest.CreateMessage(dm, new MessageRequest { Content = $"<{msg.Attachments[0].Url}>" }); // If the original message wasn't posted in DMs, send a public reminder diff --git a/PluralKit.Bot/Commands/Message.cs b/PluralKit.Bot/Commands/Message.cs index 63c4876a..4bbba674 100644 --- a/PluralKit.Bot/Commands/Message.cs +++ b/PluralKit.Bot/Commands/Message.cs @@ -285,7 +285,7 @@ public class ProxiedMessage { Content = $"{Emojis.Warn} Message contains codeblocks, raw source sent as an attachment." }, - new[] { new MultipartFile("message.txt", stream, null) }); + new[] { new MultipartFile("message.txt", stream, null, null, null) }); } return; diff --git a/PluralKit.Bot/Commands/SystemEdit.cs b/PluralKit.Bot/Commands/SystemEdit.cs index 31136d66..e62bdb7d 100644 --- a/PluralKit.Bot/Commands/SystemEdit.cs +++ b/PluralKit.Bot/Commands/SystemEdit.cs @@ -562,7 +562,7 @@ public class SystemEdit var dm = await _dmCache.GetOrCreateDmChannel(ctx.Author.Id); var msg = await ctx.Rest.CreateMessage(dm, new MessageRequest { Content = $"{Emojis.Success} System deleted. If you want to set up your PluralKit system again, you can import the file below with `pk;import`." }, - new[] { new MultipartFile("system.json", stream, null) }); + new[] { new MultipartFile("system.json", stream, null, null, null) }); await ctx.Rest.CreateMessage(dm, new MessageRequest { Content = $"<{msg.Attachments[0].Url}>" }); // If the original message wasn't posted in DMs, send a public reminder diff --git a/PluralKit.Bot/Proxy/ProxyService.cs b/PluralKit.Bot/Proxy/ProxyService.cs index 9e320d9c..b2f82a38 100644 --- a/PluralKit.Bot/Proxy/ProxyService.cs +++ b/PluralKit.Bot/Proxy/ProxyService.cs @@ -217,7 +217,8 @@ public class ProxyService FileSizeLimit = guild.FileSizeLimit(), Embeds = embeds.ToArray(), Stickers = trigger.StickerItems, - AllowEveryone = allowEveryone + AllowEveryone = allowEveryone, + Flags = trigger.Flags.HasFlag(Message.MessageFlags.VoiceMessage) ? Message.MessageFlags.VoiceMessage : null, }); await HandleProxyExecutedActions(ctx, autoproxySettings, trigger, proxyMessage, match); } diff --git a/PluralKit.Bot/Services/WebhookExecutorService.cs b/PluralKit.Bot/Services/WebhookExecutorService.cs index 505a1f73..ac97b23e 100644 --- a/PluralKit.Bot/Services/WebhookExecutorService.cs +++ b/PluralKit.Bot/Services/WebhookExecutorService.cs @@ -43,6 +43,7 @@ public record ProxyRequest public Embed[] Embeds { get; init; } public Sticker[] Stickers { get; init; } public bool AllowEveryone { get; init; } + public Message.MessageFlags? Flags { get; init; } } public class WebhookExecutorService @@ -129,6 +130,7 @@ public class WebhookExecutorService AvatarUrl = !string.IsNullOrWhiteSpace(req.AvatarUrl) ? req.AvatarUrl : null, Embeds = req.Embeds, Stickers = req.Stickers, + Flags = req.Flags, }; MultipartFile[] files = null; @@ -144,7 +146,9 @@ public class WebhookExecutorService { Id = (ulong)Array.IndexOf(files, f), Description = f.Description, - Filename = f.Filename + Filename = f.Filename, + Waveform = f.Waveform, + DurationSecs = f.DurationSecs }).ToArray(); } @@ -226,7 +230,8 @@ public class WebhookExecutorService { Id = (ulong)Array.IndexOf(files, f), Description = f.Description, - Filename = f.Filename + Filename = f.Filename, + Waveform = f.Waveform }).ToArray() }; await _rest.ExecuteWebhook(webhook.Id, webhook.Token!, req, files, threadId); @@ -240,7 +245,7 @@ public class WebhookExecutorService var attachmentResponse = await _client.GetAsync(attachment.Url, HttpCompletionOption.ResponseHeadersRead); return new MultipartFile(attachment.Filename, await attachmentResponse.Content.ReadAsStreamAsync(), - attachment.Description); + attachment.Description, attachment.Waveform, attachment.DurationSecs); } return await Task.WhenAll(attachments.Select(GetStream));