Added auto-translate language feature (~atl and ~at)
This commit is contained in:
parent
b47e68314c
commit
84a0fc4c8f
@ -6,47 +6,177 @@ using System;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NadekoBot.Services;
|
using NadekoBot.Services;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace NadekoBot.Modules.Searches
|
namespace NadekoBot.Modules.Searches
|
||||||
{
|
{
|
||||||
public partial class Searches
|
public partial class Searches
|
||||||
{
|
{
|
||||||
[NadekoCommand, Usage, Description, Aliases]
|
public struct UserChannelPair
|
||||||
[RequireContext(ContextType.Guild)]
|
|
||||||
public async Task Translate(IUserMessage umsg, string langs, [Remainder] string text = null)
|
|
||||||
{
|
{
|
||||||
var channel = (ITextChannel)umsg.Channel;
|
public ulong UserId { get; set; }
|
||||||
|
public ulong ChannelId { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
try
|
[Group]
|
||||||
|
public class TranslateCommands
|
||||||
|
{
|
||||||
|
private static ConcurrentDictionary<ulong, bool> TranslatedChannels { get; }
|
||||||
|
private static ConcurrentDictionary<UserChannelPair, string> UserLanguages { get; }
|
||||||
|
|
||||||
|
static TranslateCommands()
|
||||||
|
{
|
||||||
|
TranslatedChannels = new ConcurrentDictionary<ulong, bool>();
|
||||||
|
UserLanguages = new ConcurrentDictionary<UserChannelPair, string>();
|
||||||
|
|
||||||
|
NadekoBot.Client.MessageReceived += (msg) =>
|
||||||
|
{
|
||||||
|
var umsg = msg as IUserMessage;
|
||||||
|
if(umsg == null)
|
||||||
|
return Task.CompletedTask;
|
||||||
|
|
||||||
|
bool autoDelete;
|
||||||
|
if (!TranslatedChannels.TryGetValue(umsg.Channel.Id, out autoDelete))
|
||||||
|
return Task.CompletedTask;
|
||||||
|
|
||||||
|
var t = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
var key = new UserChannelPair()
|
||||||
|
{
|
||||||
|
UserId = umsg.Author.Id,
|
||||||
|
ChannelId = umsg.Channel.Id,
|
||||||
|
};
|
||||||
|
|
||||||
|
string langs;
|
||||||
|
if (!UserLanguages.TryGetValue(key, out langs))
|
||||||
|
return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var text = await TranslateInternal(umsg, langs, umsg.Resolve(UserMentionHandling.Ignore), true)
|
||||||
|
.ConfigureAwait(false);
|
||||||
|
if (autoDelete)
|
||||||
|
try { await umsg.DeleteAsync().ConfigureAwait(false); } catch { }
|
||||||
|
await umsg.Channel.SendMessageAsync($"{umsg.Author.Mention} `said:` "+text.Replace("<@ ", "<@").Replace("<@! ", "<@!")).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
});
|
||||||
|
return Task.CompletedTask;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[NadekoCommand, Usage, Description, Aliases]
|
||||||
|
[RequireContext(ContextType.Guild)]
|
||||||
|
public async Task Translate(IUserMessage umsg, string langs, [Remainder] string text = null)
|
||||||
|
{
|
||||||
|
var channel = (ITextChannel)umsg.Channel;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await umsg.Channel.TriggerTypingAsync().ConfigureAwait(false);
|
||||||
|
var translation = await TranslateInternal(umsg, langs, text);
|
||||||
|
await channel.SendMessageAsync(translation).ConfigureAwait(false);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
await channel.SendMessageAsync("Bad input format, or something went wrong...").ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<string> TranslateInternal(IUserMessage umsg, string langs, [Remainder] string text = null, bool silent = false)
|
||||||
{
|
{
|
||||||
var langarr = langs.ToLowerInvariant().Split('>');
|
var langarr = langs.ToLowerInvariant().Split('>');
|
||||||
if (langarr.Length != 2)
|
if (langarr.Length != 2)
|
||||||
return;
|
throw new ArgumentException();
|
||||||
string from = langarr[0];
|
string from = langarr[0];
|
||||||
string to = langarr[1];
|
string to = langarr[1];
|
||||||
text = text?.Trim();
|
text = text?.Trim();
|
||||||
if (string.IsNullOrWhiteSpace(text))
|
if (string.IsNullOrWhiteSpace(text))
|
||||||
return;
|
throw new ArgumentException();
|
||||||
|
return await GoogleTranslator.Instance.Translate(text, from, to).ConfigureAwait(false);
|
||||||
await umsg.Channel.TriggerTypingAsync().ConfigureAwait(false);
|
|
||||||
string translation = await GoogleTranslator.Instance.Translate(text, from, to).ConfigureAwait(false);
|
|
||||||
await channel.SendMessageAsync(translation).ConfigureAwait(false);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
|
public enum AutoDeleteAutoTranslate
|
||||||
{
|
{
|
||||||
Console.WriteLine(ex);
|
Del,
|
||||||
await channel.SendMessageAsync("Bad input format, or something went wrong...").ConfigureAwait(false);
|
Nodel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[NadekoCommand, Usage, Description, Aliases]
|
||||||
|
[RequireContext(ContextType.Guild)]
|
||||||
|
[RequirePermission(GuildPermission.Administrator)]
|
||||||
|
[OwnerOnly]
|
||||||
|
public async Task AutoTranslate(IUserMessage msg, AutoDeleteAutoTranslate autoDelete = AutoDeleteAutoTranslate.Nodel)
|
||||||
|
{
|
||||||
|
var channel = (ITextChannel)msg.Channel;
|
||||||
|
|
||||||
|
if (autoDelete == AutoDeleteAutoTranslate.Del)
|
||||||
|
{
|
||||||
|
TranslatedChannels.AddOrUpdate(channel.Id, true, (key, val) => true);
|
||||||
|
try { await channel.SendMessageAsync("`Started automatic translation of messages on this channel. User messages will be auto-deleted.`").ConfigureAwait(false); } catch { }
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool throwaway;
|
||||||
|
if (TranslatedChannels.TryRemove(channel.Id, out throwaway))
|
||||||
|
{
|
||||||
|
try { await channel.SendMessageAsync("`Stopped automatic translation of messages on this channel.`").ConfigureAwait(false); } catch { }
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (TranslatedChannels.TryAdd(channel.Id, autoDelete == AutoDeleteAutoTranslate.Del))
|
||||||
|
{
|
||||||
|
try { await channel.SendMessageAsync("`Started automatic translation of messages on this channel.`").ConfigureAwait(false); } catch { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[NadekoCommand, Usage, Description, Aliases]
|
||||||
|
[RequireContext(ContextType.Guild)]
|
||||||
|
public async Task AutoTransLang(IUserMessage msg, [Remainder] string langs = null)
|
||||||
|
{
|
||||||
|
var channel = (ITextChannel)msg.Channel;
|
||||||
|
|
||||||
|
var ucp = new UserChannelPair
|
||||||
|
{
|
||||||
|
UserId = msg.Author.Id,
|
||||||
|
ChannelId = msg.Channel.Id,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(langs))
|
||||||
|
{
|
||||||
|
if (UserLanguages.TryRemove(ucp, out langs))
|
||||||
|
await channel.SendMessageAsync($"{msg.Author.Mention}'s auto-translate language has been removed.").ConfigureAwait(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var langarr = langs.ToLowerInvariant().Split('>');
|
||||||
|
if (langarr.Length != 2)
|
||||||
|
return;
|
||||||
|
var from = langarr[0];
|
||||||
|
var to = langarr[1];
|
||||||
|
|
||||||
|
if (!GoogleTranslator.Instance.Languages.Contains(from) || !GoogleTranslator.Instance.Languages.Contains(to))
|
||||||
|
{
|
||||||
|
try { await channel.SendMessageAsync("`Invalid source and/or target Language.`").ConfigureAwait(false); } catch { }
|
||||||
|
}
|
||||||
|
|
||||||
|
UserLanguages.AddOrUpdate(ucp, langs, (key, val) => langs);
|
||||||
|
|
||||||
|
await channel.SendMessageAsync(":ok:").ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
[NadekoCommand, Usage, Description, Aliases]
|
||||||
|
[RequireContext(ContextType.Guild)]
|
||||||
|
public async Task Translangs(IUserMessage umsg)
|
||||||
|
{
|
||||||
|
var channel = (ITextChannel)umsg.Channel;
|
||||||
|
|
||||||
|
await channel.SendTableAsync(GoogleTranslator.Instance.Languages, str => $"{str,-15}", columns: 3);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[NadekoCommand, Usage, Description, Aliases]
|
|
||||||
[RequireContext(ContextType.Guild)]
|
|
||||||
public async Task Translangs(IUserMessage umsg)
|
|
||||||
{
|
|
||||||
var channel = (ITextChannel)umsg.Channel;
|
|
||||||
|
|
||||||
await channel.SendTableAsync(GoogleTranslator.Instance.Languages, str => $"{str,-15}", columns: 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
56
src/NadekoBot/Resources/CommandStrings.Designer.cs
generated
56
src/NadekoBot/Resources/CommandStrings.Designer.cs
generated
@ -519,7 +519,61 @@ namespace NadekoBot.Resources {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Looks up a localized string similar to av avatar.
|
/// Looks up a localized string similar to autotranslang atl.
|
||||||
|
/// </summary>
|
||||||
|
public static string autotranslang_cmd {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("autotranslang_cmd", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to `{0}atl en>fr`.
|
||||||
|
/// </summary>
|
||||||
|
public static string autotranslang_desc {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("autotranslang_desc", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to Sets your source and target language to be used with `{0}at`. Specify no arguments to remove previously set value..
|
||||||
|
/// </summary>
|
||||||
|
public static string autotranslang_usage {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("autotranslang_usage", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to autotrans at.
|
||||||
|
/// </summary>
|
||||||
|
public static string autotranslate_cmd {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("autotranslate_cmd", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to Starts automatic translation of all messages by users who set their `{0}atl` in this channel. You can set "del" argument to automatically delete all translated user messages..
|
||||||
|
/// </summary>
|
||||||
|
public static string autotranslate_desc {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("autotranslate_desc", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to `{0}at` or `{0}at del`.
|
||||||
|
/// </summary>
|
||||||
|
public static string autotranslate_usage {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("autotranslate_usage", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to avatar av.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string avatar_cmd {
|
public static string avatar_cmd {
|
||||||
get {
|
get {
|
||||||
|
@ -2089,7 +2089,7 @@
|
|||||||
<value>`{0}videocall "@SomeGuy"`</value>
|
<value>`{0}videocall "@SomeGuy"`</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="avatar_cmd" xml:space="preserve">
|
<data name="avatar_cmd" xml:space="preserve">
|
||||||
<value>av avatar</value>
|
<value>avatar av</value>
|
||||||
</data>
|
</data>
|
||||||
<data name="avatar_desc" xml:space="preserve">
|
<data name="avatar_desc" xml:space="preserve">
|
||||||
<value>Shows a mentioned person's avatar.</value>
|
<value>Shows a mentioned person's avatar.</value>
|
||||||
@ -2547,4 +2547,22 @@
|
|||||||
<data name="publicpoll_usage" xml:space="preserve">
|
<data name="publicpoll_usage" xml:space="preserve">
|
||||||
<value>`{0}ppoll Question?;Answer1;Answ 2;A_3`</value>
|
<value>`{0}ppoll Question?;Answer1;Answ 2;A_3`</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="autotranslang_cmd" xml:space="preserve">
|
||||||
|
<value>autotranslang atl</value>
|
||||||
|
</data>
|
||||||
|
<data name="autotranslang_desc" xml:space="preserve">
|
||||||
|
<value>`{0}atl en>fr`</value>
|
||||||
|
</data>
|
||||||
|
<data name="autotranslang_usage" xml:space="preserve">
|
||||||
|
<value>Sets your source and target language to be used with `{0}at`. Specify no arguments to remove previously set value.</value>
|
||||||
|
</data>
|
||||||
|
<data name="autotranslate_cmd" xml:space="preserve">
|
||||||
|
<value>autotrans at</value>
|
||||||
|
</data>
|
||||||
|
<data name="autotranslate_desc" xml:space="preserve">
|
||||||
|
<value>Starts automatic translation of all messages by users who set their `{0}atl` in this channel. You can set "del" argument to automatically delete all translated user messages.</value>
|
||||||
|
</data>
|
||||||
|
<data name="autotranslate_usage" xml:space="preserve">
|
||||||
|
<value>`{0}at` or `{0}at del`</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
Loading…
Reference in New Issue
Block a user