public async Task QuickTranslatePairAsync(Emoji emote, LanguageMap.LanguageCode languageCode) { var group = Context.Server.Settings.Translate.CustomPairs.FirstOrDefault(x => x.Language == languageCode); if (group != null) { if (group.EmoteMatches.Any(x => x == emote.Name)) { throw new Exception("Emote already mapped to a language"); } group.EmoteMatches.Add(emote.Name); } else { Context.Server.Settings.Translate.CustomPairs.Add(new GuildModel.GuildSetup.TranslateSetup.TranslationSet { EmoteMatches = new List <string> { emote.Name }, Language = languageCode }); } Context.Server.Save(); await _Translate.UpdateSetupAsync(Context.Guild.Id, Context.Server.Settings.Translate); await SimpleEmbedAsync("Pair Added:\n" + $"{emote.Name} => {languageCode}"); }
/// <summary> /// Simple method for translating a message with an embed response /// </summary> /// <param name="language"> /// The language code /// </param> /// <param name="provider"> /// The provider. /// </param> /// <param name="message"> /// The message. /// </param> /// <param name="reaction"> /// The reaction. /// </param> /// <returns> /// The <see cref="Task" />. /// </returns> public static async Task <EmbedBuilder> TranslateEmbedAsync(LanguageMap.LanguageCode language, IServiceProvider provider, IUserMessage message, SocketReaction reaction = null) { var embed = new EmbedBuilder { Title = "Translate", Color = Color.Blue }; var original = message.Content.FixLength(); var languageString = LanguageCodeToString(language); var file = await TranslateMessageAsync(languageString, message.Content, provider); var response = HandleResponse(file).FixLength(); embed.AddField($"Translated [{language}{(reaction?.Emote == null ? string.Empty : $"{reaction.Emote}")}]", $"{response}");
public async Task <EmbedBuilder> TranslateEmbedAsync(LanguageMap.LanguageCode language, IUserMessage message, IGuildChannel channel, SocketReaction reaction = null) { var embed = new EmbedBuilder { Title = "Translate", Color = Color.Blue }; var original = message.Content.FixLength(); var file = await TranslateTextAsync(message.Content, channel, language); if (file.AuthenticationResponse == TranslateLimitsNew.ResponseStatus.GuildSucceded) { var response = file.Response.TranslatedText.FixLength(); embed.AddField($"Translated [{language}{(reaction?.Emote == null ? string.Empty : $"{reaction.Emote}")}]", $"{response}");
public async Task TranslateCmdAsync(LanguageMap.LanguageCode languageCode, [Remainder] string message) { var embed = new EmbedBuilder { Title = "Translate", Color = Color.Blue }; var original = message.FixLength(); var file = await Methods.TranslateTextAsync(message, Context.Channel as IGuildChannel, languageCode); if (file.AuthenticationResponse == TranslateLimitsNew.ResponseStatus.GuildSucceded) { embed.AddField($"Translated [{file.Response.TargetLanguage}]", $"{file.Response.TranslatedText.FixLength()}"); embed.AddField($"Original [{file.Response.DetectedSourceLanguage}]", $"{original.FixLength()}"); await ReplyAsync(string.Empty, false, embed.Build()); return; } await ReplyAndDeleteAsync(file.ResponseMessage, TimeSpan.FromSeconds(20)); }
/// <summary> /// Converts from a language code to a useable google translate code. /// </summary> /// <param name="code"> /// The code. /// </param> /// <returns> /// The <see cref="string" />. /// </returns> public static string LanguageCodeToString(LanguageMap.LanguageCode code) { var language = code.ToString(); if (language == "zh_CN") { language = "zh-CN"; } if (language == "zh_TW") { language = "zh-TW"; } if (language == "_is") { language = "is"; } return(language); }
public async Task <TranslationResponse> TranslateTextAsync(string message, IGuildChannel channel, LanguageMap.LanguageCode destination, LanguageMap.LanguageCode?source = null) { if (Client == null) { return(new TranslationResponse(null, TranslateLimitsNew.ResponseStatus.Error, $"Translation is not enabled with this instance of the bot.")); } if (message == null) { return(new TranslationResponse(null, TranslateLimitsNew.ResponseStatus.Error, "Message is null")); } if (channel?.Guild == null) { return(new TranslationResponse(null, TranslateLimitsNew.ResponseStatus.Error, "Translation can only be invoked in a discord server.")); } if (string.IsNullOrWhiteSpace(message)) { return(new TranslationResponse(new TranslationResult("", "", "", "", "", null), TranslateLimitsNew.ResponseStatus.GuildSucceded, "Message Empty")); } try { var authResponse = await Limits.UpdateAsync(channel.GuildId, message); if (authResponse == TranslateLimitsNew.ResponseStatus.GuildSucceded) { var response = await Client.TranslateTextAsync(message, LanguageCodeToString(destination), LanguageCodeToString(source)); return(new TranslationResponse(response, authResponse, "Success")); } if (authResponse == TranslateLimitsNew.ResponseStatus.GuildLimitExceeded) { return(new TranslationResponse(null, authResponse, $"Guild does not have any available translations left. {Model.GetTranslateUrl()}")); } if (authResponse == TranslateLimitsNew.ResponseStatus.GuildLimitExceededByMessage) { return(new TranslationResponse(null, authResponse, $"Message is too long and will exceed the remaining characters available for translation in the guild. {Model.GetTranslateUrl()}")); } return(new TranslationResponse(null, TranslateLimitsNew.ResponseStatus.Error, "There was an error retrieving guild data.")); } catch (Exception e) { return(new TranslationResponse(null, TranslateLimitsNew.ResponseStatus.Error, $"Something went wrong while translating...\n{e}")); } }