Beispiel #1
0
        /// <summary>
        /// Gets messages from a text channel.
        /// <para>Requires <see cref="DiscordPermission.ReadMessages"/>.</para>
        /// </summary>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <IReadOnlyList <DiscordMessage> > GetChannelMessages(Snowflake channelId,
                                                                               Snowflake?baseMessageId        = null, int?limit = null,
                                                                               MessageGetStrategy getStrategy = MessageGetStrategy.Before)
        {
            UrlParametersBuilder builder = new UrlParametersBuilder();

            if (baseMessageId.HasValue)
            {
                builder.Add(getStrategy.ToString().ToLower(), baseMessageId.Value.ToString());
            }
            if (limit.HasValue)
            {
                builder.Add("limit", limit.Value.ToString());
            }

            DiscordApiData data = await rest.Get($"channels/{channelId}/messages{builder.ToQueryString()}",
                                                 $"channels/{channelId}/messages").ConfigureAwait(false);

            DiscordMessage[] messages = new DiscordMessage[data.Values.Count];

            for (int i = 0; i < messages.Length; i++)
            {
                messages[i] = new DiscordMessage(this, data.Values[i]);
            }

            return(messages);
        }
        /// <summary>
        /// Gets a list of user guilds the current bot is in.
        /// </summary>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <DiscordUserGuild[]> GetCurrentUserGuilds(int?limit             = null,
                                                                    Snowflake?baseGuildId = null, GuildGetStrategy getStrategy = GuildGetStrategy.After)
        {
            UrlParametersBuilder paramBuilder = new UrlParametersBuilder();

            if (baseGuildId.HasValue)
            {
                paramBuilder.Add(getStrategy.ToString().ToLower(), baseGuildId.ToString());
            }

            if (limit.HasValue)
            {
                paramBuilder.Add("limit", limit.Value.ToString());
            }

            DiscordApiData data = await rest.Get($"users/@me/guilds{paramBuilder.ToQueryString()}",
                                                 "users/@me/guilds").ConfigureAwait(false);

            DiscordUserGuild[] guilds = new DiscordUserGuild[data.Values.Count];

            for (int i = 0; i < guilds.Length; i++)
            {
                guilds[i] = new DiscordUserGuild(data.Values[i]);
            }

            return(guilds);
        }
        /// <summary>
        /// Gets a paginated list of users who reacted to the specified message with the specified emoji.
        /// </summary>
        /// <param name="baseUserId">The user ID to start at when retrieving reactions.</param>
        /// <param name="limit">The maximum number of reactions to return or null to use the default.</param>
        /// <param name="getStrategy">The pagination strategy to use based on <paramref name="baseUserId"/>.</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <IReadOnlyList <DiscordUser> > GetReactions(Snowflake channelId, Snowflake messageId,
                                                                      DiscordReactionEmoji emoji, Snowflake?baseUserId = null, int?limit = null,
                                                                      ReactionGetStrategy getStrategy = ReactionGetStrategy.Before)
        {
            if (emoji == null)
            {
                throw new ArgumentNullException(nameof(emoji));
            }

            UrlParametersBuilder builder = new UrlParametersBuilder();

            if (baseUserId.HasValue)
            {
                builder.Add(getStrategy.ToString().ToLower(), baseUserId.Value.ToString());
            }
            if (limit.HasValue)
            {
                builder.Add("limit", limit.Value.ToString());
            }

            DiscordApiData data = await rest.Get(
                $"channels/{channelId}/messages/{messageId}/reactions/{emoji}{builder.ToQueryString()}",
                $"channels/{channelId}/messages/message/reactions/emoji").ConfigureAwait(false);

            DiscordUser[] users = new DiscordUser[data.Values.Count];
            for (int i = 0; i < users.Length; i++)
            {
                users[i] = new DiscordUser(false, data.Values[i]);
            }

            return(users);
        }
Beispiel #4
0
        /// <summary>
        /// Bans a users from the specified guild.
        /// <para>Requires <see cref="DiscordPermission.BanMembers"/>.</para>
        /// </summary>
        /// <param name="deleteMessageDays">Number of days to delete messages for (0-7) or null to delete none.</param>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task CreateGuildBan(Snowflake guildId, Snowflake userId, int?deleteMessageDays = null)
        {
            UrlParametersBuilder parameters = new UrlParametersBuilder();

            parameters["delete-message-days"] = deleteMessageDays?.ToString();

            await rest.Put($"guilds/{guildId}/bans/{userId}{parameters.ToQueryString()}",
                           $"guilds/{guildId}/bans/user").ConfigureAwait(false);
        }
Beispiel #5
0
        /// <summary>
        /// Returns the number of members that would be kicked from a guild prune operation.
        /// <para>Requires <see cref="DiscordPermission.KickMembers"/>.</para>
        /// </summary>
        /// <param name="days">The number of days to count prune for (1-30).</param>
        /// <param name="includeRoles">
        /// By default, prune will not remove users with roles. You can optionally include specific
        /// roles in your prune by providing the <paramref name="includeRoles"/> parameter. Any inactive
        /// user that has a subset of the provided role(s) will be counted in the prune and users with
        /// additional roles will not.
        /// </param>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <int> GetGuildPruneCount(Snowflake guildId, int?days = null, IEnumerable <Snowflake> includeRoles = null)
        {
            UrlParametersBuilder parameters = new UrlParametersBuilder();

            if (days != null)
            {
                parameters["days"] = days.Value.ToString();
            }
            if (includeRoles != null)
            {
                parameters["include_roles"] = string.Join(",", includeRoles.Select(r => r.ToString()));
            }

            DiscordApiData data = await rest.Get($"guilds/{guildId}/prune{parameters.ToQueryString()}",
                                                 $"guilds/{guildId}/prune").ConfigureAwait(false);

            return(data.GetInteger("pruned").Value);
        }
Beispiel #6
0
        /// <summary>
        /// Gets an invite by its code.
        /// </summary>
        /// <param name="withCounts">Whether the returned invite should contain approximate member counts.</param>
        /// <exception cref="ArgumentException">Thrown if the invite code is empty or only contains whitespace characters.</exception>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <DiscordInvite> GetInvite(string inviteCode, bool?withCounts = null)
        {
            if (inviteCode == null)
            {
                throw new ArgumentNullException(nameof(inviteCode));
            }
            if (string.IsNullOrWhiteSpace(inviteCode))
            {
                throw new ArgumentException("Invite code cannot be empty or only contain whitespace characters.", nameof(inviteCode));
            }

            UrlParametersBuilder urlParams = new UrlParametersBuilder();

            urlParams["with_counts"] = withCounts?.ToString() ?? null;

            DiscordApiData data = await rest.Get($"invites/{inviteCode}{urlParams.ToQueryString()}", "invities/invite").ConfigureAwait(false);

            return(new DiscordInvite(this, data));
        }
        /// <summary>
        /// Gets a list of members in a guild.
        /// This method is paged, and cannot always return every member at once.
        /// </summary>
        /// <param name="guildId">The ID of the guild.</param>
        /// <param name="limit">Max number of members to return (1-1000).</param>
        /// <param name="after">The highest user ID in the previous page.</param>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <IReadOnlyList <DiscordGuildMember> > ListGuildMembers(Snowflake guildId,
                                                                                 int?limit = null, Snowflake?after = null)
        {
            UrlParametersBuilder urlParams = new UrlParametersBuilder();

            urlParams["limit"] = limit?.ToString() ?? null;
            urlParams["after"] = after?.Id.ToString() ?? null;

            DiscordApiData data = await rest.Get($"guilds/{guildId}/members{urlParams.ToQueryString()}",
                                                 $"guilds/{guildId}/members").ConfigureAwait(false);

            DiscordGuildMember[] members = new DiscordGuildMember[data.Values.Count];
            for (int i = 0; i < members.Length; i++)
            {
                members[i] = new DiscordGuildMember(this, data.Values[i], guildId);
            }

            return(members);
        }