Using RespondWithModalAsync<IModal>() without prior IModal declaration (#2369)
* add RespondWithModalAsync method for initializing missing ModalInfos on runtime * update method name and add inline docs
This commit is contained in:
@@ -19,6 +19,33 @@ namespace Discord.Interactions
|
|||||||
if (!ModalUtils.TryGet<T>(out var modalInfo))
|
if (!ModalUtils.TryGet<T>(out var modalInfo))
|
||||||
throw new ArgumentException($"{typeof(T).FullName} isn't referenced by any registered Modal Interaction Command and doesn't have a cached {typeof(ModalInfo)}");
|
throw new ArgumentException($"{typeof(T).FullName} isn't referenced by any registered Modal Interaction Command and doesn't have a cached {typeof(ModalInfo)}");
|
||||||
|
|
||||||
|
await SendModalResponseAsync(interaction, customId, modalInfo, options, modifyModal);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Respond to an interaction with a <see cref="IModal"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// This method overload uses the <paramref name="interactionService"/> parameter to create a new <see cref="ModalInfo"/>
|
||||||
|
/// if there isn't a built one already in cache.
|
||||||
|
/// </remarks>
|
||||||
|
/// <typeparam name="T">Type of the <see cref="IModal"/> implementation.</typeparam>
|
||||||
|
/// <param name="interaction">The interaction to respond to.</param>
|
||||||
|
/// <param name="interactionService">Interaction service instance that should be used to build <see cref="ModalInfo"/>s.</param>
|
||||||
|
/// <param name="options">The request options for this <see langword="async"/> request.</param>
|
||||||
|
/// <param name="modifyModal">Delegate that can be used to modify the modal.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static async Task RespondWithModalAsync<T>(this IDiscordInteraction interaction, string customId, InteractionService interactionService,
|
||||||
|
RequestOptions options = null, Action<ModalBuilder> modifyModal = null)
|
||||||
|
where T : class, IModal
|
||||||
|
{
|
||||||
|
var modalInfo = ModalUtils.GetOrAdd<T>(interactionService);
|
||||||
|
|
||||||
|
await SendModalResponseAsync(interaction, customId, modalInfo, options, modifyModal);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task SendModalResponseAsync(IDiscordInteraction interaction, string customId, ModalInfo modalInfo, RequestOptions options = null, Action<ModalBuilder> modifyModal = null)
|
||||||
|
{
|
||||||
var builder = new ModalBuilder(modalInfo.Title, customId);
|
var builder = new ModalBuilder(modalInfo.Title, customId);
|
||||||
|
|
||||||
foreach (var input in modalInfo.Components)
|
foreach (var input in modalInfo.Components)
|
||||||
|
|||||||
Reference in New Issue
Block a user