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.

CommandInfo.cs 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using Discord.Commands.Builders;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.Collections.Concurrent;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Runtime.ExceptionServices;
  10. using System.Threading.Tasks;
  11. using Microsoft.Extensions.DependencyInjection;
  12. namespace Discord.Commands
  13. {
  14. [DebuggerDisplay("{Name,nq}")]
  15. public class CommandInfo
  16. {
  17. private static readonly System.Reflection.MethodInfo _convertParamsMethod = typeof(CommandInfo).GetTypeInfo().GetDeclaredMethod(nameof(ConvertParamsList));
  18. private static readonly ConcurrentDictionary<Type, Func<IEnumerable<object>, object>> _arrayConverters = new ConcurrentDictionary<Type, Func<IEnumerable<object>, object>>();
  19. private readonly Func<ICommandContext, object[], IServiceProvider, Task> _action;
  20. public ModuleInfo Module { get; }
  21. public string Name { get; }
  22. public string Summary { get; }
  23. public string Remarks { get; }
  24. public int Priority { get; }
  25. public bool HasVarArgs { get; }
  26. public RunMode RunMode { get; }
  27. public PreconditionsMode PreconditionsMode { get; }
  28. public IReadOnlyList<string> Aliases { get; }
  29. public IReadOnlyList<ParameterInfo> Parameters { get; }
  30. public IReadOnlyList<PreconditionAttribute> Preconditions { get; }
  31. internal CommandInfo(CommandBuilder builder, ModuleInfo module, CommandService service)
  32. {
  33. Module = module;
  34. Name = builder.Name;
  35. Summary = builder.Summary;
  36. Remarks = builder.Remarks;
  37. RunMode = (builder.RunMode == RunMode.Default ? service._defaultRunMode : builder.RunMode);
  38. PreconditionsMode = (builder.PreconditionsMode == PreconditionsMode.Default ? service._defaultPreconditionsMode : builder.PreconditionsMode);
  39. Priority = builder.Priority;
  40. Aliases = module.Aliases
  41. .Permutate(builder.Aliases, (first, second) =>
  42. {
  43. if (first == "")
  44. return second;
  45. else if (second == "")
  46. return first;
  47. else
  48. return first + service._separatorChar + second;
  49. })
  50. .Select(x => service._caseSensitive ? x : x.ToLowerInvariant())
  51. .ToImmutableArray();
  52. Preconditions = builder.Preconditions.ToImmutableArray();
  53. Parameters = builder.Parameters.Select(x => x.Build(this)).ToImmutableArray();
  54. HasVarArgs = builder.Parameters.Count > 0 ? builder.Parameters[builder.Parameters.Count - 1].IsMultiple : false;
  55. _action = builder.Callback;
  56. }
  57. public async Task<PreconditionResult> CheckPreconditionsAsync(ICommandContext context, IServiceProvider services = null)
  58. {
  59. services = services ?? EmptyServiceProvider.Instance;
  60. foreach (PreconditionAttribute precondition in Module.Preconditions)
  61. {
  62. var result = await precondition.CheckPermissions(context, this, services).ConfigureAwait(false);
  63. if (!result.IsSuccess)
  64. return result;
  65. }
  66. if (PreconditionsMode == PreconditionsMode.RequireAll)
  67. {
  68. foreach (PreconditionAttribute precondition in Preconditions)
  69. {
  70. var result = await precondition.CheckPermissions(context, this, services).ConfigureAwait(false);
  71. if (!result.IsSuccess)
  72. return result;
  73. }
  74. }
  75. else
  76. {
  77. foreach (PreconditionAttribute precondition in Preconditions)
  78. {
  79. var result = await precondition.CheckPermissions(context, this, services).ConfigureAwait(false);
  80. if (result.IsSuccess)
  81. return result;
  82. }
  83. }
  84. return PreconditionResult.FromSuccess();
  85. }
  86. public async Task<ParseResult> ParseAsync(ICommandContext context, int startIndex, SearchResult searchResult, PreconditionResult? preconditionResult = null)
  87. {
  88. if (!searchResult.IsSuccess)
  89. return ParseResult.FromError(searchResult);
  90. if (preconditionResult != null && !preconditionResult.Value.IsSuccess)
  91. return ParseResult.FromError(preconditionResult.Value);
  92. string input = searchResult.Text.Substring(startIndex);
  93. return await CommandParser.ParseArgs(this, context, input, 0).ConfigureAwait(false);
  94. }
  95. public Task<ExecuteResult> ExecuteAsync(ICommandContext context, ParseResult parseResult, IServiceProvider services)
  96. {
  97. if (!parseResult.IsSuccess)
  98. return Task.FromResult(ExecuteResult.FromError(parseResult));
  99. var argList = new object[parseResult.ArgValues.Count];
  100. for (int i = 0; i < parseResult.ArgValues.Count; i++)
  101. {
  102. if (!parseResult.ArgValues[i].IsSuccess)
  103. return Task.FromResult(ExecuteResult.FromError(parseResult.ArgValues[i]));
  104. argList[i] = parseResult.ArgValues[i].Values.First().Value;
  105. }
  106. var paramList = new object[parseResult.ParamValues.Count];
  107. for (int i = 0; i < parseResult.ParamValues.Count; i++)
  108. {
  109. if (!parseResult.ParamValues[i].IsSuccess)
  110. return Task.FromResult(ExecuteResult.FromError(parseResult.ParamValues[i]));
  111. paramList[i] = parseResult.ParamValues[i].Values.First().Value;
  112. }
  113. return ExecuteAsync(context, argList, paramList, services);
  114. }
  115. public async Task<ExecuteResult> ExecuteAsync(ICommandContext context, IEnumerable<object> argList, IEnumerable<object> paramList, IServiceProvider services)
  116. {
  117. services = services ?? EmptyServiceProvider.Instance;
  118. try
  119. {
  120. object[] args = GenerateArgs(argList, paramList);
  121. for (int position = 0; position < Parameters.Count; position++)
  122. {
  123. var parameter = Parameters[position];
  124. var argument = args[position];
  125. var result = await parameter.CheckPreconditionsAsync(context, argument, services).ConfigureAwait(false);
  126. if (!result.IsSuccess)
  127. return ExecuteResult.FromError(result);
  128. }
  129. switch (RunMode)
  130. {
  131. case RunMode.Sync: //Always sync
  132. await ExecuteAsyncInternal(context, args, services).ConfigureAwait(false);
  133. break;
  134. case RunMode.Async: //Always async
  135. var t2 = Task.Run(async () =>
  136. {
  137. await ExecuteAsyncInternal(context, args, services).ConfigureAwait(false);
  138. });
  139. break;
  140. }
  141. return ExecuteResult.FromSuccess();
  142. }
  143. catch (Exception ex)
  144. {
  145. return ExecuteResult.FromError(ex);
  146. }
  147. }
  148. private async Task ExecuteAsyncInternal(ICommandContext context, object[] args, IServiceProvider services)
  149. {
  150. await Module.Service._cmdLogger.DebugAsync($"Executing {GetLogText(context)}").ConfigureAwait(false);
  151. try
  152. {
  153. await _action(context, args, services).ConfigureAwait(false);
  154. }
  155. catch (Exception ex)
  156. {
  157. var originalEx = ex;
  158. while (ex is TargetInvocationException) //Happens with void-returning commands
  159. ex = ex.InnerException;
  160. var wrappedEx = new CommandException(this, context, ex);
  161. await Module.Service._cmdLogger.ErrorAsync(wrappedEx).ConfigureAwait(false);
  162. if (Module.Service._throwOnError)
  163. {
  164. if (ex == originalEx)
  165. throw;
  166. else
  167. ExceptionDispatchInfo.Capture(ex).Throw();
  168. }
  169. }
  170. await Module.Service._cmdLogger.VerboseAsync($"Executed {GetLogText(context)}").ConfigureAwait(false);
  171. }
  172. private object[] GenerateArgs(IEnumerable<object> argList, IEnumerable<object> paramsList)
  173. {
  174. int argCount = Parameters.Count;
  175. var array = new object[Parameters.Count];
  176. if (HasVarArgs)
  177. argCount--;
  178. int i = 0;
  179. foreach (var arg in argList)
  180. {
  181. if (i == argCount)
  182. throw new InvalidOperationException("Command was invoked with too many parameters");
  183. array[i++] = arg;
  184. }
  185. if (i < argCount)
  186. throw new InvalidOperationException("Command was invoked with too few parameters");
  187. if (HasVarArgs)
  188. {
  189. var func = _arrayConverters.GetOrAdd(Parameters[Parameters.Count - 1].Type, t =>
  190. {
  191. var method = _convertParamsMethod.MakeGenericMethod(t);
  192. return (Func<IEnumerable<object>, object>)method.CreateDelegate(typeof(Func<IEnumerable<object>, object>));
  193. });
  194. array[i] = func(paramsList);
  195. }
  196. return array;
  197. }
  198. private static T[] ConvertParamsList<T>(IEnumerable<object> paramsList)
  199. => paramsList.Cast<T>().ToArray();
  200. internal string GetLogText(ICommandContext context)
  201. {
  202. if (context.Guild != null)
  203. return $"\"{Name}\" for {context.User} in {context.Guild}/{context.Channel}";
  204. else
  205. return $"\"{Name}\" for {context.User} in {context.Channel}";
  206. }
  207. }
  208. }