You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandMap.cs 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System.Collections.Generic;
  2. namespace Discord.Commands
  3. {
  4. //Represents either a single function, command group, or both
  5. internal class CommandMap
  6. {
  7. private readonly CommandMap _parent;
  8. private readonly string _name, _fullName;
  9. private readonly List<Command> _commands;
  10. private readonly Dictionary<string, CommandMap> _items;
  11. private bool _isVisible, _hasNonAliases, _hasSubGroups;
  12. public string Name => _name;
  13. public string FullName => _fullName;
  14. public bool IsVisible => _isVisible;
  15. public bool HasNonAliases => _hasNonAliases;
  16. public bool HasSubGroups => _hasSubGroups;
  17. public IEnumerable<Command> Commands => _commands;
  18. public IEnumerable<CommandMap> SubGroups => _items.Values;
  19. public CommandMap()
  20. {
  21. _items = new Dictionary<string, CommandMap>();
  22. _commands = new List<Command>();
  23. _isVisible = false;
  24. _hasNonAliases = false;
  25. _hasSubGroups = false;
  26. }
  27. public CommandMap(CommandMap parent, string name, string fullName)
  28. : this()
  29. {
  30. _parent = parent;
  31. _name = name;
  32. _fullName = fullName;
  33. }
  34. public CommandMap GetItem(string text)
  35. {
  36. return GetItem(0, text.Split(' '));
  37. }
  38. public CommandMap GetItem(int index, string[] parts)
  39. {
  40. if (index != parts.Length)
  41. {
  42. string nextPart = parts[index];
  43. CommandMap nextGroup;
  44. if (_items.TryGetValue(nextPart.ToLowerInvariant(), out nextGroup))
  45. return nextGroup.GetItem(index + 1, parts);
  46. else
  47. return null;
  48. }
  49. return this;
  50. }
  51. public IEnumerable<Command> GetCommands()
  52. {
  53. if (_commands.Count > 0)
  54. return _commands;
  55. else if (_parent != null)
  56. return _parent.GetCommands();
  57. else
  58. return null;
  59. }
  60. public IEnumerable<Command> GetCommands(string text)
  61. {
  62. return GetCommands(0, text.Split(' '));
  63. }
  64. public IEnumerable<Command> GetCommands(int index, string[] parts)
  65. {
  66. if (index != parts.Length)
  67. {
  68. string nextPart = parts[index];
  69. CommandMap nextGroup;
  70. if (_items.TryGetValue(nextPart.ToLowerInvariant(), out nextGroup))
  71. {
  72. var cmd = nextGroup.GetCommands(index + 1, parts);
  73. if (cmd != null)
  74. return cmd;
  75. }
  76. }
  77. if (_commands != null)
  78. return _commands;
  79. return null;
  80. }
  81. public void AddCommand(string text, Command command, bool isAlias)
  82. {
  83. AddCommand(0, text.Split(' '), command, isAlias);
  84. }
  85. private void AddCommand(int index, string[] parts, Command command, bool isAlias)
  86. {
  87. if (!command.IsHidden)
  88. _isVisible = true;
  89. if (index != parts.Length)
  90. {
  91. CommandMap nextGroup;
  92. string name = parts[index].ToLowerInvariant();
  93. string fullName = string.Join(" ", parts, 0, index + 1);
  94. if (!_items.TryGetValue(name, out nextGroup))
  95. {
  96. nextGroup = new CommandMap(this, name, fullName);
  97. _items.Add(name, nextGroup);
  98. _hasSubGroups = true;
  99. }
  100. nextGroup.AddCommand(index + 1, parts, command, isAlias);
  101. }
  102. else
  103. {
  104. _commands.Add(command);
  105. if (!isAlias)
  106. _hasNonAliases = true;
  107. }
  108. }
  109. public bool CanRun(User user, Channel channel, out string error)
  110. {
  111. error = null;
  112. if (_commands.Count > 0)
  113. {
  114. foreach (var cmd in _commands)
  115. {
  116. if (cmd.CanRun(user, channel, out error))
  117. return true;
  118. }
  119. }
  120. if (_items.Count > 0)
  121. {
  122. foreach (var item in _items)
  123. {
  124. if (item.Value.CanRun(user, channel, out error))
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. }
  131. }