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.

CommandBuilder.cs 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using Discord.Commands.Permissions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. namespace Discord.Commands
  7. {
  8. //TODO: Make this more friendly and expose it to be extendable
  9. public sealed class CommandBuilder
  10. {
  11. private readonly CommandService _service;
  12. private readonly Command _command;
  13. private readonly List<CommandParameter> _params;
  14. private readonly List<IPermissionChecker> _checks;
  15. private readonly List<string> _aliases;
  16. private readonly string _prefix;
  17. private bool _allowRequiredParams, _areParamsClosed;
  18. public CommandService Service => _service;
  19. internal CommandBuilder(CommandService service, string text, string prefix = "", string category = "", IEnumerable<IPermissionChecker> initialChecks = null)
  20. {
  21. _service = service;
  22. _prefix = prefix;
  23. _command = new Command(AppendPrefix(prefix, text));
  24. _command.Category = category;
  25. if (initialChecks != null)
  26. _checks = new List<IPermissionChecker>(initialChecks);
  27. else
  28. _checks = new List<IPermissionChecker>();
  29. _params = new List<CommandParameter>();
  30. _aliases = new List<string>();
  31. _allowRequiredParams = true;
  32. _areParamsClosed = false;
  33. }
  34. public CommandBuilder Alias(params string[] aliases)
  35. {
  36. _aliases.AddRange(aliases);
  37. return this;
  38. }
  39. /*public CommandBuilder Category(string category)
  40. {
  41. _command.Category = category;
  42. return this;
  43. }*/
  44. public CommandBuilder Description(string description)
  45. {
  46. _command.Description = description;
  47. return this;
  48. }
  49. public CommandBuilder Parameter(string name, ParameterType type = ParameterType.Required)
  50. {
  51. if (_areParamsClosed)
  52. throw new Exception($"No parameters may be added after a {nameof(ParameterType.Multiple)} or {nameof(ParameterType.Unparsed)} parameter.");
  53. if (!_allowRequiredParams && type == ParameterType.Required)
  54. throw new Exception($"{nameof(ParameterType.Required)} parameters may not be added after an optional one");
  55. _params.Add(new CommandParameter(name, type));
  56. if (type == ParameterType.Optional)
  57. _allowRequiredParams = false;
  58. if (type == ParameterType.Multiple || type == ParameterType.Unparsed)
  59. _areParamsClosed = true;
  60. return this;
  61. }
  62. public CommandBuilder Hide()
  63. {
  64. _command.IsHidden = true;
  65. return this;
  66. }
  67. public CommandBuilder AddCheck(IPermissionChecker check)
  68. {
  69. _checks.Add(check);
  70. return this;
  71. }
  72. public CommandBuilder AddCheck(Func<Command, User, Channel, bool> checkFunc, string errorMsg = null)
  73. {
  74. _checks.Add(new GenericPermissionChecker(checkFunc, errorMsg));
  75. return this;
  76. }
  77. public void Do(Func<CommandEventArgs, Task> func)
  78. {
  79. _command.SetRunFunc(func);
  80. Build();
  81. }
  82. public void Do(Action<CommandEventArgs> func)
  83. {
  84. _command.SetRunFunc(func);
  85. Build();
  86. }
  87. private void Build()
  88. {
  89. _command.SetParameters(_params.ToArray());
  90. _command.SetChecks(_checks.ToArray());
  91. _command.SetAliases(_aliases.Select(x => AppendPrefix(_prefix, x)).ToArray());
  92. _service.AddCommand(_command);
  93. }
  94. internal static string AppendPrefix(string prefix, string cmd)
  95. {
  96. if (cmd != "")
  97. {
  98. if (prefix != "")
  99. return prefix + ' ' + cmd;
  100. else
  101. return cmd;
  102. }
  103. else
  104. return prefix;
  105. }
  106. }
  107. public class CommandGroupBuilder
  108. {
  109. private readonly CommandService _service;
  110. private readonly string _prefix;
  111. private readonly List<IPermissionChecker> _checks;
  112. private string _category;
  113. public CommandService Service => _service;
  114. internal CommandGroupBuilder(CommandService service, string prefix = "", string category = null, IEnumerable<IPermissionChecker> initialChecks = null)
  115. {
  116. _service = service;
  117. _prefix = prefix;
  118. _category = category;
  119. if (initialChecks != null)
  120. _checks = new List<IPermissionChecker>(initialChecks);
  121. else
  122. _checks = new List<IPermissionChecker>();
  123. }
  124. public CommandGroupBuilder Category(string category)
  125. {
  126. _category = category;
  127. return this;
  128. }
  129. public void AddCheck(IPermissionChecker checker)
  130. {
  131. _checks.Add(checker);
  132. }
  133. public void AddCheck(Func<Command, User, Channel, bool> checkFunc, string errorMsg = null)
  134. {
  135. _checks.Add(new GenericPermissionChecker(checkFunc, errorMsg));
  136. }
  137. public CommandGroupBuilder CreateGroup(string cmd, Action<CommandGroupBuilder> config)
  138. {
  139. config(new CommandGroupBuilder(_service, CommandBuilder.AppendPrefix(_prefix, cmd), _category, _checks));
  140. return this;
  141. }
  142. public CommandBuilder CreateCommand()
  143. => CreateCommand("");
  144. public CommandBuilder CreateCommand(string cmd)
  145. => new CommandBuilder(_service, cmd, _prefix, _category, _checks);
  146. }
  147. }