From 4b02454ff8cab5301206615d434fba029a918643 Mon Sep 17 00:00:00 2001 From: Cenngo Date: Sun, 6 Mar 2022 23:45:08 +0300 Subject: [PATCH] add GenerateCustomIdString method to interaction service --- .../Info/Commands/ComponentCommandInfo.cs | 2 -- .../InteractionService.cs | 19 +++++++++++++++++-- .../TypeReaders/TypeReader.cs | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Discord.Net.Interactions/Info/Commands/ComponentCommandInfo.cs b/src/Discord.Net.Interactions/Info/Commands/ComponentCommandInfo.cs index 186419cfb..22d6aba6c 100644 --- a/src/Discord.Net.Interactions/Info/Commands/ComponentCommandInfo.cs +++ b/src/Discord.Net.Interactions/Info/Commands/ComponentCommandInfo.cs @@ -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"); diff --git a/src/Discord.Net.Interactions/InteractionService.cs b/src/Discord.Net.Interactions/InteractionService.cs index b7e92998b..0f023d1dc 100644 --- a/src/Discord.Net.Interactions/InteractionService.cs +++ b/src/Discord.Net.Interactions/InteractionService.cs @@ -914,8 +914,23 @@ namespace Discord.Interactions /// /// A task representing the conversion process. The task result contains the result of the conversion. /// - public Task SerializeValue(T obj, IServiceProvider services = null) => - _typeReaderMap.Get(typeof(T), services).SerializeAsync(obj); + public Task SerializeValueAsync(T obj, IServiceProvider services) => + _typeReaderMap.Get(typeof(T), services).SerializeAsync(obj, services); + + public async Task 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); + } /// /// Loads and caches an for the provided . diff --git a/src/Discord.Net.Interactions/TypeReaders/TypeReader.cs b/src/Discord.Net.Interactions/TypeReaders/TypeReader.cs index c8420d556..e518e0208 100644 --- a/src/Discord.Net.Interactions/TypeReaders/TypeReader.cs +++ b/src/Discord.Net.Interactions/TypeReaders/TypeReader.cs @@ -33,7 +33,7 @@ namespace Discord.Interactions /// /// A task representing the conversion process. The result of the task contains the conversion result. /// - public virtual Task SerializeAsync(object obj) => Task.FromResult(obj.ToString()); + public virtual Task SerializeAsync(object obj, IServiceProvider services) => Task.FromResult(obj.ToString()); } ///