Ejemplo n.º 1
0
        /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
        public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
            Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler)
        {
            Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
            Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

            // check that user flag and user Id list are exclusive, same with role flag and role Id list
            if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
            {
                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                    allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                {
                    throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                }

                if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                    allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                {
                    throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                }
            }

            var args = new UploadFileParams(stream) { Filename = filename, Content = text, IsTTS = isTTS, Embed = embed?.ToModel() ?? Optional<API.Embed>.Unspecified, AllowedMentions = allowedMentions?.ToModel() ?? Optional<API.AllowedMentions>.Unspecified, IsSpoiler = isSpoiler };
            var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false);
            return RestUserMessage.Create(client, channel, client.CurrentUser, model);
        }
Ejemplo n.º 2
0
        static void UploadFiles()
        {
            //string url = "http://*****:*****@"E:\Harbour\150426001201-1.jpg", @"E:\Harbour\150426001201-4.jpg" };
            foreach (string fileUrl in filePath)
            {
                FileInfo         fileInfo         = new FileInfo(fileUrl);
                UploadFileParams uploadFileParams = new UploadFileParams();
                uploadFileParams.FileName   = fileInfo.Name;
                uploadFileParams.FileStream = fileInfo.OpenRead();

                httpParam.UploadFiles.Add(uploadFileParams);
            }

            var sdfs = HttpUtils.Post(httpParam);
        }
Ejemplo n.º 3
0
        public async Task <IUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS)
        {
            var args = new UploadFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS
            };
            var model = await Discord.ApiClient.UploadDMFileAsync(Id, args).ConfigureAwait(false);

            return(CreateOutgoingMessage(model));
        }
Ejemplo n.º 4
0
        /// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
        public static async Task <RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client,
                                                                 Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options)
        {
            var args = new UploadFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS, Embed = embed != null?embed.ToModel() : Optional <API.Embed> .Unspecified
            };
            var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false);

            return(RestUserMessage.Create(client, channel, client.CurrentUser, model));
        }
Ejemplo n.º 5
0
        public static async Task <RestUserMessage> SendFileAsync(IChannel channel, BaseDiscordClient client,
                                                                 Stream stream, string filename, string text, bool isTTS, IGuild guild, RequestOptions options)
        {
            var args = new UploadFileParams(stream)
            {
                Filename = filename, Content = text, IsTTS = isTTS
            };
            var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false);

            return(RestUserMessage.Create(client, guild, model));
        }
Ejemplo n.º 6
0
        public async Task <IUserMessage> SendFileAsync(string filePath, string text, bool isTTS)
        {
            string filename = Path.GetFileName(filePath);

            using (var file = File.OpenRead(filePath))
            {
                var args = new UploadFileParams(file)
                {
                    Filename = filename, Content = text, IsTTS = isTTS
                };
                var model = await Discord.ApiClient.UploadDMFileAsync(Id, args).ConfigureAwait(false);

                return(CreateOutgoingMessage(model));
            }
        }
Ejemplo n.º 7
0
 public Upload UploadFile(int roomId, UploadFileParams uploadFileParams, IObserver <ProgressState> progressObserver)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 8
0
        public static async Task <Model> ModifyAsync(ulong channelId, ulong msgId, BaseDiscordClient client, Action <MessageProperties> func,
                                                     RequestOptions options)
        {
            var args = new MessageProperties();

            func(args);

            var embed  = args.Embed;
            var embeds = args.Embeds;

            bool hasText        = args.Content.IsSpecified && string.IsNullOrEmpty(args.Content.Value);
            bool hasEmbeds      = embed.IsSpecified && embed.Value != null || embeds.IsSpecified && embeds.Value?.Length > 0;
            bool hasComponents  = args.Components.IsSpecified && args.Components.Value != null;
            bool hasAttachments = args.Attachments.IsSpecified;
            bool hasFlags       = args.Flags.IsSpecified;

            // No content needed if modifying flags
            if ((!hasComponents && !hasText && !hasEmbeds && !hasAttachments) && !hasFlags)
            {
                Preconditions.NotNullOrEmpty(args.Content.IsSpecified ? args.Content.Value : string.Empty, nameof(args.Content));
            }

            if (args.AllowedMentions.IsSpecified)
            {
                AllowedMentions allowedMentions = args.AllowedMentions.Value;
                Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
                Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");

                // check that user flag and user Id list are exclusive, same with role flag and role Id list
                if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
                {
                    if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
                        allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
                    {
                        throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
                    }

                    if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
                        allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
                    {
                        throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
                    }
                }
            }

            var apiEmbeds = embed.IsSpecified || embeds.IsSpecified ? new List <API.Embed>() : null;

            if (embed.IsSpecified && embed.Value != null)
            {
                apiEmbeds.Add(embed.Value.ToModel());
            }

            if (embeds.IsSpecified && embeds.Value != null)
            {
                apiEmbeds.AddRange(embeds.Value.Select(x => x.ToModel()));
            }

            Preconditions.AtMost(apiEmbeds?.Count ?? 0, 10, nameof(args.Embeds), "A max of 10 embeds are allowed.");

            if (!args.Attachments.IsSpecified)
            {
                var apiArgs = new API.Rest.ModifyMessageParams
                {
                    Content         = args.Content,
                    Embeds          = apiEmbeds?.ToArray() ?? Optional <API.Embed[]> .Unspecified,
                    Flags           = args.Flags.IsSpecified ? args.Flags.Value : Optional.Create <MessageFlags?>(),
                    AllowedMentions = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value.ToModel() : Optional.Create <API.AllowedMentions>(),
                    Components      = args.Components.IsSpecified ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty <API.ActionRowComponent>() : Optional <API.ActionRowComponent[]> .Unspecified,
                };
                return(await client.ApiClient.ModifyMessageAsync(channelId, msgId, apiArgs, options).ConfigureAwait(false));
            }
            else
            {
                var apiArgs = new UploadFileParams(args.Attachments.Value.ToArray())
                {
                    Content          = args.Content,
                    Embeds           = apiEmbeds?.ToArray() ?? Optional <API.Embed[]> .Unspecified,
                    Flags            = args.Flags.IsSpecified ? args.Flags.Value : Optional.Create <MessageFlags?>(),
                    AllowedMentions  = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value.ToModel() : Optional.Create <API.AllowedMentions>(),
                    MessageComponent = args.Components.IsSpecified ? args.Components.Value?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Array.Empty <API.ActionRowComponent>() : Optional <API.ActionRowComponent[]> .Unspecified
                };

                return(await client.ApiClient.ModifyMessageAsync(channelId, msgId, apiArgs, options).ConfigureAwait(false));
            }
        }