Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit e6e7c92

Browse files
committed
Add attachments to embed fields
1 parent 4580669 commit e6e7c92

2 files changed

Lines changed: 53 additions & 13 deletions

File tree

TabletBot.Discord/Formatting.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Discord;
2+
13
namespace TabletBot.Discord
24
{
35
public static class Formatting
@@ -8,5 +10,7 @@ public static class Formatting
810
public const string CODE_AFFIX = "`";
911
public const string QUOTE_PREFIX = "> ";
1012
public const string CODE_BLOCK = "```";
13+
14+
public static string UrlString(IAttachment attachment) => $"[{attachment.Filename}]({attachment.Url})";
1115
}
1216
}

TabletBot.Discord/Watchers/DirectMessage/ModMailMessageWatcher.cs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
using System.Collections.Generic;
13
using System.Linq;
24
using System.Threading.Tasks;
35
using Discord;
@@ -18,28 +20,62 @@ public ModMailMessageWatcher(DiscordSocketClient client, Settings settings)
1820
_settings = settings;
1921
}
2022

21-
private IMessageChannel? _directMessageLogChannel;
23+
private ITextChannel? _directMessageLogChannel;
2224

2325
public async Task Receive(IMessage message)
2426
{
2527
if (message.Channel is IDMChannel dmChannel)
2628
{
27-
if (_directMessageLogChannel == null)
29+
await DirectMessage(message, dmChannel);
30+
}
31+
}
32+
33+
private async Task DirectMessage(IMessage message, IDMChannel dmChannel)
34+
{
35+
var embed = new EmbedBuilder
36+
{
37+
Description = message.Content,
38+
Timestamp = message.Timestamp,
39+
Author = dmChannel.Recipient.ToEmbedAuthor(),
40+
Color = Color.Blue,
41+
Fields = GetFieldForAttachments(message),
42+
Footer = new EmbedFooterBuilder
2843
{
29-
var channel = await _client.Rest.GetChannelAsync(_settings.ModMailChannelID);
30-
_directMessageLogChannel = (IMessageChannel) channel;
44+
Text = dmChannel.Id.ToString()
3145
}
46+
};
3247

33-
var embed = new EmbedBuilder
34-
{
35-
Description = message.Content,
36-
Timestamp = message.Timestamp,
37-
Author = dmChannel.Recipient.ToEmbedAuthor(),
38-
Color = Color.Blue
39-
};
48+
var existingEmbeds = message.Embeds.Any() ? message.Embeds : Array.Empty<IEmbed>();
4049

41-
await _directMessageLogChannel.SendMessageAsync(embed: embed.Build());
42-
}
50+
var embeds = from emb in existingEmbeds.Prepend(embed.Build())
51+
where emb is Embed
52+
select emb as Embed;
53+
54+
var channel = await GetModMailChannel();
55+
await channel.SendMessageAsync(embeds: embeds.ToArray());
56+
}
57+
58+
private async Task<ITextChannel> GetModMailChannel()
59+
{
60+
if (_directMessageLogChannel != null)
61+
return _directMessageLogChannel;
62+
63+
var channel = await _client.Rest.GetChannelAsync(_settings.ModMailChannelID);
64+
return _directMessageLogChannel = (ITextChannel) channel;
65+
}
66+
67+
private static List<EmbedFieldBuilder> GetFieldForAttachments(IMessage message)
68+
{
69+
return message.Attachments.Select(GetFieldForAttachment).ToList();
70+
}
71+
72+
private static EmbedFieldBuilder GetFieldForAttachment(IAttachment attachment)
73+
{
74+
return new EmbedFieldBuilder
75+
{
76+
Name = attachment.Filename,
77+
Value = Formatting.UrlString(attachment)
78+
};
4379
}
4480

4581
public Task Deleted(Cacheable<IMessage, ulong> message, Cacheable<IMessageChannel, ulong> channel) => Task.CompletedTask;

0 commit comments

Comments
 (0)