Skip to content

Commit

Permalink
Potential starboard image fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Naamloos committed Oct 18, 2023
1 parent 36b7440 commit 461549b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
5 changes: 5 additions & 0 deletions ModCore/Extensions/AsyncListenerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public void RegisterListeners(Assembly assembly)
listener.Attribute.Register(this.client, listener.Method, this.services);
}
}

public override void Dispose()
{

}
}

class ListenerMethod
Expand Down
24 changes: 18 additions & 6 deletions ModCore/Listeners/StarboardListeners.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
using ModCore.Extensions.Enums;
using ModCore.Database.DatabaseEntities;
using ModCore.Entities;
using System.IO;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;

namespace ModCore.Listeners
{
Expand Down Expand Up @@ -500,6 +503,7 @@ private static async Task updateStarboardMessage(DatabaseContext database, Disco
}
}

private static readonly string[] validFileExts = { ".jpg", ".gif", ".png", ".jpeg", ".webp" };
private static DiscordMessageBuilder buildStarboardMessage(DiscordMessage sourceMessage, long count, DiscordEmoji emoji)
{
// TODO cleanup
Expand All @@ -510,11 +514,18 @@ private static DiscordMessageBuilder buildStarboardMessage(DiscordMessage source
.WithFooter($"ID: {sourceMessage.Id}")
.WithTimestamp(sourceMessage.Id);

// This is shit code kek
if (sourceMessage.Attachments.Any(x => x.Url.ToLower().EndsWith(".jpg") || x.Url.ToLower().EndsWith(".png")
|| x.Url.ToLower().EndsWith(".jpeg") || x.Url.ToLower().EndsWith(".gif")))
embed.WithImageUrl(sourceMessage.Attachments.First(x => x.Url.ToLower().EndsWith(".jpg") || x.Url.ToLower().EndsWith(".png")
|| x.Url.ToLower().EndsWith(".jpeg") || x.Url.ToLower().EndsWith(".gif")).Url);
var imageEmbeds = new List<DiscordEmbedBuilder>();

var imageFiles = sourceMessage.Attachments.Where(x =>
{
var uri = new Uri(x.Url);
return uri.IsFile && validFileExts.Contains(Path.GetExtension(uri.AbsolutePath));
});

foreach(var img in imageFiles)
{
imageEmbeds.Add(new DiscordEmbedBuilder().WithUrl(img.Url).WithImageUrl(img.Url));
}

var emotename = emoji.GetDiscordName().Replace(":", "");
emotename = emotename.EndsWith('s') ? emotename : count > 1 ? emotename + "s" : emotename;
Expand All @@ -527,8 +538,9 @@ private static DiscordMessageBuilder buildStarboardMessage(DiscordMessage source
$"{refContent} {(sourceMessage.ReferencedMessage.Attachments.Count() > 0 ? $"_<{sourceMessage.ReferencedMessage.Attachments.Count()} file(s)>_" : "")}";
}

imageEmbeds.Add(embed);
var messageBuilder = new DiscordMessageBuilder()
.AddEmbed(embed)
.AddEmbeds(imageEmbeds.Select(x => x.Build()))
.WithContent($"{emoji} {count} {emotename} in {sourceMessage.Channel.Mention}");

messageBuilder.AddComponents(new DiscordLinkButtonComponent(sourceMessage.JumpLink.ToString(), "Go to message"));
Expand Down
10 changes: 5 additions & 5 deletions ModCore/ModCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Chronic-netstandard" Version="0.3.2.4" />
<PackageReference Include="DSharpPlus" Version="4.4.0-nightly-01393" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.4.0-nightly-01393" />
<PackageReference Include="DSharpPlus.Interactivity" Version="4.4.0-nightly-01393" />
<PackageReference Include="DSharpPlus.Rest" Version="4.4.0-nightly-01393" />
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.4.0-nightly-01393" />
<PackageReference Include="DSharpPlus" Version="4.4.2" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.4.2" />
<PackageReference Include="DSharpPlus.Interactivity" Version="4.4.2" />
<PackageReference Include="DSharpPlus.Rest" Version="4.4.2" />
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.4.2" />
<PackageReference Include="F23.StringSimilarity" Version="5.1.0" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.3" />
Expand Down

0 comments on commit 461549b

Please sign in to comment.