Пример #1
0
        public async Task ModifyMessageAsync(LocalInteractionResponse response, IRestRequestOptions options = null)
        {
            response.Type = InteractionResponseType.MessageUpdate;
            var client = Interaction.GetRestClient();
            await client.CreateInteractionResponseAsync(Interaction.Id, Interaction.Token, response, options).ConfigureAwait(false);

            SetResponded(InteractionResponseType.MessageUpdate);
        }
Пример #2
0
        public async Task ModifyMessageAsync(
            LocalInteractionResponse response,
            IRestRequestOptions options = null, CancellationToken cancellationToken = default)
        {
            Guard.IsNotNull(response);

            response.Type = InteractionResponseType.MessageUpdate;

            var client = Interaction.GetRestClient();
            await client.CreateInteractionResponseAsync(Interaction.Id, Interaction.Token, response, options, cancellationToken).ConfigureAwait(false);

            SetResponded(InteractionResponseType.MessageUpdate);
        }
Пример #3
0
        public async Task PongAsync(
            IRestRequestOptions options = null, CancellationToken cancellationToken = default)
        {
            if (Interaction.Type != InteractionType.Ping)
            {
                Throw.InvalidOperationException("The interaction type must be a ping to pong it.");
            }

            ThrowIfResponded();

            var client   = Interaction.GetRestClient();
            var response = new LocalInteractionResponse(InteractionResponseType.Pong);
            await client.CreateInteractionResponseAsync(Interaction.Id, Interaction.Token, response, options, cancellationToken : cancellationToken).ConfigureAwait(false);

            SetResponded(InteractionResponseType.Pong);
        }
Пример #4
0
        public async Task DeferAsync(
            bool deferViaMessageUpdate, bool isEphemeral = false,
            IRestRequestOptions options = null, CancellationToken cancellationToken = default)
        {
            ThrowIfResponded();

            var client       = Interaction.GetRestClient();
            var responseType = deferViaMessageUpdate
                ? InteractionResponseType.DeferredMessageUpdate
                : InteractionResponseType.DeferredChannelMessage;

            var response = new LocalInteractionResponse(responseType)
                           .WithIsEphemeral(isEphemeral);

            await client.CreateInteractionResponseAsync(Interaction.Id, Interaction.Token, response, options, cancellationToken).ConfigureAwait(false);

            SetResponded(responseType);
        }
Пример #5
0
        public async Task DeferAsync(
            bool isEphemeral            = false,
            IRestRequestOptions options = null, CancellationToken cancellationToken = default)
        {
            if (Interaction is IComponentInteraction)
            {
                await DeferAsync(true, isEphemeral, options, cancellationToken).ConfigureAwait(false);

                return;
            }

            ThrowIfResponded();
            var client   = Interaction.GetRestClient();
            var response = new LocalInteractionResponse(InteractionResponseType.DeferredChannelMessage)
                           .WithIsEphemeral(isEphemeral);

            await client.CreateInteractionResponseAsync(Interaction.Id, Interaction.Token, response, options, cancellationToken).ConfigureAwait(false);

            SetResponded(InteractionResponseType.DeferredChannelMessage);
        }
        public static Task CreateInteractionResponseAsync(this IRestClient client, Snowflake interactionId, string interactionToken, LocalInteractionResponse response, IRestRequestOptions options = null)
        {
            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            response.Validate();
            var messageContent = new CreateInitialInteractionResponseJsonRestRequestContent
            {
                Type = response.Type
            };

            if (messageContent.Type != InteractionResponseType.Pong)
            {
                if (messageContent.Type is not(InteractionResponseType.MessageUpdate or InteractionResponseType.DeferredMessageUpdate))
                {
                    messageContent.Data = new InteractionCallbackDataJsonModel
                    {
                        Tts             = Optional.Conditional(response.IsTextToSpeech, true),
                        Content         = Optional.FromNullable(response.Content),
                        Embeds          = Optional.Conditional(response.Embeds.Count != 0, x => x.Select(x => x.ToModel()).ToArray(), response.Embeds),
                        AllowedMentions = Optional.FromNullable(response.AllowedMentions.ToModel()),
                        Components      = Optional.Conditional(response.Components.Count != 0, x => x.Select(x => x.ToModel()).ToArray(), response.Components),
                        Flags           = Optional.Conditional(response.Flags != InteractionResponseFlag.None, response.Flags)
                    };
                }
                else
                {
                    // TODO: make properties properly optional via different LocalInteractionResponse types?
                    messageContent.Data = new InteractionCallbackDataJsonModel
                    {
                        Content         = response.Content,
                        Embeds          = response.Embeds.Select(x => x.ToModel()).ToArray(),
                        AllowedMentions = response.AllowedMentions?.ToModel(),
                        Components      = response.Components.Select(x => x.ToModel()).ToArray(),
                        Flags           = response.Flags
                    };
                }
            }