/// <inheritdoc />
        public virtual async Task <Result <IGuild> > CreateGuildFromTemplateAsync
        (
            string templateCode,
            string name,
            Optional <Stream> icon = default,
            CancellationToken ct   = default
        )
        {
            var packIcon = await ImagePacker.PackImageAsync(new Optional <Stream?>(icon.Value), ct);

            if (!packIcon.IsSuccess)
            {
                return(Result <IGuild> .FromError(new GenericError("Failed to pack icon."), packIcon));
            }

            var iconData = packIcon.Entity;

            return(await _discordHttpClient.PostAsync <IGuild>
                   (
                       $"guilds/templates/{templateCode}",
                       b => b.WithJson
                       (
                           j =>
            {
                j.WriteString("name", name);
                j.Write("icon", iconData);
            }
                       ),
                       ct : ct
                   ));
        }
예제 #2
0
        /// <inheritdoc />
        public virtual async Task <Result <IGuild> > CreateGuildAsync
        (
            string name,
            Optional <Stream> icon = default,
            Optional <VerificationLevel> verificationLevel = default,
            Optional <MessageNotificationLevel> defaultMessageNotifications = default,
            Optional <ExplicitContentFilterLevel> explicitContentFilter     = default,
            Optional <IReadOnlyList <IRole> > roles = default,
            Optional <IReadOnlyList <IPartialChannel> > channels = default,
            Optional <Snowflake> afkChannelID                = default,
            Optional <TimeSpan> afkTimeout                   = default,
            Optional <Snowflake> systemChannelID             = default,
            Optional <SystemChannelFlags> systemChannelFlags = default,
            CancellationToken ct = default
        )
        {
            if (name.Length < 2 || name.Length > 100)
            {
                return(new GenericError("The name must be between 2 and 100 characters."));
            }

            await using var memoryStream = new MemoryStream();

            var packIcon = await ImagePacker.PackImageAsync(new Optional <Stream?>(icon.Value), ct);

            if (!packIcon.IsSuccess)
            {
                return(Result <IGuild> .FromError(new GenericError("Failed to pack icon."), packIcon));
            }

            var iconData = packIcon.Entity;

            return(await _discordHttpClient.PostAsync <IGuild>
                   (
                       "guilds",
                       b => b.WithJson
                       (
                           json =>
            {
                json.WriteString("name", name);
                json.Write("icon", iconData, _jsonOptions);
                json.Write("verification_level", verificationLevel, _jsonOptions);
                json.Write("default_message_notifications", defaultMessageNotifications, _jsonOptions);
                json.Write("explicit_content_filter", explicitContentFilter, _jsonOptions);
                json.Write("roles", roles, _jsonOptions);
                json.Write("channels", channels, _jsonOptions);
                json.Write("afk_channel_id", afkChannelID, _jsonOptions);

                if (afkTimeout.HasValue)
                {
                    json.WriteNumber("afk_timeout", (ulong)afkTimeout.Value.TotalSeconds);
                }

                json.Write("system_channel_id", systemChannelID, _jsonOptions);
                json.Write("system_channel_flags", systemChannelFlags, _jsonOptions);
            }
                       ),
                       ct : ct
                   ));
        }
예제 #3
0
 public void SendMessage(Message message)
 {
     string        serializeObject = JsonConvert.SerializeObject(message);
     StringContent content         = new StringContent(serializeObject, Encoding.UTF8, "application/json");
     var           result          = DiscordHttpClient.PostAsync("channels/542319190825238528/messages", content).Result;
     var           resolved        = result.Content.ReadAsStringAsync().Result;
 }
        /// <inheritdoc />
        public virtual async Task <Result <IWebhook> > CreateWebhookAsync
        (
            Snowflake channelID,
            string name,
            Optional <Stream?> avatar,
            CancellationToken ct = default
        )
        {
            if (name.Length < 1 || name.Length > 80)
            {
                return(new GenericError("Names must be between 1 and 80 characters"));
            }

            if (name.Equals("clyde", StringComparison.InvariantCultureIgnoreCase))
            {
                return(new GenericError("Names cannot be \"clyde\"."));
            }

            var packAvatar = await ImagePacker.PackImageAsync(avatar, ct);

            if (!packAvatar.IsSuccess)
            {
                return(Result <IWebhook> .FromError(new GenericError("Failed to pack avatar."), packAvatar));
            }

            var avatarData = packAvatar.Entity;

            return(await _discordHttpClient.PostAsync <IWebhook>
                   (
                       $"channels/{channelID}/webhooks",
                       b => b.WithJson
                       (
                           json =>
            {
                json.WriteString("name", name);
                json.WriteString("avatar", avatarData.Value);
            }
                       ),
                       ct : ct
                   ));
        }
        /// <inheritdoc />
        public virtual async Task <Result <IApplicationCommand> > CreateGlobalApplicationCommandAsync
        (
            Snowflake applicationID,
            string name,
            string description,
            Optional <IReadOnlyList <IApplicationCommandOption> > options,
            CancellationToken ct
        )
        {
            if (name.Length is < 1 or > 32)
            {
                return(new GenericError
                       (
                           "The name must be between 1 and 32 characters."
                       ));
            }

            if (description.Length is < 1 or > 100)
            {
                return(new GenericError
                       (
                           "The description must be between 1 and 100 characters."
                       ));
            }

            return(await _discordHttpClient.PostAsync <IApplicationCommand>
                   (
                       $"applications/{applicationID}/commands",
                       b => b.WithJson
                       (
                           json =>
            {
                json.WriteString("name", name);
                json.WriteString("description", description);
                json.Write("options", options, _jsonOptions);
            }
                       ),
                       ct : ct
                   ));
        }
        /// <inheritdoc />
        public virtual Task <Result <IMessage> > CreateMessageAsync
        (
            Snowflake channelID,
            Optional <string> content = default,
            Optional <string> nonce   = default,
            Optional <bool> isTTS     = default,
            Optional <FileData> file  = default,
            Optional <IEmbed> embed   = default,
            Optional <IAllowedMentions> allowedMentions   = default,
            Optional <IMessageReference> messageReference = default,
            CancellationToken ct = default
        )
        {
            return(_discordHttpClient.PostAsync <IMessage>
                   (
                       $"channels/{channelID}/messages",
                       b =>
            {
                if (file.HasValue)
                {
                    b.AddContent(new StreamContent(file.Value.Content), "file", file.Value.Name);
                }

                b.WithJson
                (
                    json =>
                {
                    json.Write("content", content, _jsonOptions);
                    json.Write("nonce", nonce, _jsonOptions);
                    json.Write("tts", isTTS, _jsonOptions);
                    json.Write("embed", embed, _jsonOptions);
                    json.Write("allowed_mentions", allowedMentions, _jsonOptions);
                    json.Write("message_reference", messageReference, _jsonOptions);
                }
                );
            },
                       ct: ct
                   ));
        }
        /// <inheritdoc />
        public virtual async Task<Result<IMessage>> CreateMessageAsync
        (
            Snowflake channelID,
            Optional<string> content = default,
            Optional<string> nonce = default,
            Optional<bool> isTTS = default,
            Optional<FileData> file = default,
            Optional<IReadOnlyList<IEmbed>> embeds = default,
            Optional<IAllowedMentions> allowedMentions = default,
            Optional<IMessageReference> messageReference = default,
            Optional<IReadOnlyList<IMessageComponent>> components = default,
            CancellationToken ct = default
        )
        {
            if (nonce.HasValue && nonce.Value.Length > 25)
            {
                return new GenericError("The nonce length must be less than 25 characters.");
            }

            return await _discordHttpClient.PostAsync<IMessage>
            (
                $"channels/{channelID}/messages",
                b =>
                {
                    if (file.HasValue)
                    {
                        b.AddContent(new StreamContent(file.Value.Content), "file", file.Value.Name);
                    }

                    b.WithJson
                    (
                        json =>
                        {
                            json.Write("content", content, _jsonOptions);
                            json.Write("nonce", nonce, _jsonOptions);
                            json.Write("tts", isTTS, _jsonOptions);
                            json.Write("embeds", embeds, _jsonOptions);
                            json.Write("allowed_mentions", allowedMentions, _jsonOptions);
                            json.Write("message_reference", messageReference, _jsonOptions);
                            json.Write("components", components, _jsonOptions);
                        }
                    );
                },
                ct: ct
            );
        }
예제 #8
0
 /// <inheritdoc />
 public virtual Task <ICreateRestEntityResult <IChannel> > CreateDMAsync
 (
     Snowflake recipientID,
     CancellationToken ct = default
 )
 {
     return(_discordHttpClient.PostAsync <IChannel>
            (
                "users/@me/channels",
                b => b.WithJson
                (
                    json =>
     {
         json.WriteString("recipient_id", recipientID.ToString());
     }
                ),
                ct: ct
            ));
 }
예제 #9
0
        /// <inheritdoc />
        public virtual async Task <Result <IEmoji> > CreateGuildEmojiAsync
        (
            Snowflake guildID,
            string name,
            Stream image,
            IReadOnlyList <Snowflake> roles,
            CancellationToken ct = default
        )
        {
            if (image.Length > 256000)
            {
                return(new GenericError("Image too large."));
            }

            var packImage = await ImagePacker.PackImageAsync(image, ct);

            if (!packImage.IsSuccess)
            {
                return(Result <IEmoji> .FromError(new GenericError("Failed to pack emoji."), packImage));
            }

            var emojiData = packImage.Entity;

            return(await _discordHttpClient.PostAsync <IEmoji>
                   (
                       $"guilds/{guildID}/emojis",
                       b => b.WithJson
                       (
                           json =>
            {
                json.WriteString("name", name);
                json.WriteString("image", emojiData);

                json.WritePropertyName("roles");
                JsonSerializer.Serialize(json, roles, _jsonOptions);
            }
                       ),
                       ct : ct
                   ));
        }
예제 #10
0
 /// <inheritdoc />
 public virtual Task <ICreateRestEntityResult <IMessage> > CreateMessageAsync
 (
     Snowflake channelID,
     Optional <string> content = default,
     Optional <string> nonce   = default,
     Optional <bool> isTTS     = default,
     Optional <FileData> file  = default,
     Optional <IEmbed> embed   = default,
     Optional <IAllowedMentions> allowedMentions   = default,
     Optional <IMessageReference> messageReference = default,
     CancellationToken ct = default
 )
 {
     return(_discordHttpClient.PostAsync <IMessage>
            (
                $"channels/{channelID}/messages",
                b =>
     {
         if (file.HasValue)
         {
             b.AddContent(new StreamContent(file.Value !.Content), "file", file.Value !.Name);
         }
 /// <inheritdoc />
 public virtual Task <Result> CreateInteractionResponseAsync
 (
     Snowflake interactionID,
     string interactionToken,
     IInteractionResponse response,
     CancellationToken ct
 )
 {
     return(_discordHttpClient.PostAsync
            (
                $"interactions/{interactionID}/{interactionToken}/callback",
                b => b.WithJson
                (
                    json =>
     {
         JsonSerializer.Serialize(json, response, _jsonOptions);
     },
                    false
                ),
                ct
            ));
 }
 /// <inheritdoc />
 public Task <Result <IStageInstance> > CreateStageInstanceAsync
 (
     Snowflake channelID,
     string topic,
     Optional <StagePrivacyLevel> privacyLevel = default,
     CancellationToken ct = default
 )
 {
     return(_discordHttpClient.PostAsync <IStageInstance>
            (
                "stage-instances",
                r => r.WithJson
                (
                    json =>
     {
         json.WriteString("channel_id", channelID.ToString());
         json.WriteString("topic", topic);
         json.Write("privacy_level", privacyLevel);
     }
                ),
                ct: ct
            ));
 }