feat: add support for attachment descriptions
This commit is contained in:
parent
36acb5bae6
commit
0ca356eec3
@ -154,8 +154,8 @@ namespace Myriad.Rest
|
|||||||
{
|
{
|
||||||
for (var i = 0; i < files.Length; i++)
|
for (var i = 0; i < files.Length; i++)
|
||||||
{
|
{
|
||||||
var (filename, stream) = files[i];
|
var (filename, stream, _) = files[i];
|
||||||
mfd.Add(new StreamContent(stream), $"file{i}", filename);
|
mfd.Add(new StreamContent(stream), $"files[{i}]", filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,5 +2,5 @@ using System.IO;
|
|||||||
|
|
||||||
namespace Myriad.Rest.Types
|
namespace Myriad.Rest.Types
|
||||||
{
|
{
|
||||||
public record MultipartFile(string Filename, Stream Data);
|
public record MultipartFile(string Filename, Stream Data, string? Description);
|
||||||
}
|
}
|
@ -8,6 +8,7 @@ namespace Myriad.Rest.Types.Requests
|
|||||||
public string? Username { get; init; }
|
public string? Username { get; init; }
|
||||||
public string? AvatarUrl { get; init; }
|
public string? AvatarUrl { get; init; }
|
||||||
public Embed[] Embeds { get; init; }
|
public Embed[] Embeds { get; init; }
|
||||||
|
public Message.Attachment[] Attachments { get; set; }
|
||||||
public AllowedMentions? AllowedMentions { get; init; }
|
public AllowedMentions? AllowedMentions { get; init; }
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -71,6 +71,7 @@ namespace Myriad.Types
|
|||||||
public record Attachment
|
public record Attachment
|
||||||
{
|
{
|
||||||
public ulong Id { get; init; }
|
public ulong Id { get; init; }
|
||||||
|
public string? Description { get; init; }
|
||||||
public string Filename { get; init; }
|
public string Filename { get; init; }
|
||||||
public int Size { get; init; }
|
public int Size { get; init; }
|
||||||
public string Url { get; init; }
|
public string Url { get; init; }
|
||||||
|
@ -113,7 +113,7 @@ namespace PluralKit.Bot
|
|||||||
|
|
||||||
var msg = await ctx.Rest.CreateMessage(dm.Id,
|
var msg = await ctx.Rest.CreateMessage(dm.Id,
|
||||||
new MessageRequest { Content = $"{Emojis.Success} Here you go!" },
|
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}>" });
|
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
|
// If the original message wasn't posted in DMs, send a public reminder
|
||||||
|
@ -189,7 +189,7 @@ namespace PluralKit.Bot
|
|||||||
await ctx.Rest.CreateMessage(
|
await ctx.Rest.CreateMessage(
|
||||||
ctx.Channel.Id,
|
ctx.Channel.Id,
|
||||||
new MessageRequest { Content = $"{Emojis.Warn} Message contains codeblocks, raw source sent as an attachment." },
|
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;
|
return;
|
||||||
|
@ -129,6 +129,12 @@ namespace PluralKit.Bot
|
|||||||
_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);
|
req.Attachments.Length, req.Attachments.Select(a => a.Size).Sum() / 1024 / 1024, attachmentChunks.Count);
|
||||||
files = await GetAttachmentFiles(attachmentChunks[0]);
|
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;
|
Message webhookMessage;
|
||||||
@ -174,7 +180,17 @@ namespace PluralKit.Bot
|
|||||||
for (var i = 1; i < attachmentChunks.Count; i++)
|
for (var i = 1; i < attachmentChunks.Count; i++)
|
||||||
{
|
{
|
||||||
var files = await GetAttachmentFiles(attachmentChunks[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);
|
await _rest.ExecuteWebhook(webhook.Id, webhook.Token!, req, files, threadId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -184,7 +200,7 @@ namespace PluralKit.Bot
|
|||||||
async Task<MultipartFile> GetStream(Message.Attachment attachment)
|
async Task<MultipartFile> GetStream(Message.Attachment attachment)
|
||||||
{
|
{
|
||||||
var attachmentResponse = await _client.GetAsync(attachment.Url, HttpCompletionOption.ResponseHeadersRead);
|
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));
|
return await Task.WhenAll(attachments.Select(GetStream));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user