Browse Source

replace the command path method in command map

pull/2211/head
Cenngo 3 years ago
parent
commit
f1cf2160da
3 changed files with 13 additions and 27 deletions
  1. +4
    -3
      src/Discord.Net.Interactions/LocalizationManagers/JsonLocalizationManager.cs
  2. +7
    -3
      src/Discord.Net.Interactions/LocalizationManagers/ResxLocalizationManager.cs
  3. +2
    -21
      src/Discord.Net.Interactions/Map/CommandMap.cs

+ 4
- 3
src/Discord.Net.Interactions/LocalizationManagers/JsonLocalizationManager.cs View File

@@ -23,10 +23,10 @@ namespace Discord.Interactions
_fileName = fileName;
}

public Task<IDictionary<string, string>> GetAllDescriptionsAsync(IList<string> key, LocalizationTarget destinationType, IServiceProvider serviceProvider) =>
public Task<IDictionary<string, string>> GetAllDescriptionsAsync(IList<string> key, LocalizationTarget destinationType) =>
GetValuesAsync(key, DescriptionIdentifier);

public Task<IDictionary<string, string>> GetAllNamesAsync(IList<string> key, LocalizationTarget destinationType, IServiceProvider serviceProvider) =>
public Task<IDictionary<string, string>> GetAllNamesAsync(IList<string> key, LocalizationTarget destinationType) =>
GetValuesAsync(key, NameIdentifier);

private string[] GetAllFiles() =>
@@ -50,7 +50,8 @@ namespace Discord.Interactions
var obj = await JObject.LoadAsync(jr);
var token = string.Join(".", key) + $".{identifier}";
var value = (string)obj.SelectToken(token);
result[locale] = value;
if (value is not null)
result[locale] = value;
}

return result;


+ 7
- 3
src/Discord.Net.Interactions/LocalizationManagers/ResxLocalizationManager.cs View File

@@ -25,10 +25,10 @@ namespace Discord.Interactions
_supportedLocales = supportedLocales;
}

public Task<IDictionary<string, string>> GetAllDescriptionsAsync(IList<string> key, LocalizationTarget destinationType, IServiceProvider serviceProvider) =>
public Task<IDictionary<string, string>> GetAllDescriptionsAsync(IList<string> key, LocalizationTarget destinationType) =>
Task.FromResult(GetValues(key, DescriptionIdentifier));

public Task<IDictionary<string, string>> GetAllNamesAsync(IList<string> key, LocalizationTarget destinationType, IServiceProvider serviceProvider) =>
public Task<IDictionary<string, string>> GetAllNamesAsync(IList<string> key, LocalizationTarget destinationType) =>
Task.FromResult(GetValues(key, NameIdentifier));

private IDictionary<string, string> GetValues(IList<string> key, string identifier)
@@ -39,7 +39,11 @@ namespace Discord.Interactions
var resourceManager = _localizerCache.GetOrAdd(resourceName, new ResourceManager(resourceName, _assembly));

foreach (var locale in _supportedLocales)
result[locale.Name] = resourceManager.GetString(identifier, locale);
{
var value = resourceManager.GetString(identifier, locale);
if (value is not null)
result[locale.Name] = value;
}

return result;
}


+ 2
- 21
src/Discord.Net.Interactions/Map/CommandMap.cs View File

@@ -42,7 +42,7 @@ namespace Discord.Interactions

public void RemoveCommand(T command)
{
var key = ParseCommandName(command);
var key = CommandHierarchy.GetCommandPath(command);

_root.RemoveCommand(key, 0);
}
@@ -60,28 +60,9 @@ namespace Discord.Interactions

private void AddCommand(T command)
{
var key = ParseCommandName(command);
var key = CommandHierarchy.GetCommandPath(command);

_root.AddCommand(key, 0, command);
}

private IList<string> ParseCommandName(T command)
{
var keywords = new List<string>() { command.Name };

var currentParent = command.Module;

while (currentParent != null)
{
if (!string.IsNullOrEmpty(currentParent.SlashGroupName))
keywords.Add(currentParent.SlashGroupName);

currentParent = currentParent.Parent;
}

keywords.Reverse();

return keywords;
}
}
}

Loading…
Cancel
Save