feat: add support for attachment descriptions

This commit is contained in:
spiral 2021-11-19 09:34:52 -05:00
parent 36acb5bae6
commit 0ca356eec3
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
7 changed files with 25 additions and 7 deletions

View File

@ -154,8 +154,8 @@ namespace Myriad.Rest
{
for (var i = 0; i < files.Length; i++)
{
var (filename, stream) = files[i];
mfd.Add(new StreamContent(stream), $"file{i}", filename);
var (filename, stream, _) = files[i];
mfd.Add(new StreamContent(stream), $"files[{i}]", filename);
}
}

View File

@ -2,5 +2,5 @@ using System.IO;
namespace Myriad.Rest.Types
{
public record MultipartFile(string Filename, Stream Data);
public record MultipartFile(string Filename, Stream Data, string? Description);
}

View File

@ -8,6 +8,7 @@ namespace Myriad.Rest.Types.Requests
public string? Username { get; init; }
public string? AvatarUrl { get; init; }
public Embed[] Embeds { get; init; }
public Message.Attachment[] Attachments { get; set; }
public AllowedMentions? AllowedMentions { get; init; }
}
}

View File

@ -71,6 +71,7 @@ namespace Myriad.Types
public record Attachment
{
public ulong Id { get; init; }
public string? Description { get; init; }
public string Filename { get; init; }
public int Size { get; init; }
public string Url { get; init; }

View File

@ -113,7 +113,7 @@ namespace PluralKit.Bot
var msg = await ctx.Rest.CreateMessage(dm.Id,
new MessageRequest { Content = $"{Emojis.Success} Here you go!" },
new[] { new MultipartFile("system.json", stream) });
new[] { new MultipartFile("system.json", stream, null) });
await ctx.Rest.CreateMessage(dm.Id, new MessageRequest { Content = $"<{msg.Attachments[0].Url}>" });
// If the original message wasn't posted in DMs, send a public reminder

View File

@ -189,7 +189,7 @@ namespace PluralKit.Bot
await ctx.Rest.CreateMessage(
ctx.Channel.Id,
new MessageRequest { Content = $"{Emojis.Warn} Message contains codeblocks, raw source sent as an attachment." },
new[] { new MultipartFile("message.txt", stream) });
new[] { new MultipartFile("message.txt", stream, null) });
}
return;

View File

@ -129,6 +129,12 @@ namespace PluralKit.Bot
_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]);
webhookReq.Attachments = files.Select(f => new Message.Attachment()
{
Id = (ulong)Array.IndexOf(files, f),
Description = f.Description,
Filename = f.Filename
}).ToArray();
}
Message webhookMessage;
@ -174,7 +180,17 @@ 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,
Attachments = files.Select(f => new Message.Attachment()
{
Id = (ulong)Array.IndexOf(files, f),
Description = f.Description,
Filename = f.Filename,
}).ToArray()
};
await _rest.ExecuteWebhook(webhook.Id, webhook.Token!, req, files, threadId);
}
}
@ -184,7 +200,7 @@ namespace PluralKit.Bot
async Task<MultipartFile> GetStream(Message.Attachment attachment)
{
var attachmentResponse = await _client.GetAsync(attachment.Url, HttpCompletionOption.ResponseHeadersRead);
return new(attachment.Filename, await attachmentResponse.Content.ReadAsStreamAsync());
return new(attachment.Filename, await attachmentResponse.Content.ReadAsStreamAsync(), attachment.Description);
}
return await Task.WhenAll(attachments.Select(GetStream));