Skip to content

Commit

Permalink
small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Naamloos committed Dec 11, 2023
1 parent eac388b commit f7e350f
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions ModCore/ContextMenu/MessageContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ private async Task<DiscordMessageSticker> stealieSticker(DiscordGuild guild, Dis
return await guild.CreateStickerAsync(sticker.Name ?? "Sticker", sticker.Description ?? "", "🤡", memory, sticker.FormatType);
}

private static readonly string[] validFileExts = { ".jpg", ".gif", ".png", ".jpeg", ".webp" };

[ContextMenu(ApplicationCommandType.MessageContextMenu, "Translate (DeepL)")]
[SlashCommandPermissions(Permissions.ReadMessageHistory)]
[GuildOnly]
Expand All @@ -201,7 +203,11 @@ public async Task TranslateAsync(ContextMenuContext ctx)

var translateImage = "";

if (ctx.TargetMessage.Attachments.Count() > 0)
if (ctx.TargetMessage.Attachments.Where(x =>
{
var uri = new Uri(x.Url);
return validFileExts.Contains(Path.GetExtension(uri.AbsolutePath));
}).Count() > 0)
{
try
{
Expand Down Expand Up @@ -234,14 +240,14 @@ public async Task TranslateAsync(ContextMenuContext ctx)
var iconAsset = assembly.GetManifestResourceStream(resource);

TextResult translation = string.IsNullOrEmpty(translate) ? null : await translator.TranslateTextAsync(translate, null, LanguageCode.EnglishAmerican);
TextResult imageTranslation = string.IsNullOrEmpty(translateImage)? null : await translator.TranslateTextAsync(translateImage, null, LanguageCode.EnglishAmerican);
TextResult imageTranslation = string.IsNullOrEmpty(translateImage) ? null : await translator.TranslateTextAsync(translateImage, null, LanguageCode.EnglishAmerican);

var embed = new DiscordEmbedBuilder()
.WithDescription($"Translated using [DeepL](https://www.deepl.com/translator).")
.WithColor(new DiscordColor("09a0e2"))
.WithAuthor(name: "Translation", iconUrl: "attachment://globe-showing-europe.gif");

if(translation is not null)
if (translation is not null)
{
embed.AddField($"Original Message Text ({translation.DetectedSourceLanguageCode})", translate);
embed.AddField($"Translated Message Text ({LanguageCode.EnglishAmerican})", translation.Text);
Expand Down

0 comments on commit f7e350f

Please sign in to comment.