using Discord; using Discord.Commands; using NadekoBot.Extensions; using NadekoBot.Core.Services; using NLog; using System.Globalization; using System.Threading.Tasks; using Discord.WebSocket; using NadekoBot.Core.Services.Impl; namespace NadekoBot.Modules { public abstract class NadekoTopLevelModule : ModuleBase { protected readonly Logger _log; protected CultureInfo _cultureInfo; public readonly string ModuleTypeName; public readonly string LowerModuleTypeName; public NadekoStrings _strings { get; set; } public CommandHandler _cmdHandler { get; set; } public ILocalization _localization { get; set; } public string Prefix => _cmdHandler.GetPrefix(Context.Guild); protected NadekoTopLevelModule(bool isTopLevelModule = true) { //if it's top level module ModuleTypeName = isTopLevelModule ? this.GetType().Name : this.GetType().DeclaringType.Name; LowerModuleTypeName = ModuleTypeName.ToLowerInvariant(); _log = LogManager.GetCurrentClassLogger(); } protected override void BeforeExecute(CommandInfo cmd) { _cultureInfo = _localization.GetCultureInfo(Context.Guild?.Id); } //public Task 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 ReplyConfirmLocalized(string textKey) //{ // var text = NadekoBot.ResponsesResourceManager.GetString(textKey, cultureInfo); // return Context.Channel.SendConfirmAsync(Context.User.Mention + " " + textKey); //} //public Task 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); //} protected string GetText(string key) => _strings.GetText(key, _cultureInfo, LowerModuleTypeName); protected string GetText(string key, params object[] replacements) => _strings.GetText(key, _cultureInfo, LowerModuleTypeName, replacements); public Task ErrorLocalized(string textKey, params object[] replacements) { var text = GetText(textKey, replacements); return Context.Channel.SendErrorAsync(text); } public Task ReplyErrorLocalized(string textKey, params object[] replacements) { var text = GetText(textKey, replacements); return Context.Channel.SendErrorAsync(Context.User.Mention + " " + text); } public Task ConfirmLocalized(string textKey, params object[] replacements) { var text = GetText(textKey, replacements); return Context.Channel.SendConfirmAsync(text); } public Task ReplyConfirmLocalized(string textKey, params object[] replacements) { var text = GetText(textKey, replacements); return Context.Channel.SendConfirmAsync(Context.User.Mention + " " + text); } // TypeConverter typeConverter = TypeDescriptor.GetConverter(propType); ? public async Task GetUserInputAsync(ulong userId, ulong channelId) { var userInputTask = new TaskCompletionSource(); var dsc = (DiscordSocketClient)Context.Client; 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) { var _ = Task.Run(() => { if (!(arg is SocketUserMessage userMsg) || !(userMsg.Channel is ITextChannel chan) || userMsg.Author.Id != userId || userMsg.Channel.Id != channelId) { return Task.CompletedTask; } if (userInputTask.TrySetResult(arg.Content)) { userMsg.DeleteAfter(1); } return Task.CompletedTask; }); return Task.CompletedTask; } } } public abstract class NadekoTopLevelModule : NadekoTopLevelModule where TService : INService { public TService _service { get; set; } public NadekoTopLevelModule(bool isTopLevel = true) : base(isTopLevel) { } } public abstract class NadekoSubmodule : NadekoTopLevelModule { protected NadekoSubmodule() : base(false) { } } public abstract class NadekoSubmodule : NadekoTopLevelModule where TService : INService { protected NadekoSubmodule() : base(false) { } } }