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

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