public static async Task <IUserMessage> CreateInteractionFollowupAsync(this IRestClient client,
                                                                               Snowflake applicationId, string interactionToken, LocalInteractionFollowup followup,
                                                                               IRestRequestOptions options = null, CancellationToken cancellationToken = default)
        {
            Guard.IsNotNull(followup);

            followup.Validate();
            var messageContent = new CreateFollowupMessageJsonRestRequestContent
            {
                Content         = Optional.FromNullable(followup.Content),
                Tts             = Optional.Conditional(followup.IsTextToSpeech, true),
                Embeds          = Optional.Conditional(followup.Embeds.Count != 0, x => x.Select(x => x.ToModel()).ToArray(), followup.Embeds),
                AllowedMentions = Optional.FromNullable(followup.AllowedMentions.ToModel()),
                Components      = Optional.Conditional(followup.Components.Count != 0, x => x.Select(x => x.ToModel()).ToArray(), followup.Components),
                Flags           = followup.Flags
            };

            Task <MessageJsonModel> task;

            if (followup.Attachments.Count != 0)
            {
                // If there are attachments, we must send them via multipart HTTP content.
                // Our `messageContent` will be serialized into a "payload_json" form data field.
                var content = new MultipartJsonPayloadRestRequestContent <CreateFollowupMessageJsonRestRequestContent>(messageContent, followup.Attachments);
                task = client.ApiClient.CreateFollowupInteractionResponseAsync(applicationId, interactionToken, content, options, cancellationToken);
            }
            else
            {
                task = client.ApiClient.CreateFollowupInteractionResponseAsync(applicationId, interactionToken, messageContent, options, cancellationToken);
            }

            var model = await task.ConfigureAwait(false);

            return(new TransientUserMessage(client, model));
        }
Exemplo n.º 2
0
        public static async Task <IThreadChannel> CreatePrivateThreadAsync(this IRestClient client, Snowflake channelId, string name, TimeSpan?automaticArchiveDuration = null, IRestRequestOptions options = null)
        {
            var content = new CreateThreadJsonRestRequestContent
            {
                Name = name,
                AutoArchiveDuration = Optional.Conditional(automaticArchiveDuration != null, duration => (int)duration.Value.TotalMinutes, automaticArchiveDuration),
                Type = ChannelType.PrivateThread
            };
            var model = await client.ApiClient.CreateThreadAsync(channelId, content, null, options).ConfigureAwait(false);

            return(new TransientThreadChannel(client, model));
        }
Exemplo n.º 3
0
        public static CreateInitialInteractionResponseJsonRestRequestContent ToContent(this ILocalInteractionResponse response,
                                                                                       IJsonSerializer serializer, out IList <LocalAttachment> attachments)
        {
            Guard.IsNotNull(response);

            var content = new CreateInitialInteractionResponseJsonRestRequestContent
            {
                Type = response.Type
            };

            attachments = Array.Empty <LocalAttachment>();

            if (response is LocalInteractionAutoCompleteResponse autoCompleteResponse)
            {
                content.Data = new InteractionCallbackAutoCompleteDataJsonModel
                {
                    Choices = Optional.Convert(autoCompleteResponse.Choices, choices => choices?.Select(choice => choice.ToModel(serializer)).ToArray())
                };
            }
            else if (response is LocalInteractionMessageResponse messageResponse)
            {
                if (content.Type is not InteractionResponseType.MessageUpdate)
                {
                    messageResponse.Validate();
                    content.Data = new InteractionCallbackMessageDataJsonModel
                    {
                        Tts     = Optional.Conditional(messageResponse.IsTextToSpeech, true),
                        Content = Optional.FromNullable(messageResponse.Content),
                        Embeds  = Optional.Conditional(messageResponse.Embeds.Count != 0,
                                                       x => x.Select(x => x.ToModel()).ToArray(), messageResponse.Embeds),
                        AllowedMentions = Optional.FromNullable(messageResponse.AllowedMentions.ToModel()),
                        Components      = Optional.Conditional(messageResponse.Components.Count != 0,
                                                               x => x.Select(x => x.ToModel()).ToArray(), messageResponse.Components),
                        Flags = Optional.Conditional(messageResponse.Flags != MessageFlag.None,
                                                     messageResponse.Flags)
                    };
                }
                else
                {
                    // TODO: make properties properly optional via different LocalInteractionResponse types?
                    content.Data = new InteractionCallbackMessageDataJsonModel
                    {
                        //Tts = messageResponse.IsTextToSpeech,
                        Content         = messageResponse.Content,
                        Embeds          = messageResponse.Embeds.Select(x => x.ToModel()).ToArray(),
                        AllowedMentions = messageResponse.AllowedMentions?.ToModel(),
                        Components      = messageResponse.Components.Select(x => x.ToModel()).ToArray(),
                        Flags           = messageResponse.Flags
                    };
                }
                attachments = messageResponse.Attachments;
            }
Exemplo n.º 4
0
        public override ValueTask <PresenceUpdatedEventArgs> HandleDispatchAsync(IGatewayApiClient shard, PresenceJsonModel model)
        {
            if (model.GuildId == default) // just in case?
            {
                return(new(result : null));
            }

            CachedPresence oldPresence = null;
            IPresence      newPresence = null;

            if (CacheProvider.TryGetPresences(model.GuildId, out var cache))
            {
                if (model.Status != UserStatus.Offline)
                {
                    if (cache.TryGetValue(model.User.Id, out var presence))
                    {
                        newPresence = presence;
                        oldPresence = presence.Clone() as CachedPresence;
                        newPresence.Update(model);
                    }
                    else
                    {
                        newPresence = new CachedPresence(Client, model);
                        cache.Add(model.User.Id, newPresence as CachedPresence);
                    }
                }
                else
                {
                    cache.TryRemove(model.User.Id, out oldPresence);
                }
            }

            newPresence ??= new TransientPresence(Client, model);

            var user = Optional.Conditional(model.User.Username != null, (tuple) =>
            {
                var(client, model) = tuple;
                return(new TransientUser(client, model) as IUser);
            }, (Client, model.User));

            var e = new PresenceUpdatedEventArgs(model.GuildId, user, oldPresence, newPresence);

            return(new(e));
        }
        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
                    };
                }
            }
        public static async Task <IUserMessage> SendMessageAsync(this IRestClient client,
                                                                 Snowflake channelId, LocalMessage message,
                                                                 IRestRequestOptions options = null, CancellationToken cancellationToken = default)
        {
            Guard.IsNotNull(message);

            message.Validate();
            var messageContent = new CreateMessageJsonRestRequestContent
            {
                Content          = Optional.FromNullable(message.Content),
                Tts              = Optional.Conditional(message.IsTextToSpeech, true),
                Embeds           = Optional.Conditional(message.Embeds.Count != 0, x => x.Select(x => x.ToModel()).ToArray(), message.Embeds),
                Flags            = Optional.Conditional(message.Flags != 0, message.Flags),
                AllowedMentions  = Optional.FromNullable(message.AllowedMentions.ToModel()),
                MessageReference = Optional.FromNullable(message.Reference.ToModel()),
                Nonce            = Optional.FromNullable(message.Nonce),
                Components       = Optional.Conditional(message.Components.Count != 0, x => x.Select(x => x.ToModel()).ToArray(), message.Components),
                StickerIds       = Optional.Conditional(message.StickerIds.Count != 0, x => x.ToArray(), message.StickerIds)
            };

            Task <MessageJsonModel> task;

            if (message.Attachments.Count != 0)
            {
                // If there are attachments, we must send them via multipart HTTP content.
                // Our `messageContent` will be serialized into a "payload_json" form data field.
                var content = new MultipartJsonPayloadRestRequestContent <CreateMessageJsonRestRequestContent>(messageContent, message.Attachments);
                task = client.ApiClient.CreateMessageAsync(channelId, content, options, cancellationToken);
            }
            else
            {
                task = client.ApiClient.CreateMessageAsync(channelId, messageContent, options, cancellationToken);
            }

            var model = await task.ConfigureAwait(false);

            return(new TransientUserMessage(client, model));
        }
Exemplo n.º 7
0
        public static ComponentJsonModel ToModel(this LocalComponent component)
        {
            if (component == null)
            {
                return(null);
            }

            var model = new ComponentJsonModel();

            if (component is LocalRowComponent rowComponent)
            {
                model.Type       = ComponentType.Row;
                model.Components = rowComponent.Components.Select(x => x.ToModel()).ToArray();
            }
            else if (component is LocalNestedComponent nestedComponent)
            {
                model.Disabled = Optional.Conditional(nestedComponent.IsDisabled, true);
                if (component is ILocalInteractiveComponent interactiveComponent)
                {
                    model.CustomId = interactiveComponent.CustomId;
                }

                if (component is LocalButtonComponentBase buttonComponentBase)
                {
                    model.Type  = ComponentType.Button;
                    model.Label = Optional.FromNullable(buttonComponentBase.Label);
                    model.Emoji = Optional.FromNullable(buttonComponentBase.Emoji.ToModel());

                    if (buttonComponentBase is LocalButtonComponent buttonComponent)
                    {
                        model.Style = (ButtonComponentStyle)buttonComponent.Style;
                    }
                    else if (buttonComponentBase is LocalLinkButtonComponent linkButtonComponent)
                    {
                        model.Style = ButtonComponentStyle.Link;
                        model.Url   = linkButtonComponent.Url;
                    }
                    else
                    {
                        throw new InvalidOperationException("Unknown local button component type.");
                    }
                }
                else if (component is LocalSelectionComponent selectionComponent)
                {
                    model.Type        = ComponentType.Selection;
                    model.Placeholder = Optional.FromNullable(selectionComponent.Placeholder);
                    model.MinValues   = Optional.FromNullable(selectionComponent.MinimumSelectedOptions);
                    model.MaxValues   = Optional.FromNullable(selectionComponent.MaximumSelectedOptions);
                    model.Options     = selectionComponent.Options.Select(x => x.ToModel()).ToArray();
                }
                else
                {
                    throw new InvalidOperationException("Unknown local nested component type.");
                }
            }
            else
            {
                throw new InvalidOperationException("Unknown local component type.");
            }

            return(model);
        }