Skip to content

Commit

Permalink
Implement proper message reposting with embeds + support attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkavrba committed Jan 28, 2022
1 parent aa7869b commit bd2c608
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions MrClean/Services/MessageFilteringService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using Discord;
using Discord.WebSocket;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -46,19 +47,41 @@ message.Channel is not SocketGuildChannel channel ||
{
return;
}

await Task.Delay(matching.Delay * 1000);

if (matching.RepostChannelId is not null)
{
var repostChannel = channel.Guild.GetTextChannel(matching.RepostChannelId.Value);
var embed = new EmbedBuilder()
.WithColor(0x5865F2)
.WithAuthor(message.Author)
.WithDescription(message.Content)
.WithTimestamp(message.EditedTimestamp ?? message.CreatedAt)
.WithFooter("Reposted from #" + channel.Name)
.Build();

// Repost the message embed and all other attached embeds
var embeds = new List<Embed> {embed};

embeds.AddRange(message.Embeds);

await repostChannel.SendMessageAsync(
text: message.Content,
embeds: message.Embeds.ToArray(),
stickers: message.Stickers.ToArray(),
messageReference: message.Reference
var repostedMessage = await repostChannel.SendMessageAsync(embeds: embeds.ToArray());

// Repost all message attachments (files, images, videos...)
var httpClient = new HttpClient();
var attachments = await Task.WhenAll(
message.Attachments.Select(async a =>
new FileAttachment(
await httpClient.GetStreamAsync(a.Url),
a.Filename,
a.Filename,
a.IsSpoiler()
)
)
);

await repostChannel.SendFilesAsync(attachments, "", messageReference: repostedMessage.Reference);
}

await message.DeleteAsync();
Expand Down

0 comments on commit bd2c608

Please sign in to comment.