Esempio n. 1
0
        public async IAsyncEnumerable <DiscordMessagePacket> GetMessagesAsync(
            ulong channelId,
            int amount = 100)
        {
            for (var i = amount - 1; i >= 0; i -= 100)
            {
                var qs = new QueryString();

                qs.Add("limit", amount);

                var response = await RestClient.GetAsync(DiscordApiRoutes.ChannelMessages(channelId) + qs.Query);

                HandleErrors(response);

                var messages = JsonHelper.Deserialize <DiscordMessagePacket[]>(response.Body);

                foreach (var message in messages)
                {
                    yield return(message);
                }

                if (messages.Length < 100)
                {
                    break;
                }
            }
        }
Esempio n. 2
0
        public async ValueTask <DiscordMessagePacket> SendFileAsync(
            ulong channelId,
            Stream stream,
            string fileName,
            MessageArgs args)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            args.Embed = new DiscordEmbed
            {
                Image = new EmbedImage
                {
                    Url = "attachment://" + fileName
                }
            };

            List <MultiformItem> items = new List <MultiformItem>();

            if (!string.IsNullOrEmpty(args.Content))
            {
                var content = new StringContent(args.Content);
                items.Add(new MultiformItem {
                    Name = "content", Content = content
                });
            }

            if (stream.CanSeek)
            {
                var memoryStream = new MemoryStream();
                await stream.CopyToAsync(memoryStream).ConfigureAwait(false);

                memoryStream.Position = 0;
                stream = memoryStream;
            }

            var image = new StreamContent(stream);

            items.Add(new MultiformItem {
                Name = "file", Content = image, FileName = fileName
            });
            image.Headers.Add("Content-Type", "image/png");
            image.Headers.Add("Content-Disposition", "form-data; name=\"file\"; filename=\"" + fileName + "\"");

            var response = await RestClient.PostMultipartAsync(
                DiscordApiRoutes.ChannelMessages(channelId),
                items.ToArray())
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordMessagePacket>(response.Body));
        }
Esempio n. 3
0
        public async ValueTask <DiscordMessagePacket> SendMessageAsync(ulong channelId, MessageArgs args)
        {
            if (args.Content == string.Empty)
            {
                args.Content = null;
            }

            var response = await RestClient.PostJsonAsync(
                DiscordApiRoutes.ChannelMessages(channelId), args)
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordMessagePacket>(response.Body));
        }