Skip to content

Commit

Permalink
Add support for DeepL translations via the context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Naamloos committed Nov 28, 2023
1 parent 6251eea commit 02b921d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
Binary file added ModCore/Assets/globe-showing-europe.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions ModCore/ContextMenu/MessageContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
using System.Collections.Generic;
using DSharpPlus.Interactivity;
using System.Linq;
using DeepL;
using System;
using System.Reflection;
using ModCore.Entities;

namespace ModCore.ContextMenu
{
Expand All @@ -19,6 +23,8 @@ public class MessageContextMenu : ApplicationCommandModule

public InteractivityExtension interactivity { private get; set; }

public Settings settings { private get; set; }

const string EMOJI_REGEX = @"<a?:(.+?):(\d+)>";

[ContextMenu(ApplicationCommandType.MessageContextMenu, "Copy emoji")]
Expand Down Expand Up @@ -175,5 +181,50 @@ private async Task<DiscordMessageSticker> stealieSticker(DiscordGuild guild, Dis
memory.Position = 0;
return await guild.CreateStickerAsync(sticker.Name ?? "Sticker", sticker.Description ?? "", "🤡", memory, sticker.FormatType);
}

[ContextMenu(ApplicationCommandType.MessageContextMenu, "Translate (DeepL)")]
[SlashCommandPermissions(Permissions.ReadMessageHistory)]
[GuildOnly]
public async Task TranslateAsync(ContextMenuContext ctx)
{
if(string.IsNullOrEmpty(settings.DeepLToken))
{
await ctx.CreateResponseAsync("No DeepL token configured! Notify the bot developers via /contact!", true);
return;
}

var translate = ctx.TargetMessage.Content;
var translator = new Translator(settings.DeepLToken);

if(string.IsNullOrEmpty(translate))
{
await ctx.CreateResponseAsync("That message has no content!", true);
return;
}

await ctx.DeferAsync(false);

try
{
var assembly = Assembly.GetExecutingAssembly();
var resource = "ModCore.Assets.globe-showing-europe.gif";
var iconAsset = assembly.GetManifestResourceStream(resource);

var translation = await translator.TranslateTextAsync(translate, null, LanguageCode.EnglishAmerican);
await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder()
.AddEmbed(new DiscordEmbedBuilder()
.WithDescription($"Translated from language: {translation.DetectedSourceLanguageCode} to {LanguageCode.EnglishAmerican}.")
.AddField("Original Text", translate)
.AddField("Translated Text", translation.Text)
.WithColor(new DiscordColor("09a0e2"))
.WithThumbnail("attachment://globe-showing-europe.gif"))
.AddComponents(new DiscordLinkButtonComponent(ctx.TargetMessage.JumpLink.ToString(), "Jump to Original", emoji: new DiscordComponentEmoji("💭")))
.AddFile("globe-showing-europe.gif", iconAsset));
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
3 changes: 3 additions & 0 deletions ModCore/Entities/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class Settings

[JsonProperty("sus_guild_channel_id")]
public ulong SusGuildChannelId { get; private set; } = 1040374549293838376;

[JsonProperty("deepl-token")]
public string DeepLToken { get; private set; } = "";
}

public struct DatabaseSettings
Expand Down
7 changes: 7 additions & 0 deletions ModCore/ModCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<None Remove="Assets\globe-showing-europe.gif" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Assets\globe-showing-europe.gif" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Chronic-netstandard" Version="0.3.2.4" />
<PackageReference Include="DeepL.net" Version="1.8.0" />
<PackageReference Include="DSharpPlus" Version="4.4.3" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.4.3" />
<PackageReference Include="DSharpPlus.Interactivity" Version="4.4.3" />
Expand Down

0 comments on commit 02b921d

Please sign in to comment.