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