NadekoBot/NadekoBot.Modules.Searches/TranslatorCommands.cs

137 lines
5.2 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
using System.Threading.Tasks;
using System.Linq;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
using NadekoBot.Core.Services;
2017-07-17 19:42:36 +00:00
using NadekoBot.Modules.Searches.Services;
namespace NadekoBot.Modules.Searches
{
public partial class Searches
{
[Group]
2017-02-24 16:10:44 +00:00
public class TranslateCommands : NadekoSubmodule
{
2017-05-27 08:19:27 +00:00
private readonly SearchesService _searches;
private readonly IGoogleApiService _google;
2017-05-27 08:19:27 +00:00
public TranslateCommands(SearchesService searches, IGoogleApiService google)
{
2017-05-27 08:19:27 +00:00
_searches = searches;
_google = google;
}
[NadekoCommand, Usage, Description, Aliases]
public async Task Translate(string langs, [Remainder] string text = null)
{
try
{
2016-12-16 18:43:57 +00:00
await Context.Channel.TriggerTypingAsync().ConfigureAwait(false);
2017-05-27 08:19:27 +00:00
var translation = await _searches.Translate(langs, text);
2017-02-24 16:10:44 +00:00
await Context.Channel.SendConfirmAsync(GetText("translation") + " " + langs, translation).ConfigureAwait(false);
}
catch
{
2017-02-24 16:10:44 +00:00
await ReplyErrorLocalized("bad_input_format").ConfigureAwait(false);
}
}
//[NadekoCommand, Usage, Description, Aliases]
//[OwnerOnly]
//public async Task Obfuscate([Remainder] string txt)
//{
// var lastItem = "en";
// foreach (var item in _google.Languages.Except(new[] { "en" }).Where(x => x.Length < 4))
// {
// var txt2 = await _searches.Translate(lastItem + ">" + item, txt);
// await Context.Channel.EmbedAsync(new EmbedBuilder()
// .WithOkColor()
// .WithTitle(lastItem + ">" + item)
// .AddField("Input", txt)
// .AddField("Output", txt2));
// txt = txt2;
// await Task.Delay(500);
// lastItem = item;
// }
// txt = await _searches.Translate(lastItem + ">en", txt);
// await Context.Channel.SendConfirmAsync("Final output:\n\n" + txt);
//}
public enum AutoDeleteAutoTranslate
{
Del,
Nodel
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.Administrator)]
[OwnerOnly]
2016-12-16 18:43:57 +00:00
public async Task AutoTranslate(AutoDeleteAutoTranslate autoDelete = AutoDeleteAutoTranslate.Nodel)
{
2016-12-16 18:43:57 +00:00
var channel = (ITextChannel)Context.Channel;
if (autoDelete == AutoDeleteAutoTranslate.Del)
{
2017-05-27 08:19:27 +00:00
_searches.TranslatedChannels.AddOrUpdate(channel.Id, true, (key, val) => true);
2017-02-24 16:10:44 +00:00
await ReplyConfirmLocalized("atl_ad_started").ConfigureAwait(false);
return;
}
2017-05-27 08:19:27 +00:00
if (_searches.TranslatedChannels.TryRemove(channel.Id, out _))
{
2017-02-24 16:10:44 +00:00
await ReplyConfirmLocalized("atl_stopped").ConfigureAwait(false);
return;
}
2017-05-27 08:19:27 +00:00
if (_searches.TranslatedChannels.TryAdd(channel.Id, autoDelete == AutoDeleteAutoTranslate.Del))
{
2017-02-24 16:10:44 +00:00
await ReplyConfirmLocalized("atl_started").ConfigureAwait(false);
}
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task AutoTransLang([Remainder] string langs = null)
{
var ucp = new UserChannelPair
{
2016-12-16 18:43:57 +00:00
UserId = Context.User.Id,
ChannelId = Context.Channel.Id,
};
if (string.IsNullOrWhiteSpace(langs))
{
2017-05-27 08:19:27 +00:00
if (_searches.UserLanguages.TryRemove(ucp, out langs))
2017-02-24 16:10:44 +00:00
await ReplyConfirmLocalized("atl_removed").ConfigureAwait(false);
return;
}
var langarr = langs.ToLowerInvariant().Split('>');
if (langarr.Length != 2)
return;
var from = langarr[0];
var to = langarr[1];
2017-05-27 08:19:27 +00:00
if (!_google.Languages.Contains(from) || !_google.Languages.Contains(to))
{
2017-02-24 16:10:44 +00:00
await ReplyErrorLocalized("invalid_lang").ConfigureAwait(false);
2016-11-08 18:28:18 +00:00
return;
}
2017-05-27 08:19:27 +00:00
_searches.UserLanguages.AddOrUpdate(ucp, langs, (key, val) => langs);
2017-02-24 16:10:44 +00:00
await ReplyConfirmLocalized("atl_set", from, to).ConfigureAwait(false);
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task Translangs()
{
2017-05-27 08:19:27 +00:00
await Context.Channel.SendTableAsync(_google.Languages, str => $"{str,-15}", 3);
}
}
}
}