Browse Source

add GenerateCustomIdString method to interaction service

pull/2169/head
Cenngo 3 years ago
parent
commit
98e9e79a76
5 changed files with 20 additions and 7 deletions
  1. +0
    -2
      src/Discord.Net.Interactions/Info/Commands/ComponentCommandInfo.cs
  2. +17
    -2
      src/Discord.Net.Interactions/InteractionService.cs
  3. +1
    -1
      src/Discord.Net.Interactions/TypeReaders/DefaultSnowflakeReader.cs
  4. +1
    -1
      src/Discord.Net.Interactions/TypeReaders/EnumReader.cs
  5. +1
    -1
      src/Discord.Net.Interactions/TypeReaders/TypeReader.cs

+ 0
- 2
src/Discord.Net.Interactions/Info/Commands/ComponentCommandInfo.cs View File

@@ -51,8 +51,6 @@ namespace Discord.Interactions
var paramCount = paramList.Count();
var captureCount = wildcardCaptures?.Count() ?? 0;

if (paramCount < captureCount + 1)
return await InvokeEventAndReturn(context, ExecuteResult.FromError(InteractionCommandError.BadArgs, "Command was invoked with too many parameters")).ConfigureAwait(false);
if (context.Interaction is not IComponentInteraction messageComponent)
return ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Component Command Interaction");



+ 17
- 2
src/Discord.Net.Interactions/InteractionService.cs View File

@@ -914,8 +914,23 @@ namespace Discord.Interactions
/// <returns>
/// A task representing the conversion process. The task result contains the result of the conversion.
/// </returns>
public Task<string> SerializeValue<T>(T obj, IServiceProvider services = null) =>
_typeReaderMap.Get(typeof(T), services).SerializeAsync(obj);
public Task<string> SerializeValueAsync<T>(T obj, IServiceProvider services) =>
_typeReaderMap.Get(typeof(T), services).SerializeAsync(obj, services);

public async Task<string> GenerateCustomIdStringAsync(string format, IServiceProvider services, params object[] args)
{
var serializedValues = new string[args.Length];

for(var i = 0; i < args.Length; i++)
{
var arg = args[i];
var typeReader = _typeReaderMap.Get(arg.GetType(), null);
var result = await typeReader.SerializeAsync(arg, services);
serializedValues[i] = result;
}

return string.Format(format, serializedValues);
}

/// <summary>
/// Loads and caches an <see cref="ModalInfo"/> for the provided <see cref="IModal"/>.


+ 1
- 1
src/Discord.Net.Interactions/TypeReaders/DefaultSnowflakeReader.cs View File

@@ -19,7 +19,7 @@ namespace Discord.Interactions
TypeConverterResult.FromSuccess(result) : TypeConverterResult.FromError(InteractionCommandError.ConvertFailed, $"{option} must be a valid {typeof(T).Name} snowflake to be parsed.");
}

public override Task<string> SerializeAsync(object obj) => Task.FromResult((obj as ISnowflakeEntity)?.Id.ToString());
public override Task<string> SerializeAsync(object obj, IServiceProvider services) => Task.FromResult((obj as ISnowflakeEntity)?.Id.ToString());
}

internal sealed class DefaultUserReader<T> : DefaultSnowflakeReader<T>


+ 1
- 1
src/Discord.Net.Interactions/TypeReaders/EnumReader.cs View File

@@ -12,7 +12,7 @@ namespace Discord.Interactions
TypeConverterResult.FromSuccess(result) : TypeConverterResult.FromError(InteractionCommandError.ConvertFailed, $"Value {option} cannot be converted to {nameof(T)}"));
}

public override Task<string> SerializeAsync(object obj)
public override Task<string> SerializeAsync(object obj, IServiceProvider services)
{
var name = Enum.GetName(typeof(T), obj);



+ 1
- 1
src/Discord.Net.Interactions/TypeReaders/TypeReader.cs View File

@@ -33,7 +33,7 @@ namespace Discord.Interactions
/// <returns>
/// A task representing the conversion process. The result of the task contains the conversion result.
/// </returns>
public virtual Task<string> SerializeAsync(object obj) => Task.FromResult(obj.ToString());
public virtual Task<string> SerializeAsync(object obj, IServiceProvider services) => Task.FromResult(obj.ToString());
}

/// <inheritdoc/>


Loading…
Cancel
Save