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 12 kB

8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 CommandService _commandService;
  20. private readonly Func<ICommandContext, object[], IServiceProvider, CommandInfo, Task> _action;
  21. public ModuleInfo Module { get; }
  22. public string Name { get; }
  23. public string Summary { get; }
  24. public string Remarks { get; }
  25. public int Priority { get; }
  26. public bool HasVarArgs { get; }
  27. public RunMode RunMode { get; }
  28. public IReadOnlyList<string> Aliases { get; }
  29. public IReadOnlyList<ParameterInfo> Parameters { get; }
  30. public IReadOnlyList<PreconditionAttribute> Preconditions { get; }
  31. public IReadOnlyList<Attribute> Attributes { get; }
  32. internal CommandInfo(CommandBuilder builder, ModuleInfo module, CommandService service)
  33. {
  34. Module = module;
  35. Name = builder.Name;
  36. Summary = builder.Summary;
  37. Remarks = builder.Remarks;
  38. RunMode = (builder.RunMode == RunMode.Default ? service._defaultRunMode : builder.RunMode);
  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. Attributes = builder.Attributes.ToImmutableArray();
  54. Parameters = builder.Parameters.Select(x => x.Build(this)).ToImmutableArray();
  55. HasVarArgs = builder.Parameters.Count > 0 ? builder.Parameters[builder.Parameters.Count - 1].IsMultiple : false;
  56. _action = builder.Callback;
  57. _commandService = service;
  58. }
  59. public async Task<PreconditionResult> CheckPreconditionsAsync(ICommandContext context, IServiceProvider services = null)
  60. {
  61. services = services ?? EmptyServiceProvider.Instance;
  62. async Task<PreconditionResult> CheckGroups(IEnumerable<PreconditionAttribute> preconditions, string type)
  63. {
  64. foreach (IGrouping<string, PreconditionAttribute> preconditionGroup in preconditions.GroupBy(p => p.Group, StringComparer.Ordinal))
  65. {
  66. if (preconditionGroup.Key == null)
  67. {
  68. foreach (PreconditionAttribute precondition in preconditionGroup)
  69. {
  70. var result = await precondition.CheckPermissionsAsync(context, this, services).ConfigureAwait(false);
  71. if (!result.IsSuccess)
  72. return result;
  73. }
  74. }
  75. else
  76. {
  77. var results = new List<PreconditionResult>();
  78. foreach (PreconditionAttribute precondition in preconditionGroup)
  79. results.Add(await precondition.CheckPermissionsAsync(context, this, services).ConfigureAwait(false));
  80. if (!results.Any(p => p.IsSuccess))
  81. return PreconditionGroupResult.FromError($"{type} precondition group {preconditionGroup.Key} failed.", results);
  82. }
  83. }
  84. return PreconditionGroupResult.FromSuccess();
  85. }
  86. var moduleResult = await CheckGroups(Module.Preconditions, "Module");
  87. if (!moduleResult.IsSuccess)
  88. return moduleResult;
  89. var commandResult = await CheckGroups(Preconditions, "Command");
  90. if (!commandResult.IsSuccess)
  91. return commandResult;
  92. return PreconditionResult.FromSuccess();
  93. }
  94. public async Task<ParseResult> ParseAsync(ICommandContext context, int startIndex, SearchResult searchResult, PreconditionResult preconditionResult = null, IServiceProvider services = null)
  95. {
  96. services = services ?? EmptyServiceProvider.Instance;
  97. if (!searchResult.IsSuccess)
  98. return ParseResult.FromError(searchResult);
  99. if (preconditionResult != null && !preconditionResult.IsSuccess)
  100. return ParseResult.FromError(preconditionResult);
  101. string input = searchResult.Text.Substring(startIndex);
  102. return await CommandParser.ParseArgsAsync(this, context, _commandService._ignoreExtraArgs, services, input, 0).ConfigureAwait(false);
  103. }
  104. public Task<IResult> ExecuteAsync(ICommandContext context, ParseResult parseResult, IServiceProvider services)
  105. {
  106. if (!parseResult.IsSuccess)
  107. return Task.FromResult((IResult)ExecuteResult.FromError(parseResult));
  108. var argList = new object[parseResult.ArgValues.Count];
  109. for (int i = 0; i < parseResult.ArgValues.Count; i++)
  110. {
  111. if (!parseResult.ArgValues[i].IsSuccess)
  112. return Task.FromResult((IResult)ExecuteResult.FromError(parseResult.ArgValues[i]));
  113. argList[i] = parseResult.ArgValues[i].Values.First().Value;
  114. }
  115. var paramList = new object[parseResult.ParamValues.Count];
  116. for (int i = 0; i < parseResult.ParamValues.Count; i++)
  117. {
  118. if (!parseResult.ParamValues[i].IsSuccess)
  119. return Task.FromResult((IResult)ExecuteResult.FromError(parseResult.ParamValues[i]));
  120. paramList[i] = parseResult.ParamValues[i].Values.First().Value;
  121. }
  122. return ExecuteAsync(context, argList, paramList, services);
  123. }
  124. public async Task<IResult> ExecuteAsync(ICommandContext context, IEnumerable<object> argList, IEnumerable<object> paramList, IServiceProvider services)
  125. {
  126. services = services ?? EmptyServiceProvider.Instance;
  127. try
  128. {
  129. object[] args = GenerateArgs(argList, paramList);
  130. for (int position = 0; position < Parameters.Count; position++)
  131. {
  132. var parameter = Parameters[position];
  133. object argument = args[position];
  134. var result = await parameter.CheckPreconditionsAsync(context, argument, services).ConfigureAwait(false);
  135. if (!result.IsSuccess)
  136. return ExecuteResult.FromError(result);
  137. }
  138. switch (RunMode)
  139. {
  140. case RunMode.Sync: //Always sync
  141. return await ExecuteInternalAsync(context, args, services).ConfigureAwait(false);
  142. case RunMode.Async: //Always async
  143. var t2 = Task.Run(async () =>
  144. {
  145. await ExecuteInternalAsync(context, args, services).ConfigureAwait(false);
  146. });
  147. break;
  148. }
  149. return ExecuteResult.FromSuccess();
  150. }
  151. catch (Exception ex)
  152. {
  153. return ExecuteResult.FromError(ex);
  154. }
  155. }
  156. private async Task<IResult> ExecuteInternalAsync(ICommandContext context, object[] args, IServiceProvider services)
  157. {
  158. await Module.Service._cmdLogger.DebugAsync($"Executing {GetLogText(context)}").ConfigureAwait(false);
  159. try
  160. {
  161. var task = _action(context, args, services, this);
  162. if (task is Task<IResult> resultTask)
  163. {
  164. var result = await resultTask.ConfigureAwait(false);
  165. await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false);
  166. if (result is RuntimeResult execResult)
  167. return execResult;
  168. }
  169. else if (task is Task<ExecuteResult> execTask)
  170. {
  171. var result = await execTask.ConfigureAwait(false);
  172. await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false);
  173. return result;
  174. }
  175. else
  176. {
  177. await task.ConfigureAwait(false);
  178. var result = ExecuteResult.FromSuccess();
  179. await Module.Service._commandExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false);
  180. }
  181. var executeResult = ExecuteResult.FromSuccess();
  182. return executeResult;
  183. }
  184. catch (Exception ex)
  185. {
  186. var originalEx = ex;
  187. while (ex is TargetInvocationException) //Happens with void-returning commands
  188. ex = ex.InnerException;
  189. var wrappedEx = new CommandException(this, context, ex);
  190. await Module.Service._cmdLogger.ErrorAsync(wrappedEx).ConfigureAwait(false);
  191. if (Module.Service._throwOnError)
  192. {
  193. if (ex == originalEx)
  194. throw;
  195. else
  196. ExceptionDispatchInfo.Capture(ex).Throw();
  197. }
  198. return ExecuteResult.FromError(CommandError.Exception, ex.Message);
  199. }
  200. finally
  201. {
  202. await Module.Service._cmdLogger.VerboseAsync($"Executed {GetLogText(context)}").ConfigureAwait(false);
  203. }
  204. }
  205. private object[] GenerateArgs(IEnumerable<object> argList, IEnumerable<object> paramsList)
  206. {
  207. int argCount = Parameters.Count;
  208. var array = new object[Parameters.Count];
  209. if (HasVarArgs)
  210. argCount--;
  211. int i = 0;
  212. foreach (object arg in argList)
  213. {
  214. if (i == argCount)
  215. throw new InvalidOperationException("Command was invoked with too many parameters");
  216. array[i++] = arg;
  217. }
  218. if (i < argCount)
  219. throw new InvalidOperationException("Command was invoked with too few parameters");
  220. if (HasVarArgs)
  221. {
  222. var func = _arrayConverters.GetOrAdd(Parameters[Parameters.Count - 1].Type, t =>
  223. {
  224. var method = _convertParamsMethod.MakeGenericMethod(t);
  225. return (Func<IEnumerable<object>, object>)method.CreateDelegate(typeof(Func<IEnumerable<object>, object>));
  226. });
  227. array[i] = func(paramsList);
  228. }
  229. return array;
  230. }
  231. private static T[] ConvertParamsList<T>(IEnumerable<object> paramsList)
  232. => paramsList.Cast<T>().ToArray();
  233. internal string GetLogText(ICommandContext context)
  234. {
  235. if (context.Guild != null)
  236. return $"\"{Name}\" for {context.User} in {context.Guild}/{context.Channel}";
  237. else
  238. return $"\"{Name}\" for {context.User} in {context.Channel}";
  239. }
  240. }
  241. }