2017-02-13 09:29:57 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using NadekoBot.Extensions;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
using NadekoBot.Services;
|
2016-08-18 21:00:54 +00:00
|
|
|
|
using NLog;
|
2017-02-13 09:29:57 +00:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-06-13 12:21:24 +00:00
|
|
|
|
using Discord.WebSocket;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Services.Impl;
|
2016-08-15 14:57:40 +00:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules
|
|
|
|
|
{
|
2017-02-20 22:46:34 +00:00
|
|
|
|
public abstract class NadekoTopLevelModule : ModuleBase
|
2016-08-15 14:57:40 +00:00
|
|
|
|
{
|
2017-02-13 09:29:57 +00:00
|
|
|
|
protected readonly Logger _log;
|
2017-02-14 18:06:02 +00:00
|
|
|
|
protected CultureInfo _cultureInfo;
|
2017-06-15 23:55:14 +00:00
|
|
|
|
|
2017-02-13 12:44:21 +00:00
|
|
|
|
public readonly string ModuleTypeName;
|
|
|
|
|
public readonly string LowerModuleTypeName;
|
2017-06-15 23:55:14 +00:00
|
|
|
|
|
2017-05-22 23:59:31 +00:00
|
|
|
|
public NadekoStrings _strings { get; set; }
|
2017-05-30 04:54:59 +00:00
|
|
|
|
public CommandHandler _cmdHandler { get; set; }
|
2017-05-22 23:59:31 +00:00
|
|
|
|
public ILocalization _localization { get; set; }
|
|
|
|
|
|
2017-05-30 04:54:59 +00:00
|
|
|
|
public string Prefix => _cmdHandler.GetPrefix(Context.Guild);
|
|
|
|
|
|
2017-02-20 22:46:34 +00:00
|
|
|
|
protected NadekoTopLevelModule(bool isTopLevelModule = true)
|
2016-08-15 14:57:40 +00:00
|
|
|
|
{
|
2017-02-13 09:29:57 +00:00
|
|
|
|
//if it's top level module
|
2017-02-13 12:44:21 +00:00
|
|
|
|
ModuleTypeName = isTopLevelModule ? this.GetType().Name : this.GetType().DeclaringType.Name;
|
|
|
|
|
LowerModuleTypeName = ModuleTypeName.ToLowerInvariant();
|
2016-08-18 21:00:54 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
2017-02-13 13:23:29 +00:00
|
|
|
|
}
|
2017-02-13 09:29:57 +00:00
|
|
|
|
|
2017-07-05 15:38:38 +00:00
|
|
|
|
protected override void BeforeExecute(CommandInfo cmd)
|
2017-02-13 13:23:29 +00:00
|
|
|
|
{
|
2017-06-15 23:55:14 +00:00
|
|
|
|
_cultureInfo = _localization.GetCultureInfo(Context.Guild?.Id);
|
2017-02-13 09:29:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 12:24:11 +00:00
|
|
|
|
//public Task<IUserMessage> ReplyConfirmLocalized(string titleKey, string textKey, string url = null, string footer = null)
|
|
|
|
|
//{
|
|
|
|
|
// var title = NadekoBot.ResponsesResourceManager.GetString(titleKey, cultureInfo);
|
|
|
|
|
// var text = NadekoBot.ResponsesResourceManager.GetString(textKey, cultureInfo);
|
|
|
|
|
// return Context.Channel.SendConfirmAsync(title, text, url, footer);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//public Task<IUserMessage> ReplyConfirmLocalized(string textKey)
|
|
|
|
|
//{
|
|
|
|
|
// var text = NadekoBot.ResponsesResourceManager.GetString(textKey, cultureInfo);
|
|
|
|
|
// return Context.Channel.SendConfirmAsync(Context.User.Mention + " " + textKey);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//public Task<IUserMessage> ReplyErrorLocalized(string titleKey, string textKey, string url = null, string footer = null)
|
|
|
|
|
//{
|
|
|
|
|
// var title = NadekoBot.ResponsesResourceManager.GetString(titleKey, cultureInfo);
|
|
|
|
|
// var text = NadekoBot.ResponsesResourceManager.GetString(textKey, cultureInfo);
|
|
|
|
|
// return Context.Channel.SendErrorAsync(title, text, url, footer);
|
|
|
|
|
//}
|
2017-06-15 23:55:14 +00:00
|
|
|
|
|
2017-02-13 20:29:06 +00:00
|
|
|
|
protected string GetText(string key) =>
|
2017-05-22 23:59:31 +00:00
|
|
|
|
_strings.GetText(key, _cultureInfo, LowerModuleTypeName);
|
2017-02-13 20:29:06 +00:00
|
|
|
|
|
|
|
|
|
protected string GetText(string key, params object[] replacements) =>
|
2017-05-22 23:59:31 +00:00
|
|
|
|
_strings.GetText(key, _cultureInfo, LowerModuleTypeName, replacements);
|
2017-02-13 20:29:06 +00:00
|
|
|
|
|
2017-02-13 12:24:11 +00:00
|
|
|
|
public Task<IUserMessage> ErrorLocalized(string textKey, params object[] replacements)
|
2017-02-13 09:29:57 +00:00
|
|
|
|
{
|
2017-02-15 10:03:40 +00:00
|
|
|
|
var text = GetText(textKey, replacements);
|
|
|
|
|
return Context.Channel.SendErrorAsync(text);
|
2017-02-13 09:29:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 12:24:11 +00:00
|
|
|
|
public Task<IUserMessage> ReplyErrorLocalized(string textKey, params object[] replacements)
|
2017-02-13 09:29:57 +00:00
|
|
|
|
{
|
2017-02-15 10:03:40 +00:00
|
|
|
|
var text = GetText(textKey, replacements);
|
|
|
|
|
return Context.Channel.SendErrorAsync(Context.User.Mention + " " + text);
|
2017-02-13 09:29:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 12:24:11 +00:00
|
|
|
|
public Task<IUserMessage> ConfirmLocalized(string textKey, params object[] replacements)
|
2017-02-13 09:29:57 +00:00
|
|
|
|
{
|
2017-02-15 10:03:40 +00:00
|
|
|
|
var text = GetText(textKey, replacements);
|
|
|
|
|
return Context.Channel.SendConfirmAsync(text);
|
2017-02-13 09:29:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-13 12:24:11 +00:00
|
|
|
|
public Task<IUserMessage> ReplyConfirmLocalized(string textKey, params object[] replacements)
|
2017-02-13 09:29:57 +00:00
|
|
|
|
{
|
2017-02-15 10:03:40 +00:00
|
|
|
|
var text = GetText(textKey, replacements);
|
|
|
|
|
return Context.Channel.SendConfirmAsync(Context.User.Mention + " " + text);
|
2017-02-13 09:29:57 +00:00
|
|
|
|
}
|
2017-06-19 13:42:10 +00:00
|
|
|
|
|
|
|
|
|
// TypeConverter typeConverter = TypeDescriptor.GetConverter(propType); ?
|
2017-06-13 12:21:24 +00:00
|
|
|
|
public async Task<string> GetUserInputAsync(ulong userId, ulong channelId)
|
|
|
|
|
{
|
|
|
|
|
var userInputTask = new TaskCompletionSource<string>();
|
2017-06-19 13:42:10 +00:00
|
|
|
|
var dsc = (DiscordSocketClient)Context.Client;
|
2017-06-13 12:21:24 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
dsc.MessageReceived += MessageReceived;
|
|
|
|
|
|
|
|
|
|
if ((await Task.WhenAny(userInputTask.Task, Task.Delay(10000))) != userInputTask.Task)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await userInputTask.Task;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
dsc.MessageReceived -= MessageReceived;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Task MessageReceived(SocketMessage arg)
|
|
|
|
|
{
|
2017-06-15 23:55:14 +00:00
|
|
|
|
var _ = Task.Run(() =>
|
2017-06-13 12:21:24 +00:00
|
|
|
|
{
|
2017-06-15 23:55:14 +00:00
|
|
|
|
if (!(arg is SocketUserMessage userMsg) ||
|
|
|
|
|
!(userMsg.Channel is ITextChannel chan) ||
|
|
|
|
|
userMsg.Author.Id != userId ||
|
|
|
|
|
userMsg.Channel.Id != channelId)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-16 22:17:07 +00:00
|
|
|
|
if (userInputTask.TrySetResult(arg.Content))
|
|
|
|
|
{
|
|
|
|
|
userMsg.DeleteAfter(1);
|
|
|
|
|
}
|
2017-06-13 12:21:24 +00:00
|
|
|
|
return Task.CompletedTask;
|
2017-06-15 23:55:14 +00:00
|
|
|
|
});
|
2017-06-13 12:21:24 +00:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-13 10:12:13 +00:00
|
|
|
|
}
|
2017-07-15 16:34:34 +00:00
|
|
|
|
|
|
|
|
|
public abstract class NadekoTopLevelModule<TService> : NadekoTopLevelModule where TService : INService
|
|
|
|
|
{
|
|
|
|
|
public TService _service { get; set; }
|
|
|
|
|
|
|
|
|
|
public NadekoTopLevelModule(bool isTopLevel = true) : base(isTopLevel)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-13 09:29:57 +00:00
|
|
|
|
|
2017-02-20 22:46:34 +00:00
|
|
|
|
public abstract class NadekoSubmodule : NadekoTopLevelModule
|
2017-07-15 16:34:34 +00:00
|
|
|
|
{
|
|
|
|
|
protected NadekoSubmodule() : base(false) { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract class NadekoSubmodule<TService> : NadekoTopLevelModule<TService> where TService : INService
|
2017-02-13 10:12:13 +00:00
|
|
|
|
{
|
2017-02-13 23:19:59 +00:00
|
|
|
|
protected NadekoSubmodule() : base(false)
|
2017-02-13 09:29:57 +00:00
|
|
|
|
{
|
2016-08-15 14:57:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-13 14:03:39 +00:00
|
|
|
|
}
|