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.

ParameterBuilder.cs 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Collections.Generic;
  5. namespace Discord.Commands.Builders
  6. {
  7. public class ParameterBuilder
  8. {
  9. private readonly List<ParameterPreconditionAttribute> _preconditions;
  10. private readonly List<Attribute> _attributes;
  11. public CommandBuilder Command { get; }
  12. public string Name { get; internal set; }
  13. public Type ParameterType { get; internal set; }
  14. public TypeReader TypeReader { get; set; }
  15. public bool IsOptional { get; set; }
  16. public bool IsRemainder { get; set; }
  17. public bool IsMultiple { get; set; }
  18. public object DefaultValue { get; set; }
  19. public string Summary { get; set; }
  20. public IReadOnlyList<ParameterPreconditionAttribute> Preconditions => _preconditions;
  21. public IReadOnlyList<Attribute> Attributes => _attributes;
  22. //Automatic
  23. internal ParameterBuilder(CommandBuilder command)
  24. {
  25. _preconditions = new List<ParameterPreconditionAttribute>();
  26. _attributes = new List<Attribute>();
  27. Command = command;
  28. }
  29. //User-defined
  30. internal ParameterBuilder(CommandBuilder command, string name, Type type)
  31. : this(command)
  32. {
  33. Discord.Preconditions.NotNull(name, nameof(name));
  34. Name = name;
  35. SetType(type);
  36. }
  37. internal void SetType(Type type)
  38. {
  39. var readers = Command.Module.Service.GetTypeReaders(type);
  40. if (readers != null)
  41. TypeReader = readers.FirstOrDefault().Value;
  42. else
  43. TypeReader = Command.Module.Service.GetDefaultTypeReader(type);
  44. if (TypeReader == null)
  45. throw new InvalidOperationException($"{type} does not have a TypeReader registered for it. Parameter: {Name} in {Command.PrimaryAlias}");
  46. if (type.GetTypeInfo().IsValueType)
  47. DefaultValue = Activator.CreateInstance(type);
  48. else if (type.IsArray)
  49. type = ParameterType.GetElementType();
  50. ParameterType = type;
  51. }
  52. public ParameterBuilder WithSummary(string summary)
  53. {
  54. Summary = summary;
  55. return this;
  56. }
  57. public ParameterBuilder WithDefault(object defaultValue)
  58. {
  59. DefaultValue = defaultValue;
  60. return this;
  61. }
  62. public ParameterBuilder WithIsOptional(bool isOptional)
  63. {
  64. IsOptional = isOptional;
  65. return this;
  66. }
  67. public ParameterBuilder WithIsRemainder(bool isRemainder)
  68. {
  69. IsRemainder = isRemainder;
  70. return this;
  71. }
  72. public ParameterBuilder WithIsMultiple(bool isMultiple)
  73. {
  74. IsMultiple = isMultiple;
  75. return this;
  76. }
  77. public ParameterBuilder AddAttributes(params Attribute[] attributes)
  78. {
  79. _attributes.AddRange(attributes);
  80. return this;
  81. }
  82. public ParameterBuilder AddPrecondition(ParameterPreconditionAttribute precondition)
  83. {
  84. _preconditions.Add(precondition);
  85. return this;
  86. }
  87. internal ParameterInfo Build(CommandInfo info)
  88. {
  89. if (TypeReader == null)
  90. throw new InvalidOperationException($"No type reader found for type {ParameterType.Name}, one must be specified");
  91. return new ParameterInfo(this, info, Command.Module.Service);
  92. }
  93. }
  94. }