Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Component TypeConverters and CustomID TypeReaders (#2169)
* fix sharded client current user * add custom setter to group property of module builder * rename serilazation method * init * create typemap and default typereaders * add default readers * create typereader targetting flags * seperate custom id readers with component typeconverters * add typereaders * add customid readers * clean up component info argument parsing * remove obsolete method * add component typeconverters to modals * fix build errors * add inline docs * bug fixes * code cleanup and refactorings * fix build errors * add GenerateCustomIdString method to interaction service * add GenerateCustomIdString method to interaction service * add inline docs to componentparameterbuilder * add inline docs to GenerateCustomIdStringAsync method
- Loading branch information
Showing
34 changed files
with
812 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/Discord.Net.Interactions/Builders/Parameters/ComponentCommandParameterBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
|
||
namespace Discord.Interactions.Builders | ||
{ | ||
/// <summary> | ||
/// Represents a builder for creating <see cref="ComponentCommandParameterInfo"/>. | ||
/// </summary> | ||
public class ComponentCommandParameterBuilder : ParameterBuilder<ComponentCommandParameterInfo, ComponentCommandParameterBuilder> | ||
{ | ||
/// <summary> | ||
/// Get the <see cref="ComponentTypeConverter"/> assigned to this parameter, if <see cref="IsRouteSegmentParameter"/> is <see langword="false"/>. | ||
/// </summary> | ||
public ComponentTypeConverter TypeConverter { get; private set; } | ||
|
||
/// <summary> | ||
/// Get the <see cref="Discord.Interactions.TypeReader"/> assigned to this parameter, if <see cref="IsRouteSegmentParameter"/> is <see langword="true"/>. | ||
/// </summary> | ||
public TypeReader TypeReader { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets whether this parameter is a CustomId segment or a Component value parameter. | ||
/// </summary> | ||
public bool IsRouteSegmentParameter { get; private set; } | ||
|
||
/// <inheritdoc/> | ||
protected override ComponentCommandParameterBuilder Instance => this; | ||
|
||
internal ComponentCommandParameterBuilder(ICommandBuilder command) : base(command) { } | ||
|
||
/// <summary> | ||
/// Initializes a new <see cref="ComponentCommandParameterBuilder"/>. | ||
/// </summary> | ||
/// <param name="command">Parent command of this parameter.</param> | ||
/// <param name="name">Name of this command.</param> | ||
/// <param name="type">Type of this parameter.</param> | ||
public ComponentCommandParameterBuilder(ICommandBuilder command, string name, Type type) : base(command, name, type) { } | ||
|
||
/// <inheritdoc/> | ||
public override ComponentCommandParameterBuilder SetParameterType(Type type) => SetParameterType(type, null); | ||
|
||
/// <summary> | ||
/// Sets <see cref="ParameterBuilder{TInfo, TBuilder}.ParameterType"/>. | ||
/// </summary> | ||
/// <param name="type">New value of the <see cref="ParameterBuilder{TInfo, TBuilder}.ParameterType"/>.</param> | ||
/// <param name="services">Service container to be used to resolve the dependencies of this parameters <see cref="Interactions.TypeConverter"/>.</param> | ||
/// <returns> | ||
/// The builder instance. | ||
/// </returns> | ||
public ComponentCommandParameterBuilder SetParameterType(Type type, IServiceProvider services) | ||
{ | ||
base.SetParameterType(type); | ||
|
||
if (IsRouteSegmentParameter) | ||
TypeReader = Command.Module.InteractionService.GetTypeReader(type); | ||
else | ||
TypeConverter = Command.Module.InteractionService.GetComponentTypeConverter(ParameterType, services); | ||
|
||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Sets <see cref="IsRouteSegmentParameter"/>. | ||
/// </summary> | ||
/// <param name="isRouteSegment">New value of the <see cref="IsRouteSegmentParameter"/>.</param> | ||
/// <returns> | ||
/// The builder instance. | ||
/// </returns> | ||
public ComponentCommandParameterBuilder SetIsRouteSegment(bool isRouteSegment) | ||
{ | ||
IsRouteSegmentParameter = isRouteSegment; | ||
return this; | ||
} | ||
|
||
internal override ComponentCommandParameterInfo Build(ICommandInfo command) | ||
=> new(this, command); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Discord.Interactions | ||
{ | ||
internal interface ITypeConverter<T> | ||
{ | ||
public bool CanConvertTo(Type type); | ||
|
||
public Task<TypeConverterResult> ReadAsync(IInteractionContext context, T option, IServiceProvider services); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.