Applied codestyle to all .cs files

This commit is contained in:
Kwoth
2021-12-29 06:07:16 +01:00
parent 723447c7d4
commit 82000c97a4
543 changed files with 13221 additions and 14059 deletions

View File

@@ -4,7 +4,7 @@ using System.Globalization;
namespace NadekoBot.Services;
/// <summary>
/// Defines methods to retrieve and reload bot strings
/// Defines methods to retrieve and reload bot strings
/// </summary>
public interface IBotStrings
{
@@ -13,4 +13,4 @@ public interface IBotStrings
void Reload();
CommandStrings GetCommandStrings(string commandName, ulong? guildId = null);
CommandStrings GetCommandStrings(string commandName, CultureInfo cultureInfo);
}
}

View File

@@ -2,27 +2,27 @@
namespace NadekoBot.Services;
/// <summary>
/// Implemented by classes which provide localized strings in their own ways
/// Implemented by classes which provide localized strings in their own ways
/// </summary>
public interface IBotStringsProvider
{
/// <summary>
/// Gets localized string
/// Gets localized string
/// </summary>
/// <param name="localeName">Language name</param>
/// <param name="key">String key</param>
/// <returns>Localized string</returns>
string GetText(string localeName, string key);
/// <summary>
/// Reloads string cache
/// Reloads string cache
/// </summary>
void Reload();
/// <summary>
/// Gets command arg examples and description
/// Gets command arg examples and description
/// </summary>
/// <param name="localeName">Language name</param>
/// <param name="commandName">Command name</param>
CommandStrings GetCommandStrings(string localeName, string commandName);
}
}

View File

@@ -2,15 +2,15 @@
namespace NadekoBot.Services;
/// <summary>
/// Basic interface used for classes implementing strings loading mechanism
/// Basic interface used for classes implementing strings loading mechanism
/// </summary>
public interface IStringsSource
{
/// <summary>
/// Gets all response strings
/// Gets all response strings
/// </summary>
/// <returns>Dictionary(localename, Dictionary(key, response))</returns>
Dictionary<string, Dictionary<string, string>> GetResponseStrings();
Dictionary<string, Dictionary<string, CommandStrings>> GetCommandStrings();
}
}

View File

@@ -7,9 +7,10 @@ namespace NadekoBot.Services;
public class BotStrings : IBotStrings
{
/// <summary>
/// Used as failsafe in case response key doesn't exist in the selected or default language.
/// Used as failsafe in case response key doesn't exist in the selected or default language.
/// </summary>
private readonly CultureInfo _usCultureInfo = new("en-US");
private readonly ILocalization _localization;
private readonly IBotStringsProvider _stringsProvider;
@@ -31,15 +32,16 @@ public class BotStrings : IBotStrings
if (string.IsNullOrWhiteSpace(text))
{
Log.Warning("'{Key}' key is missing from '{LanguageName}' response strings. You may ignore this message", key, cultureInfo.Name);
Log.Warning("'{Key}' key is missing from '{LanguageName}' response strings. You may ignore this message",
key,
cultureInfo.Name);
text = GetString(key, _usCultureInfo) ?? $"Error: dkey {key} not found!";
if (string.IsNullOrWhiteSpace(text))
{
return
$"I can't tell you if the command is executed, because there was an error printing out the response." +
$" Key '{key}' is missing from resources. You may ignore this message.";
}
"I can't tell you if the command is executed, because there was an error printing out the response."
+ $" Key '{key}' is missing from resources. You may ignore this message.";
}
return text;
}
@@ -51,39 +53,38 @@ public class BotStrings : IBotStrings
}
catch (FormatException)
{
Log.Warning(" Key '{Key}' is not properly formatted in '{LanguageName}' response strings. Please report this", key, cultureInfo.Name);
Log.Warning(
" Key '{Key}' is not properly formatted in '{LanguageName}' response strings. Please report this",
key,
cultureInfo.Name);
if (cultureInfo.Name != _usCultureInfo.Name)
return GetText(key, _usCultureInfo, data);
return
$"I can't tell you if the command is executed, because there was an error printing out the response.\n" +
$"Key '{key}' is not properly formatted. Please report this.";
"I can't tell you if the command is executed, because there was an error printing out the response.\n"
+ $"Key '{key}' is not properly formatted. Please report this.";
}
}
public CommandStrings GetCommandStrings(string commandName, ulong? guildId = null)
=> GetCommandStrings(commandName, _localization.GetCultureInfo(guildId));
public CommandStrings GetCommandStrings(string commandName, CultureInfo cultureInfo)
{
var cmdStrings = _stringsProvider.GetCommandStrings(cultureInfo.Name, commandName);
var cmdStrings = _stringsProvider.GetCommandStrings(cultureInfo.Name, commandName);
if (cmdStrings is null)
{
if (cultureInfo.Name == _usCultureInfo.Name)
{
Log.Warning("'{CommandName}' doesn't exist in 'en-US' command strings. Please report this",
commandName);
return new()
{
Args = new[] {""},
Desc = "?"
};
return new() { Args = new[] { "" }, Desc = "?" };
}
// Log.Warning(@"'{CommandName}' command strings don't exist in '{LanguageName}' culture.
// This message is safe to ignore, however you can ask in Nadeko support server how you can contribute command translations",
// commandName, cultureInfo.Name);
return GetCommandStrings(commandName, _usCultureInfo);
}
@@ -98,6 +99,7 @@ public class CommandStrings
{
[YamlMember(Alias = "desc")]
public string Desc { get; set; }
[YamlMember(Alias = "args")]
public string[] Args { get; set; }
}
}

View File

@@ -6,20 +6,17 @@ public class LocalBotStringsProvider : IBotStringsProvider
private readonly IStringsSource _source;
private IReadOnlyDictionary<string, Dictionary<string, string>> responseStrings;
private IReadOnlyDictionary<string, Dictionary<string, CommandStrings>> commandStrings;
public LocalBotStringsProvider(IStringsSource source)
{
_source = source;
Reload();
}
public string GetText(string localeName, string key)
{
if (responseStrings.TryGetValue(localeName, out var langStrings)
&& langStrings.TryGetValue(key, out var text))
{
if (responseStrings.TryGetValue(localeName, out var langStrings) && langStrings.TryGetValue(key, out var text))
return text;
}
return null;
}
@@ -34,10 +31,8 @@ public class LocalBotStringsProvider : IBotStringsProvider
{
if (commandStrings.TryGetValue(localeName, out var langStrings)
&& langStrings.TryGetValue(commandName, out var strings))
{
return strings;
}
return null;
}
}
}

View File

@@ -5,25 +5,25 @@ using YamlDotNet.Serialization;
namespace NadekoBot.Services;
/// <summary>
/// Loads strings from the local default filepath <see cref="_responsesPath"/>
/// Loads strings from the local default filepath <see cref="_responsesPath" />
/// </summary>
public class LocalFileStringsSource : IStringsSource
{
private readonly string _responsesPath = "data/strings/responses";
private readonly string _commandsPath = "data/strings/commands";
public LocalFileStringsSource(string responsesPath = "data/strings/responses",
public LocalFileStringsSource(
string responsesPath = "data/strings/responses",
string commandsPath = "data/strings/commands")
{
_responsesPath = responsesPath;
_commandsPath = commandsPath;
}
public Dictionary<string, Dictionary<string, string>> GetResponseStrings()
{
var outputDict = new Dictionary<string, Dictionary<string, string>>();
foreach (var file in Directory.GetFiles(_responsesPath))
{
try
{
var langDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(file));
@@ -34,19 +34,16 @@ public class LocalFileStringsSource : IStringsSource
{
Log.Error(ex, "Error loading {FileName} response strings: {ErrorMessage}", file, ex.Message);
}
}
return outputDict;
}
public Dictionary<string, Dictionary<string, CommandStrings>> GetCommandStrings()
{
var deserializer = new DeserializerBuilder()
.Build();
var deserializer = new DeserializerBuilder().Build();
var outputDict = new Dictionary<string, Dictionary<string, CommandStrings>>();
foreach (var file in Directory.GetFiles(_commandsPath))
{
try
{
var text = File.ReadAllText(file);
@@ -58,11 +55,10 @@ public class LocalFileStringsSource : IStringsSource
{
Log.Error(ex, "Error loading {FileName} command strings: {ErrorMessage}", file, ex.Message);
}
}
return outputDict;
}
private static string GetLocaleName(string fileName)
{
fileName = Path.GetFileName(fileName);
@@ -70,4 +66,4 @@ public class LocalFileStringsSource : IStringsSource
var secondDotIndex = fileName.LastIndexOf('.');
return fileName.Substring(dotIndex, secondDotIndex - dotIndex);
}
}
}

View File

@@ -1,12 +1,12 @@
#nullable disable
using System.Web;
using StackExchange.Redis;
using System.Web;
namespace NadekoBot.Services;
/// <summary>
/// Uses <see cref="IStringsSource"/> to load strings into redis hash (only on Shard 0)
/// and retrieves them from redis via <see cref="GetText"/>
/// Uses <see cref="IStringsSource" /> to load strings into redis hash (only on Shard 0)
/// and retrieves them from redis via <see cref="GetText" />
/// </summary>
public class RedisBotStringsProvider : IBotStringsProvider
{
@@ -14,14 +14,17 @@ public class RedisBotStringsProvider : IBotStringsProvider
private readonly IStringsSource _source;
private readonly IBotCredentials _creds;
public RedisBotStringsProvider(ConnectionMultiplexer redis, DiscordSocketClient discordClient,
IStringsSource source, IBotCredentials creds)
public RedisBotStringsProvider(
ConnectionMultiplexer redis,
DiscordSocketClient discordClient,
IStringsSource source,
IBotCredentials creds)
{
_redis = redis;
_source = source;
_creds = creds;
if(discordClient.ShardId == 0)
if (discordClient.ShardId == 0)
Reload();
}
@@ -33,20 +36,18 @@ public class RedisBotStringsProvider : IBotStringsProvider
public CommandStrings GetCommandStrings(string localeName, string commandName)
{
string argsStr = _redis.GetDatabase().HashGet($"{_creds.RedisKey()}:commands:{localeName}", $"{commandName}::args");
string argsStr = _redis.GetDatabase()
.HashGet($"{_creds.RedisKey()}:commands:{localeName}", $"{commandName}::args");
if (argsStr == default)
return null;
var descStr = _redis.GetDatabase().HashGet($"{_creds.RedisKey()}:commands:{localeName}", $"{commandName}::desc");
var descStr = _redis.GetDatabase()
.HashGet($"{_creds.RedisKey()}:commands:{localeName}", $"{commandName}::desc");
if (descStr == default)
return null;
var args = Array.ConvertAll(argsStr.Split('&'), HttpUtility.UrlDecode);
return new()
{
Args = args,
Desc = descStr
};
return new() { Args = args, Desc = descStr };
}
public void Reload()
@@ -54,23 +55,20 @@ public class RedisBotStringsProvider : IBotStringsProvider
var redisDb = _redis.GetDatabase();
foreach (var (localeName, localeStrings) in _source.GetResponseStrings())
{
var hashFields = localeStrings
.Select(x => new HashEntry(x.Key, x.Value))
.ToArray();
var hashFields = localeStrings.Select(x => new HashEntry(x.Key, x.Value)).ToArray();
redisDb.HashSet($"{_creds.RedisKey()}:responses:{localeName}", hashFields);
}
foreach (var (localeName, localeStrings) in _source.GetCommandStrings())
{
var hashFields = localeStrings
.Select(x => new HashEntry($"{x.Key}::args",
string.Join('&', Array.ConvertAll(x.Value.Args, HttpUtility.UrlEncode))))
.Concat(localeStrings
.Select(x => new HashEntry($"{x.Key}::desc", x.Value.Desc)))
.ToArray();
.Select(x => new HashEntry($"{x.Key}::args",
string.Join('&', Array.ConvertAll(x.Value.Args, HttpUtility.UrlEncode))))
.Concat(localeStrings.Select(x => new HashEntry($"{x.Key}::desc", x.Value.Desc)))
.ToArray();
redisDb.HashSet($"{_creds.RedisKey()}:commands:{localeName}", hashFields);
}
}
}
}