예제 #1
0
        [Id(790933585812652042)] // global
        public async Task Translate(string message, string language = null)
        {
            string fromLanguage = null;

            if (language != null)
            {
                fromLanguage = LanguageCodesUtils.ToCode(language);
                if (fromLanguage == null)
                {
                    await ReplyAsync($":x: Language not recognised.", flags : InteractionResponseFlags.Ephemeral);

                    return;
                }
            }
            var client   = TranslationClient.Create();
            var response = await client.TranslateTextAsync(message, LanguageCodes.English, fromLanguage);

            var actualFrom = response.DetectedSourceLanguage == null ? fromLanguage : response.DetectedSourceLanguage;
            var name       = LanguageCodesUtils.ToName(actualFrom);

            await ReplyAsync($"Translated from {name}:\r\n>>> {response.TranslatedText}", flags : InteractionResponseFlags.Ephemeral);
        }
예제 #2
0
        public async Task TranslateCmd([Required] string message, string language = null)
        {
            string fromLanguage = null;

            if (language != null)
            {
                fromLanguage = LanguageCodesUtils.ToCode(language);
                if (fromLanguage == null)
                {
                    await Interaction.RespondAsync(":x: Language not recognised", ephemeral : true);

                    return;
                }
            }
            await Interaction.AcknowledgeAsync(Discord.InteractionResponseFlags.Ephemeral);

            var client   = TranslationClient.Create();
            var response = await client.TranslateTextAsync(message, LanguageCodes.English, fromLanguage);

            var actualFrom = response.DetectedSourceLanguage == null ? fromLanguage : response.DetectedSourceLanguage;
            var name       = LanguageCodesUtils.ToName(actualFrom);
            await Interaction.FollowupAsync($"Translate from {name}\r\n>>> {response.TranslatedText}");
        }
예제 #3
0
        public async Task Translate(IUserMessage arg, IUser manualRequester, string language)
        {
            if (!(Watched.TryGetValue(arg.Author.Id, out var users)))
            {
                if (manualRequester == null)
                {
                    return; // manual request doesn't need to send to all users, so we can continue
                }
            }
            if (users != null && users.Count == 0)
            {
                Watched.Remove(arg.Author.Id);
                if (manualRequester == null)
                {
                    return;
                }
            }
            var client   = TranslationClient.Create();
            var response = await client.TranslateTextAsync(arg.Content, language);

            if (response.DetectedSourceLanguage == language)
            {
                if (manualRequester != null)
                {
                    await manualRequester.SendMessageAsync($"Message has same detected language as wanting to translate into.");
                }
            }
            else
            {
                var embedB = new EmbedBuilder();
                embedB.Title = $"Translation from {LanguageCodesUtils.ToName(response.DetectedSourceLanguage)}";
                embedB.Url   = arg.GetJumpUrl();
                embedB.WithFooter(arg.Author.Username, arg.Author.GetAnyAvatarUrl());
                embedB.Description = response.TranslatedText;
                if (response.OriginalText.Length > 1024)
                {
                    embedB.AddField($"Original, part 1", Program.Clamp(response.OriginalText, 1024));
                    // 0 -> 1023
                    // 1024 ->
                    embedB.AddField($"Original, part 2", response.OriginalText.Substring(1024));
                }
                else
                {
                    embedB.AddField("Original", Program.Clamp(response.OriginalText, 1024));
                }
                var embed = embedB.Build();
                if (manualRequester != null)
                {
                    await manualRequester.SendMessageAsync(embed : embed);

                    return;
                }
                var rm = new List <ulong>();
                foreach (var id in users)
                {
                    var usr = Program.Client.GetUser(id);
                    if (usr == null)
                    {
                        rm.Add(id);
                        continue;
                    }
                    await usr.SendMessageAsync(embed : embed);
                }
                foreach (var x in rm)
                {
                    users.Remove(x);
                }
                if (rm.Count > 0)
                {
                    Watched[arg.Author.Id] = users;
                    this.OnSave();
                }
            }
        }