NadekoBot/NadekoBot.Core/Common/NadekoModule.cs

154 lines
5.6 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
using NadekoBot.Core.Services;
2016-08-18 21:00:54 +00:00
using NLog;
using System.Globalization;
using System.Threading.Tasks;
2017-06-13 12:21:24 +00:00
using Discord.WebSocket;
using NadekoBot.Core.Services.Impl;
2016-08-15 14:57:40 +00:00
namespace NadekoBot.Modules
{
public abstract class NadekoTopLevelModule : ModuleBase
2016-08-15 14:57:40 +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; }
public CommandHandler _cmdHandler { get; set; }
2017-05-22 23:59:31 +00:00
public ILocalization _localization { get; set; }
public string Prefix => _cmdHandler.GetPrefix(Context.Guild);
protected NadekoTopLevelModule(bool isTopLevelModule = true)
2016-08-15 14:57:40 +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-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);
}
//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
protected string GetText(string key) =>
2017-05-22 23:59:31 +00:00
_strings.GetText(key, _cultureInfo, LowerModuleTypeName);
protected string GetText(string key, params object[] replacements) =>
2017-05-22 23:59:31 +00:00
_strings.GetText(key, _cultureInfo, LowerModuleTypeName, replacements);
public Task<IUserMessage> ErrorLocalized(string textKey, params object[] replacements)
{
var text = GetText(textKey, replacements);
return Context.Channel.SendErrorAsync(text);
}
public Task<IUserMessage> ReplyErrorLocalized(string textKey, params object[] replacements)
{
var text = GetText(textKey, replacements);
return Context.Channel.SendErrorAsync(Context.User.Mention + " " + text);
}
public Task<IUserMessage> ConfirmLocalized(string textKey, params object[] replacements)
{
var text = GetText(textKey, replacements);
return Context.Channel.SendConfirmAsync(text);
}
public Task<IUserMessage> ReplyConfirmLocalized(string textKey, params object[] replacements)
{
var text = GetText(textKey, replacements);
return Context.Channel.SendConfirmAsync(Context.User.Mention + " " + text);
}
// 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>();
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)
{
}
}
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
{
protected NadekoSubmodule() : base(false)
{
2016-08-15 14:57:40 +00:00
}
}
}