Exemplo 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;
                }
            }
        }
Exemplo n.º 2
0
        public async ValueTask <int?> PruneGuildMembersAsync(
            ulong guildId,
            int days,
            bool computePruneCount = false)
        {
            if (days <= 0)
            {
                throw new InvalidOperationException(
                          $"Parameter '{nameof(days)}' cannot be lower than 1.");
            }

            QueryString qs = new QueryString();

            qs.Add("days", days);
            qs.Add("compute_prune_count", computePruneCount);

            var response = await RestClient.PostAsync(
                DiscordApiRoutes.GuildPrune(
                    guildId) + qs.Query)
                           .ConfigureAwait(false);

            HandleErrors(response);
            if (computePruneCount)
            {
                return(JsonHelper.Deserialize <DiscordPruneObject>(response.Body).Pruned);
            }
            return(null);
        }
Exemplo n.º 3
0
        public async ValueTask <DiscordGuildMemberPacket[]> GetGuildMembersAsync(ulong guildId)
        {
            var   items  = new List <DiscordGuildMemberPacket>();
            ulong?lastId = null;

            while (true)
            {
                var url = DiscordApiRoutes.GuildMembers(guildId) + "?limit=1000";

                if (lastId.HasValue)
                {
                    url += "&after=" + lastId;
                }

                var response = await RestClient.GetAsync(url).ConfigureAwait(false);

                HandleErrors(response);
                var packet = JsonHelper.Deserialize <DiscordGuildMemberPacket[]>(response.Body);

                items.AddRange(packet);

                if (packet.Length < 1000)
                {
                    break;
                }

                lastId = packet.Last().User.Id;
            }

            return(items.ToArray());
        }
Exemplo n.º 4
0
        public async ValueTask TriggerTypingAsync(ulong channelId)
        {
            var response = await RestClient.PostAsync(
                DiscordApiRoutes.ChannelTyping(channelId))
                           .ConfigureAwait(false);

            HandleErrors(response);
        }
Exemplo n.º 5
0
        public async ValueTask <DiscordRolePacket[]> GetRolesAsync(ulong guildId)
        {
            var response = await RestClient.GetAsync(
                DiscordApiRoutes.GuildRoles(guildId))
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordRolePacket[]>(response.Body));
        }
Exemplo n.º 6
0
        public async ValueTask <DiscordUserPacket> GetUserAsync(ulong userId)
        {
            var response = await RestClient.GetAsync(
                DiscordApiRoutes.User(userId))
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordUserPacket>(response.Body));
        }
Exemplo n.º 7
0
        public async ValueTask <DiscordChannelPacket[]> GetDMChannelsAsync()
        {
            var response = await RestClient.GetAsync(
                DiscordApiRoutes.UserMeChannels())
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordChannelPacket[]>(response.Body));
        }
Exemplo n.º 8
0
        public async ValueTask DeleteGuildAsync(
            ulong guildId)
        {
            var response = await RestClient.DeleteAsync(
                DiscordApiRoutes.Guild(guildId))
                           .ConfigureAwait(false);

            HandleErrors(response);
        }
Exemplo n.º 9
0
        public async ValueTask DeleteChannelAsync(
            ulong channelId)
        {
            var response = await RestClient.DeleteAsync(
                DiscordApiRoutes.Channel(channelId))
                           .ConfigureAwait(false);

            HandleErrors(response);
        }
Exemplo n.º 10
0
        public async ValueTask <GatewayConnectionPacket> GetGatewayBotAsync()
        {
            var response = await RestClient.GetAsync(
                DiscordApiRoutes.BotGateway())
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <GatewayConnectionPacket>(response.Body));
        }
Exemplo n.º 11
0
        public async ValueTask <DiscordChannelPacket> GetChannelAsync(
            ulong channelId)
        {
            var response = await RestClient.GetAsync(
                DiscordApiRoutes.Channel(channelId))
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordChannelPacket>(response.Body));
        }
Exemplo n.º 12
0
        public async ValueTask DeleteReactionsAsync(
            ulong channelId,
            ulong messageId)
        {
            var response = await RestClient.DeleteAsync(
                DiscordApiRoutes.MessageReactions(channelId, messageId))
                           .ConfigureAwait(false);

            HandleErrors(response);
        }
Exemplo n.º 13
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));
        }
Exemplo n.º 14
0
        /// <summary>
        /// Adds a role to a guild member.
        /// </summary>
        /// <param name="guildId"></param>
        /// <param name="userId"></param>
        /// <param name="roleId"></param>
        public async ValueTask AddGuildMemberRoleAsync(
            ulong guildId,
            ulong userId,
            ulong roleId)
        {
            var response = await RestClient.PutAsync(
                DiscordApiRoutes.GuildMemberRole(guildId, userId, roleId))
                           .ConfigureAwait(false);

            HandleErrors(response);
        }
Exemplo n.º 15
0
        public async ValueTask DeleteMessagesAsync(
            ulong channelId,
            params ulong[] messageId)
        {
            var response = await RestClient.PostJsonAsync(
                DiscordApiRoutes.ChannelBulkDeleteMessages(channelId),
                new ChannelBulkDeleteArgs(messageId))
                           .ConfigureAwait(false);

            HandleErrors(response);
        }
Exemplo n.º 16
0
        public async ValueTask CreateReactionAsync(
            ulong channelId,
            ulong messageId,
            DiscordEmoji emoji)
        {
            var response = await RestClient.PutAsync(
                DiscordApiRoutes.MessageReactionMe(channelId, messageId, emoji))
                           .ConfigureAwait(false);

            HandleErrors(response);
        }
Exemplo n.º 17
0
        public async ValueTask <DiscordEmoji> GetEmojiAsync(
            ulong guildId,
            ulong emojiId)
        {
            var response = await RestClient.GetAsync(
                DiscordApiRoutes.GuildEmoji(guildId, emojiId))
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordEmoji>(response.Body));
        }
Exemplo n.º 18
0
        /// <summary>
        /// Creates a Direct channel to a user.
        /// </summary>
        /// <param name="userId">Id of the user</param>
        public async ValueTask <DiscordChannelPacket> CreateDMChannelAsync(
            ulong userId)
        {
            var response = await RestClient.PostAsync(
                DiscordApiRoutes.UserMeChannels(),
                $"{{\"recipient_id\":{userId}}}")
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordChannelPacket>(response.Body));
        }
Exemplo n.º 19
0
        public async ValueTask ModifyGuildMemberAsync(
            ulong guildId,
            ulong userId,
            ModifyGuildMemberArgs packet)
        {
            var response = await RestClient.PatchJsonAsync(
                DiscordApiRoutes.GuildMember(guildId, userId),
                packet)
                           .ConfigureAwait(false);

            HandleErrors(response);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Creates a new role in the guild specified.
        /// </summary>
        /// <param name="guildId">The guild in which you want to create a role.</param>
        /// <param name="args">The properties of the role.</param>
        /// <returns>The role you've created, if successful</returns>
        public async ValueTask <DiscordRolePacket> CreateGuildRoleAsync(
            ulong guildId,
            CreateRoleArgs args)
        {
            var response = await RestClient.PostJsonAsync(
                DiscordApiRoutes.GuildRoles(guildId),
                args)
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordRolePacket>(response.Body));
        }
Exemplo n.º 21
0
        public async ValueTask <DiscordRolePacket> EditRoleAsync(
            ulong guildId,
            DiscordRolePacket role)
        {
            var response = await RestClient.PutJsonAsync(
                DiscordApiRoutes.GuildRole(guildId, role.Id),
                role)
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordRolePacket>(response.Body));
        }
Exemplo n.º 22
0
        public async ValueTask <DiscordUserPacket[]> GetReactionsAsync(
            ulong channelId,
            ulong messageId,
            DiscordEmoji emoji)
        {
            var response = await RestClient.GetAsync(
                DiscordApiRoutes.MessageReaction(channelId, messageId, emoji))
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordUserPacket[]>(response.Body));
        }
Exemplo n.º 23
0
        /// <summary>
        /// Creates and uploads a new emoji for a guild.
        /// </summary>
        /// <param name="guildId"></param>
        /// <param name="args"></param>
        /// <returns>The created emoji.</returns>
        public async ValueTask <DiscordEmoji> CreateEmojiAsync(
            ulong guildId,
            EmojiCreationArgs args)
        {
            var response = await RestClient.PostJsonAsync(
                DiscordApiRoutes.GuildEmoji(guildId),
                args)
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordEmoji>(response.Body));
        }
Exemplo n.º 24
0
        public async ValueTask <DiscordMessagePacket> EditMessageAsync(
            ulong channelId,
            ulong messageId,
            EditMessageArgs args)
        {
            var response = await RestClient.PatchJsonAsync(
                DiscordApiRoutes.ChannelMessage(channelId, messageId),
                args)
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordMessagePacket>(response.Body));
        }
Exemplo n.º 25
0
        public async ValueTask ModifySelfAsync(UserModifyArgs args)
        {
            if (args.Avatar.Type == ImageType.WEBP)
            {
                throw new InvalidDataException("Can't upload WEBP images.");
            }

            var response = await RestClient.PatchJsonAsync(
                DiscordApiRoutes.UserMe(),
                args)
                           .ConfigureAwait(false);

            HandleErrors(response);
        }
Exemplo n.º 26
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));
        }
Exemplo n.º 27
0
        public async ValueTask KickGuildMemberAsync(
            ulong guildId,
            ulong userId,
            string reason = null)
        {
            QueryString qs = new QueryString();

            if (!string.IsNullOrWhiteSpace(reason))
            {
                qs.Add("reason", reason);
            }

            var response = await RestClient.DeleteAsync(
                DiscordApiRoutes.GuildMember(guildId, userId) + qs.Query)
                           .ConfigureAwait(false);

            HandleErrors(response);
        }
Exemplo n.º 28
0
        public async ValueTask <int> GetPruneCountAsync(
            ulong guildId,
            int days)
        {
            if (days <= 0)
            {
                throw new InvalidOperationException(
                          $"Parameter '{nameof(days)}' cannot be lower than 1.");
            }

            QueryString qs = new QueryString();

            qs.Add("days", days);

            var response = await RestClient.GetAsync(
                DiscordApiRoutes.GuildPrune(guildId) + qs.Query)
                           .ConfigureAwait(false);

            HandleErrors(response);
            return(JsonHelper.Deserialize <DiscordPruneObject>(response.Body).Pruned);
        }
Exemplo n.º 29
0
        /// <summary>
        /// Adds a user to a guild's ban list.
        /// </summary>
        /// <param name="guildId">Id of the guild you want to ban a user from</param>
        /// <param name="userId">Id of the user you want to ban</param>
        /// <param name="pruneDays">Amount of days you want to prune messages from the user</param>
        /// <param name="reason">Reason for the ban</param>
        public async ValueTask AddGuildBanAsync(
            ulong guildId,
            ulong userId,
            int pruneDays = 7,
            string reason = null)
        {
            QueryString qs = new QueryString();

            if (!string.IsNullOrWhiteSpace(reason))
            {
                qs.Add("reason", reason);
            }

            if (pruneDays != 0)
            {
                qs.Add("delete-message-days", pruneDays);
            }

            var response = await RestClient.PutAsync(DiscordApiRoutes.GuildBan(guildId, userId) + qs.Query)
                           .ConfigureAwait(false);

            HandleErrors(response);
        }