More localization, DiscordModule renamed to NadekoModule

This commit is contained in:
Kwoth 2017-02-13 10:29:57 +01:00
parent 34c6924320
commit 3f420f78f2
16 changed files with 147 additions and 25 deletions

View File

@ -20,7 +20,7 @@ using NLog;
namespace NadekoBot.Modules.Administration
{
[NadekoModule("Administration", ".")]
public partial class Administration : DiscordModule
public partial class Administration : NadekoModule
{
private static ConcurrentDictionary<ulong, string> GuildMuteRoles { get; } = new ConcurrentDictionary<ulong, string>();

View File

@ -0,0 +1,67 @@
using Discord;
using Discord.Commands;
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Administration.Commands
{
public partial class Administration
{
[Group]
public class LocalizationCommands : ModuleBase
{
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
[RequireUserPermission(GuildPermission.Administrator)]
public async Task SetLocale([Remainder] string name)
{
CultureInfo ci = null;
try
{
if(name.Trim().ToLowerInvariant() == "default")
{
NadekoBot.Localization.RemoveGuildCulture(Context.Guild);
}
ci = new CultureInfo(name);
NadekoBot.Localization.SetGuildCulture(Context.Guild, ci);
await Context.Channel.SendConfirmAsync($"Your guild's locale is now {ci}.").ConfigureAwait(false);
}
catch(Exception) {
//_log.warn(ex);
await Context.Channel.SendConfirmAsync($"Failed setting locale. Revisit this command's help.").ConfigureAwait(false);
}
}
[NadekoCommand, Usage, Description, Aliases]
[OwnerOnly]
public async Task SetDefaulLocale(string name)
{
CultureInfo ci = null;
try
{
if (name.Trim().ToLowerInvariant() == "default")
{
NadekoBot.Localization.RemoveGuildCulture(Context.Guild);
}
ci = new CultureInfo(name);
NadekoBot.Localization.SetGuildCulture(Context.Guild, ci);
await Context.Channel.SendConfirmAsync($"Your guild's locale is now {ci}.").ConfigureAwait(false);
}
catch (Exception)
{
//_log.warn(ex);
await Context.Channel.SendConfirmAsync($"Failed setting locale. Revisit this command's help.").ConfigureAwait(false);
}
}
}
}
}

View File

@ -17,7 +17,7 @@ using NLog;
namespace NadekoBot.Modules.ClashOfClans
{
[NadekoModule("ClashOfClans", ",")]
public class ClashOfClans : DiscordModule
public class ClashOfClans : NadekoModule
{
public static ConcurrentDictionary<ulong, List<ClashWar>> ClashWars { get; set; } = new ConcurrentDictionary<ulong, List<ClashWar>>();

View File

@ -17,7 +17,7 @@ using NadekoBot.DataStructures;
namespace NadekoBot.Modules.CustomReactions
{
[NadekoModule("CustomReactions", ".")]
public class CustomReactions : DiscordModule
public class CustomReactions : NadekoModule
{
private static CustomReaction[] _globalReactions = new CustomReaction[] { };
public static CustomReaction[] GlobalReactions => _globalReactions;

View File

@ -1,22 +1,69 @@
using Discord.Commands;
using Discord;
using Discord.Commands;
using NadekoBot.Extensions;
using NLog;
using System;
using System.Collections.Concurrent;
using System.Globalization;
using System.Threading.Tasks;
namespace NadekoBot.Modules
{
public abstract class DiscordModule : ModuleBase
public abstract class NadekoModule : ModuleBase
{
protected Logger _log { get; }
protected string _prefix { get; }
protected readonly Logger _log;
public readonly string _prefix;
public readonly CultureInfo cultureInfo;
public DiscordModule()
public NadekoModule(bool isTopLevelModule = true)
{
string prefix;
if (NadekoBot.ModulePrefixes.TryGetValue(this.GetType().Name, out prefix))
_prefix = prefix;
else
_prefix = "?missing_prefix?";
//if it's top level module
var typeName = isTopLevelModule ? this.GetType().Name : this.GetType().DeclaringType.Name;
if (!NadekoBot.ModulePrefixes.TryGetValue(typeName, out _prefix))
_prefix = "?err?";
_log = LogManager.GetCurrentClassLogger();
cultureInfo = (Context.Guild == null
? CultureInfo.CurrentCulture
: NadekoBot.Localization.GetCultureInfo(Context.Guild));
}
}
public abstract class NadekoSubmodule : NadekoModule
{
public NadekoSubmodule() : base(false)
{
}
}
public static class ModuleBaseExtensions
{
public static Task<IUserMessage> ConfirmLocalized(this NadekoModule module, string titleKey, string textKey, string url = null, string footer = null)
{
var title = NadekoBot.ResponsesResourceManager.GetString(titleKey, module.cultureInfo);
var text = NadekoBot.ResponsesResourceManager.GetString(textKey, module.cultureInfo);
return module.Context.Channel.SendConfirmAsync(title, text, url, footer);
}
public static Task<IUserMessage> ConfirmLocalized(this NadekoModule module, string textKey)
{
var text = NadekoBot.ResponsesResourceManager.GetString(textKey, module.cultureInfo);
return module.Context.Channel.SendConfirmAsync(textKey);
}
public static Task<IUserMessage> ErrorLocalized(this NadekoModule module, string titleKey, string textKey, string url = null, string footer = null)
{
var title = NadekoBot.ResponsesResourceManager.GetString(titleKey, module.cultureInfo);
var text = NadekoBot.ResponsesResourceManager.GetString(textKey, module.cultureInfo);
return module.Context.Channel.SendErrorAsync(title, text, url, footer);
}
public static Task<IUserMessage> ErrorLocalized(this NadekoModule module, string textKey)
{
var text = NadekoBot.ResponsesResourceManager.GetString(textKey, module.cultureInfo);
return module.Context.Channel.SendErrorAsync(textKey);
}
}
}

View File

@ -12,7 +12,7 @@ using System.Collections.Generic;
namespace NadekoBot.Modules.Gambling
{
[NadekoModule("Gambling", "$")]
public partial class Gambling : DiscordModule
public partial class Gambling : NadekoModule
{
public static string CurrencyName { get; set; }
public static string CurrencyPluralName { get; set; }

View File

@ -11,7 +11,7 @@ using NadekoBot.Extensions;
namespace NadekoBot.Modules.Games
{
[NadekoModule("Games", ">")]
public partial class Games : DiscordModule
public partial class Games : NadekoModule
{
private static string[] _8BallResponses { get; } = NadekoBot.BotConfig.EightBallResponses.Select(ebr => ebr.Text).ToArray();

View File

@ -13,7 +13,7 @@ using System.Collections.Generic;
namespace NadekoBot.Modules.Help
{
[NadekoModule("Help", "-")]
public partial class Help : DiscordModule
public partial class Help : NadekoModule
{
private static string helpString { get; } = NadekoBot.BotConfig.HelpString;
public static string HelpString => String.Format(helpString, NadekoBot.Credentials.ClientId, NadekoBot.ModulePrefixes[typeof(Help).Name]);

View File

@ -21,7 +21,7 @@ namespace NadekoBot.Modules.Music
{
[NadekoModule("Music", "!!")]
[DontAutoLoad]
public partial class Music : DiscordModule
public partial class Music : NadekoModule
{
public static ConcurrentDictionary<ulong, MusicPlayer> MusicPlayers { get; } = new ConcurrentDictionary<ulong, MusicPlayer>();

View File

@ -17,7 +17,7 @@ using System.Collections.Concurrent;
namespace NadekoBot.Modules.NSFW
{
[NadekoModule("NSFW", "~")]
public class NSFW : DiscordModule
public class NSFW : NadekoModule
{
private static ConcurrentDictionary<ulong, Timer> AutoHentaiTimers { get; } = new ConcurrentDictionary<ulong, Timer>();

View File

@ -15,7 +15,7 @@ using NLog;
namespace NadekoBot.Modules.Permissions
{
[NadekoModule("Permissions", ";")]
public partial class Permissions : DiscordModule
public partial class Permissions : NadekoModule
{
public class PermissionCache
{

View File

@ -16,7 +16,7 @@ using System.Collections.Concurrent;
namespace NadekoBot.Modules.Pokemon
{
[NadekoModule("Pokemon", ">")]
public partial class Pokemon : DiscordModule
public partial class Pokemon : NadekoModule
{
private static List<PokemonType> PokemonTypes = new List<PokemonType>();
private static ConcurrentDictionary<ulong, PokeStats> Stats = new ConcurrentDictionary<ulong, PokeStats>();
@ -105,7 +105,7 @@ namespace NadekoBot.Modules.Pokemon
if (targetUser == null)
{
await Context.Channel.SendMessageAsync("No such person.").ConfigureAwait(false);
await ReplyLocalized("no user found").ConfigureAwait(false);
return;
}
else if (targetUser == user)

View File

@ -28,7 +28,7 @@ using System.Xml.Linq;
namespace NadekoBot.Modules.Searches
{
[NadekoModule("Searches", "~")]
public partial class Searches : DiscordModule
public partial class Searches : NadekoModule
{
[NadekoCommand, Usage, Description, Aliases]
public async Task Weather([Remainder] string query)

View File

@ -21,7 +21,7 @@ using NadekoBot.Services;
namespace NadekoBot.Modules.Utility
{
[NadekoModule("Utility", ".")]
public partial class Utility : DiscordModule
public partial class Utility : NadekoModule
{
private static ConcurrentDictionary<ulong, Timer> rotatingRoleColors = new ConcurrentDictionary<ulong, Timer>();

View File

@ -16,6 +16,8 @@ using NadekoBot.TypeReaders;
using System.Collections.Concurrent;
using NadekoBot.Modules.Music;
using NadekoBot.Services.Database.Models;
using System.Resources;
using NadekoBot.Resources;
namespace NadekoBot
{
@ -29,7 +31,10 @@ namespace NadekoBot
public static CommandService CommandService { get; private set; }
public static CommandHandler CommandHandler { get; private set; }
public static DiscordShardedClient Client { get; private set; }
public static BotCredentials Credentials { get; private set; }
public static BotCredentials Credentials { get; }
public static Localization Localization { get; private set; }
public static ResourceManager ResponsesResourceManager { get; } = new ResourceManager(typeof(ResponseStrings));
public static GoogleApiService Google { get; private set; }
public static StatsService Stats { get; private set; }
@ -79,6 +84,7 @@ namespace NadekoBot
#endif
//initialize Services
Localization = new Localization(NadekoBot.AllGuildConfigs.ToDictionary(x => x.GuildId, x => x.Locale));
CommandService = new CommandService(new CommandServiceConfig() {
CaseSensitiveCommands = false,
DefaultRunMode = RunMode.Sync

View File

@ -64,6 +64,8 @@ namespace NadekoBot.Services.Database.Models
public AntiRaidSetting AntiRaidSetting { get; set; }
public AntiSpamSetting AntiSpamSetting { get; set; }
public string Locale { get; set; }
//public List<ProtectionIgnoredChannel> ProtectionIgnoredChannels { get; set; } = new List<ProtectionIgnoredChannel>();
}