Browse Source

add GenerateCustomIdString method to interaction service

pull/2169/head
Cenngo 3 years ago
parent
commit
4b02454ff8
3 changed files with 18 additions and 5 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/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 paramCount = paramList.Count();
var captureCount = wildcardCaptures?.Count() ?? 0; 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) if (context.Interaction is not IComponentInteraction messageComponent)
return ExecuteResult.FromError(InteractionCommandError.ParseFailed, $"Provided {nameof(IInteractionContext)} doesn't belong to a Component Command Interaction"); 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> /// <returns>
/// A task representing the conversion process. The task result contains the result of the conversion. /// A task representing the conversion process. The task result contains the result of the conversion.
/// </returns> /// </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> /// <summary>
/// Loads and caches an <see cref="ModalInfo"/> for the provided <see cref="IModal"/>. /// Loads and caches an <see cref="ModalInfo"/> for the provided <see cref="IModal"/>.


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

@@ -33,7 +33,7 @@ namespace Discord.Interactions
/// <returns> /// <returns>
/// A task representing the conversion process. The result of the task contains the conversion result. /// A task representing the conversion process. The result of the task contains the conversion result.
/// </returns> /// </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/> /// <inheritdoc/>


Loading…
Cancel
Save