NadekoBot/src/NadekoBot/Modules/NadekoModule.cs

97 lines
3.7 KiB
C#
Raw Normal View History

using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
2016-08-18 21:00:54 +00:00
using NLog;
using System;
using System.Collections.Concurrent;
using System.Globalization;
using System.Threading.Tasks;
2016-08-15 14:57:40 +00:00
namespace NadekoBot.Modules
{
public abstract class NadekoModule : ModuleBase
2016-08-15 14:57:40 +00:00
{
protected readonly Logger _log;
public readonly string _prefix;
public readonly CultureInfo cultureInfo;
2017-02-13 12:44:21 +00:00
public readonly string ModuleTypeName;
public readonly string LowerModuleTypeName;
2016-08-15 14:57:40 +00:00
public NadekoModule(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();
if (!NadekoBot.ModulePrefixes.TryGetValue(ModuleTypeName, out _prefix))
_prefix = "?err?";
2016-08-18 21:00:54 +00:00
_log = LogManager.GetCurrentClassLogger();
cultureInfo = (Context.Guild == null
? CultureInfo.CurrentCulture
: NadekoBot.Localization.GetCultureInfo(Context.Guild));
}
//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-02-13 12:44:21 +00:00
protected string GetText(string key)
{
return NadekoBot.ResponsesResourceManager.GetString(LowerModuleTypeName + "_" + key, cultureInfo);
}
protected string GetText(string key, params object[] replacements)
{
return string.Format(GetText(key), replacements);
}
public Task<IUserMessage> ErrorLocalized(string textKey, params object[] replacements)
{
2017-02-13 12:44:21 +00:00
var text = GetText(textKey);
return Context.Channel.SendErrorAsync(string.Format(text, replacements));
}
public Task<IUserMessage> ReplyErrorLocalized(string textKey, params object[] replacements)
{
2017-02-13 12:44:21 +00:00
var text = GetText(textKey);
return Context.Channel.SendErrorAsync(Context.User.Mention + " " +string.Format(text, replacements));
}
public Task<IUserMessage> ConfirmLocalized(string textKey, params object[] replacements)
{
2017-02-13 12:44:21 +00:00
var text = GetText(textKey);
return Context.Channel.SendConfirmAsync(string.Format(text, replacements));
}
public Task<IUserMessage> ReplyConfirmLocalized(string textKey, params object[] replacements)
{
2017-02-13 12:44:21 +00:00
var text = GetText(textKey);
return Context.Channel.SendConfirmAsync(Context.User.Mention + " " + string.Format(text, replacements));
}
2017-02-13 10:12:13 +00:00
}
2017-02-13 10:12:13 +00:00
public abstract class NadekoSubmodule : NadekoModule
{
public NadekoSubmodule() : base(false)
{
2016-08-15 14:57:40 +00:00
}
}
}