diff --git a/src/Discord.Net/API/DiscordRawClient.cs b/src/Discord.Net/API/DiscordRawClient.cs index 55beb7f42..0bee8d8b9 100644 --- a/src/Discord.Net/API/DiscordRawClient.cs +++ b/src/Discord.Net/API/DiscordRawClient.cs @@ -169,7 +169,7 @@ namespace Discord.API //Channels public async Task GetChannel(ulong channelId) { - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); try { @@ -179,7 +179,8 @@ namespace Discord.API } public async Task GetChannel(ulong guildId, ulong channelId) { - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); try { @@ -192,57 +193,57 @@ namespace Discord.API } public async Task> GetGuildChannels(ulong guildId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + return await Send>("GET", $"guilds/{guildId}/channels").ConfigureAwait(false); } public async Task CreateGuildChannel(ulong guildId, CreateGuildChannelParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (args.Bitrate <= 0) throw new ArgumentOutOfRangeException(nameof(args.Bitrate)); - if (args.Name == "") throw new ArgumentOutOfRangeException(nameof(args.Name)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.GreaterThan(args.Bitrate, 0, nameof(args.Bitrate)); + Preconditions.NotNullOrWhitespace(args.Name, nameof(args.Name)); return await Send("POST", $"guilds/{guildId}/channels", args).ConfigureAwait(false); } public async Task DeleteChannel(ulong channelId) { - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); return await Send("DELETE", $"channels/{channelId}").ConfigureAwait(false); } public async Task ModifyGuildChannel(ulong channelId, ModifyGuildChannelParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); - if (args.Name == "") throw new ArgumentOutOfRangeException(nameof(args.Name)); - if (args.Position < 0) throw new ArgumentOutOfRangeException(nameof(args.Position)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.AtLeast(args.Position, 0, nameof(args.Position)); + Preconditions.NotNullOrWhitespace(args.Name, nameof(args.Name)); return await Send("PATCH", $"channels/{channelId}", args).ConfigureAwait(false); } public async Task ModifyGuildChannel(ulong channelId, ModifyTextChannelParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); - if (args.Name == "") throw new ArgumentOutOfRangeException(nameof(args.Name)); - if (args.Position < 0) throw new ArgumentOutOfRangeException(nameof(args.Position)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.AtLeast(args.Position, 0, nameof(args.Position)); + Preconditions.NotNullOrWhitespace(args.Name, nameof(args.Name)); return await Send("PATCH", $"channels/{channelId}", args).ConfigureAwait(false); } public async Task ModifyGuildChannel(ulong channelId, ModifyVoiceChannelParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); - if (args.Bitrate <= 0) throw new ArgumentOutOfRangeException(nameof(args.Bitrate)); - if (args.Name == "") throw new ArgumentOutOfRangeException(nameof(args.Name)); - if (args.Position < 0) throw new ArgumentOutOfRangeException(nameof(args.Position)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.GreaterThan(args.Bitrate, 0, nameof(args.Bitrate)); + Preconditions.AtLeast(args.Position, 0, nameof(args.Position)); + Preconditions.NotNullOrWhitespace(args.Name, nameof(args.Name)); return await Send("PATCH", $"channels/{channelId}", args).ConfigureAwait(false); } public async Task ModifyGuildChannels(ulong guildId, IEnumerable args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotNull(args, nameof(args)); var channels = args.ToArray(); switch (channels.Length) @@ -261,7 +262,8 @@ namespace Discord.API //Channel Permissions public async Task ModifyChannelPermissions(ulong channelId, ulong targetId, ModifyChannelPermissionsParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); + Preconditions.NotNull(args, nameof(args)); + await Send("PUT", $"channels/{channelId}/permissions/{targetId}", args).ConfigureAwait(false); } public async Task DeleteChannelPermission(ulong channelId, ulong targetId) @@ -272,7 +274,7 @@ namespace Discord.API //Guilds public async Task GetGuild(ulong guildId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); try { @@ -282,50 +284,50 @@ namespace Discord.API } public async Task CreateGuild(CreateGuildParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (string.IsNullOrEmpty(args.Name)) throw new ArgumentNullException(nameof(args.Name)); - if (string.IsNullOrEmpty(args.Region)) throw new ArgumentNullException(nameof(args.Region)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.NotNullOrWhitespace(args.Name, nameof(args.Name)); + Preconditions.NotNullOrWhitespace(args.Region, nameof(args.Region)); return await Send("POST", "guilds", args).ConfigureAwait(false); } public async Task DeleteGuild(ulong guildId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); return await Send("DELETE", $"guilds/{guildId}").ConfigureAwait(false); } public async Task LeaveGuild(ulong guildId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); return await Send("DELETE", $"users/@me/guilds/{guildId}").ConfigureAwait(false); } public async Task ModifyGuild(ulong guildId, ModifyGuildParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (args.AFKChannelId == 0) throw new ArgumentOutOfRangeException(nameof(args.AFKChannelId)); - if (args.AFKTimeout < 0) throw new ArgumentOutOfRangeException(nameof(args.AFKTimeout)); - if (args.Name == "") throw new ArgumentOutOfRangeException(nameof(args.Name)); - //if (args.OwnerId == 0) throw new ArgumentOutOfRangeException(nameof(args.OwnerId)); - //if (args.Region == "") throw new ArgumentOutOfRangeException(nameof(args.Region)); - if (args.VerificationLevel < 0) throw new ArgumentOutOfRangeException(nameof(args.VerificationLevel)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.NotEqual(args.AFKChannelId, 0, nameof(args.AFKChannelId)); + Preconditions.AtLeast(args.AFKTimeout, 0, nameof(args.AFKTimeout)); + Preconditions.NotNullOrWhitespace(args.Name, nameof(args.Name)); + Preconditions.NotNull(args.Owner, nameof(args.Owner)); + Preconditions.NotNull(args.Region, nameof(args.Region)); + Preconditions.AtLeast(args.VerificationLevel, 0, nameof(args.VerificationLevel)); return await Send("PATCH", $"guilds/{guildId}", args).ConfigureAwait(false); } public async Task BeginGuildPrune(ulong guildId, GuildPruneParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (args.Days < 0) throw new ArgumentOutOfRangeException(nameof(args.Days)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.AtLeast(args.Days, 0, nameof(args.Days)); return await Send("POST", $"guilds/{guildId}/prune", args).ConfigureAwait(false); } public async Task GetGuildPruneCount(ulong guildId, GuildPruneParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (args.Days < 0) throw new ArgumentOutOfRangeException(nameof(args.Days)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.AtLeast(args.Days, 0, nameof(args.Days)); return await Send("GET", $"guilds/{guildId}/prune", args).ConfigureAwait(false); } @@ -333,23 +335,23 @@ namespace Discord.API //Guild Bans public async Task> GetGuildBans(ulong guildId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + return await Send>("GET", $"guilds/{guildId}/bans").ConfigureAwait(false); } public async Task CreateGuildBan(ulong guildId, ulong userId, CreateGuildBanParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (userId == 0) throw new ArgumentOutOfRangeException(nameof(userId)); - if (args.PruneDays < 0) throw new ArgumentOutOfRangeException(nameof(args.PruneDays)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotEqual(userId, 0, nameof(userId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.AtLeast(args.PruneDays, 0, nameof(args.PruneDays)); await Send("PUT", $"guilds/{guildId}/bans/{userId}", args).ConfigureAwait(false); } public async Task RemoveGuildBan(ulong guildId, ulong userId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (userId == 0) throw new ArgumentOutOfRangeException(nameof(userId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotEqual(userId, 0, nameof(userId)); await Send("DELETE", $"guilds/{guildId}/bans/{userId}").ConfigureAwait(false); } @@ -357,7 +359,7 @@ namespace Discord.API //Guild Embeds public async Task GetGuildEmbed(ulong guildId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); try { @@ -367,8 +369,8 @@ namespace Discord.API } public async Task ModifyGuildEmbed(ulong guildId, ModifyGuildEmbedParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); return await Send("PATCH", $"guilds/{guildId}/embed", args).ConfigureAwait(false); } @@ -376,39 +378,39 @@ namespace Discord.API //Guild Integrations public async Task> GetGuildIntegrations(ulong guildId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); return await Send>("GET", $"guilds/{guildId}/integrations").ConfigureAwait(false); } public async Task CreateGuildIntegration(ulong guildId, CreateGuildIntegrationParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (args.Id == 0) throw new ArgumentOutOfRangeException(nameof(args.Id)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.NotEqual(args.Id, 0, nameof(args.Id)); return await Send("POST", $"guilds/{guildId}/integrations").ConfigureAwait(false); } public async Task DeleteGuildIntegration(ulong guildId, ulong integrationId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (integrationId == 0) throw new ArgumentOutOfRangeException(nameof(integrationId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotEqual(integrationId, 0, nameof(integrationId)); return await Send("DELETE", $"guilds/{guildId}/integrations/{integrationId}").ConfigureAwait(false); } public async Task ModifyGuildIntegration(ulong guildId, ulong integrationId, ModifyGuildIntegrationParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (integrationId == 0) throw new ArgumentOutOfRangeException(nameof(integrationId)); - if (args.ExpireBehavior < 0) throw new ArgumentOutOfRangeException(nameof(args.ExpireBehavior)); - if (args.ExpireGracePeriod < 0) throw new ArgumentOutOfRangeException(nameof(args.ExpireGracePeriod)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotEqual(integrationId, 0, nameof(integrationId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.AtLeast(args.ExpireBehavior, 0, nameof(args.ExpireBehavior)); + Preconditions.AtLeast(args.ExpireGracePeriod, 0, nameof(args.ExpireGracePeriod)); return await Send("PATCH", $"guilds/{guildId}/integrations/{integrationId}", args).ConfigureAwait(false); } public async Task SyncGuildIntegration(ulong guildId, ulong integrationId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (integrationId == 0) throw new ArgumentOutOfRangeException(nameof(integrationId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotEqual(integrationId, 0, nameof(integrationId)); return await Send("POST", $"guilds/{guildId}/integrations/{integrationId}/sync").ConfigureAwait(false); } @@ -416,7 +418,7 @@ namespace Discord.API //Guild Invites public async Task GetInvite(string inviteIdOrXkcd) { - if (string.IsNullOrEmpty(inviteIdOrXkcd)) throw new ArgumentOutOfRangeException(nameof(inviteIdOrXkcd)); + Preconditions.NotNullOrEmpty(inviteIdOrXkcd, nameof(inviteIdOrXkcd)); try { @@ -426,34 +428,34 @@ namespace Discord.API } public async Task> GetGuildInvites(ulong guildId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); return await Send>("GET", $"guilds/{guildId}/invites").ConfigureAwait(false); } public async Task GetChannelInvites(ulong channelId) { - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); return await Send("GET", $"channels/{channelId}/invites").ConfigureAwait(false); } public async Task CreateChannelInvite(ulong channelId, CreateChannelInviteParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); - if (args.MaxAge < 0) throw new ArgumentOutOfRangeException(nameof(args.MaxAge)); - if (args.MaxUses < 0) throw new ArgumentOutOfRangeException(nameof(args.MaxUses)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.AtLeast(args.MaxAge, 0, nameof(args.MaxAge)); + Preconditions.AtLeast(args.MaxUses, 0, nameof(args.MaxUses)); return await Send("POST", $"channels/{channelId}/invites", args).ConfigureAwait(false); } public async Task DeleteInvite(string inviteCode) { - if (string.IsNullOrEmpty(inviteCode)) throw new ArgumentOutOfRangeException(nameof(inviteCode)); + Preconditions.NotNullOrEmpty(inviteCode, nameof(inviteCode)); return await Send("DELETE", $"invites/{inviteCode}").ConfigureAwait(false); } public async Task AcceptInvite(string inviteCode) { - if (string.IsNullOrEmpty(inviteCode)) throw new ArgumentOutOfRangeException(nameof(inviteCode)); + Preconditions.NotNullOrEmpty(inviteCode, nameof(inviteCode)); await Send("POST", $"invites/{inviteCode}").ConfigureAwait(false); } @@ -461,8 +463,8 @@ namespace Discord.API //Guild Members public async Task GetGuildMember(ulong guildId, ulong userId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (userId == 0) throw new ArgumentOutOfRangeException(nameof(userId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotEqual(userId, 0, nameof(userId)); try { @@ -472,10 +474,10 @@ namespace Discord.API } public async Task> GetGuildMembers(ulong guildId, GetGuildMembersParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (args.Limit.IsSpecified && args.Limit <= 0) throw new ArgumentOutOfRangeException(nameof(args.Limit)); - if (args.Offset.IsSpecified && args.Offset < 0) throw new ArgumentOutOfRangeException(nameof(args.Offset)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.GreaterThan(args.Limit, 0, nameof(args.Limit)); + Preconditions.AtLeast(args.Offset, 0, nameof(args.Offset)); int limit = args.Limit.GetValueOrDefault(int.MaxValue); int offset = args.Offset.GetValueOrDefault(0); @@ -513,55 +515,55 @@ namespace Discord.API } public async Task RemoveGuildMember(ulong guildId, ulong userId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (userId == 0) throw new ArgumentOutOfRangeException(nameof(userId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotEqual(userId, 0, nameof(userId)); await Send("DELETE", $"guilds/{guildId}/members/{userId}").ConfigureAwait(false); } public async Task ModifyGuildMember(ulong guildId, ulong userId, ModifyGuildMemberParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (userId == 0) throw new ArgumentOutOfRangeException(nameof(userId)); - + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotEqual(userId, 0, nameof(userId)); + Preconditions.NotNull(args, nameof(args)); + await Send("PATCH", $"guilds/{guildId}/members/{userId}", args).ConfigureAwait(false); } //Guild Roles public async Task> GetGuildRoles(ulong guildId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); return await Send>("GET", $"guilds/{guildId}/roles").ConfigureAwait(false); } public async Task CreateGuildRole(ulong guildId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); return await Send("POST", $"guilds/{guildId}/roles").ConfigureAwait(false); } public async Task DeleteGuildRole(ulong guildId, ulong roleId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (roleId == 0) throw new ArgumentOutOfRangeException(nameof(roleId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotEqual(roleId, 0, nameof(roleId)); await Send("DELETE", $"guilds/{guildId}/roles/{roleId}").ConfigureAwait(false); } public async Task ModifyGuildRole(ulong guildId, ulong roleId, ModifyGuildRoleParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); - if (roleId == 0) throw new ArgumentOutOfRangeException(nameof(roleId)); - if (args.Color < 0) throw new ArgumentOutOfRangeException(nameof(args.Color)); - if (args.Name == "") throw new ArgumentOutOfRangeException(nameof(args.Name)); - if (args.Position < 0) throw new ArgumentOutOfRangeException(nameof(args.Position)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotEqual(roleId, 0, nameof(roleId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.AtLeast(args.Color, 0, nameof(args.Color)); + Preconditions.NotNullOrWhitespace(args.Name, nameof(args.Name)); + Preconditions.AtLeast(args.Position, 0, nameof(args.Position)); return await Send("PATCH", $"guilds/{guildId}/roles/{roleId}", args).ConfigureAwait(false); } public async Task> ModifyGuildRoles(ulong guildId, IEnumerable args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + Preconditions.NotNull(args, nameof(args)); var roles = args.ToArray(); switch (roles.Length) @@ -578,9 +580,9 @@ namespace Discord.API //Messages public async Task> GetChannelMessages(ulong channelId, GetChannelMessagesParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); - if (args.Limit <= 0) throw new ArgumentOutOfRangeException(nameof(args.Limit)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.AtLeast(args.Limit, 0, nameof(args.Limit)); int limit = args.Limit; ulong? relativeId = args.RelativeMessageId.IsSpecified ? args.RelativeMessageId.Value : (ulong?)null; @@ -625,7 +627,8 @@ namespace Discord.API } public Task CreateMessage(ulong guildId, ulong channelId, CreateMessageParams args) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + return CreateMessageInternal(guildId, channelId, args); } public Task CreateDMMessage(ulong channelId, CreateMessageParams args) @@ -634,11 +637,11 @@ namespace Discord.API } public async Task CreateMessageInternal(ulong guildId, ulong channelId, CreateMessageParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); - if (string.IsNullOrEmpty(args.Content)) throw new ArgumentNullException(nameof(args.Content)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.NotNullOrEmpty(args.Content, nameof(args.Content)); if (args.Content.Length > DiscordConfig.MaxMessageSize) - throw new ArgumentOutOfRangeException($"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", nameof(args.Content)); + throw new ArgumentException($"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", nameof(args.Content)); if (guildId != 0) return await Send("POST", $"channels/{channelId}/messages", args, GuildBucket.SendEditMessage, guildId).ConfigureAwait(false); @@ -647,7 +650,8 @@ namespace Discord.API } public Task UploadFile(ulong guildId, ulong channelId, Stream file, UploadFileParams args) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + return UploadFileInternal(guildId, channelId, file, args); } public Task UploadDMFile(ulong channelId, Stream file, UploadFileParams args) @@ -656,11 +660,11 @@ namespace Discord.API } private async Task UploadFileInternal(ulong guildId, ulong channelId, Stream file, UploadFileParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); if (args.Content.IsSpecified) { - if (string.IsNullOrEmpty(args.Content.Value)) throw new ArgumentNullException(nameof(args.Content)); + Preconditions.NotNullOrEmpty(args.Content, nameof(args.Content)); if (args.Content.Value.Length > DiscordConfig.MaxMessageSize) throw new ArgumentOutOfRangeException($"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", nameof(args.Content)); } @@ -672,7 +676,8 @@ namespace Discord.API } public Task DeleteMessage(ulong guildId, ulong channelId, ulong messageId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + return DeleteMessageInternal(guildId, channelId, messageId); } public Task DeleteDMMessage(ulong channelId, ulong messageId) @@ -681,8 +686,8 @@ namespace Discord.API } private async Task DeleteMessageInternal(ulong guildId, ulong channelId, ulong messageId) { - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); - if (messageId == 0) throw new ArgumentOutOfRangeException(nameof(messageId)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); + Preconditions.NotEqual(messageId, 0, nameof(messageId)); if (guildId != 0) await Send("DELETE", $"channels/{channelId}/messages/{messageId}", GuildBucket.DeleteMessage, guildId).ConfigureAwait(false); @@ -691,7 +696,8 @@ namespace Discord.API } public Task DeleteMessages(ulong guildId, ulong channelId, DeleteMessagesParam args) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + return DeleteMessagesInternal(guildId, channelId, args); } public Task DeleteDMMessages(ulong channelId, DeleteMessagesParam args) @@ -700,9 +706,9 @@ namespace Discord.API } private async Task DeleteMessagesInternal(ulong guildId, ulong channelId, DeleteMessagesParam args) { - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); - if (args == null) throw new ArgumentNullException(nameof(args)); - if (args.MessageIds == null) throw new ArgumentNullException(nameof(args.MessageIds)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.NotNull(args.MessageIds, nameof(args.MessageIds)); var messageIds = args.MessageIds.ToArray(); switch (messageIds.Length) @@ -722,7 +728,8 @@ namespace Discord.API } public Task ModifyMessage(ulong guildId, ulong channelId, ulong messageId, ModifyMessageParams args) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); + return ModifyMessageInternal(guildId, channelId, messageId, args); } public Task ModifyDMMessage(ulong channelId, ulong messageId, ModifyMessageParams args) @@ -731,9 +738,15 @@ namespace Discord.API } private async Task ModifyMessageInternal(ulong guildId, ulong channelId, ulong messageId, ModifyMessageParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); - if (messageId == 0) throw new ArgumentOutOfRangeException(nameof(messageId)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); + Preconditions.NotEqual(messageId, 0, nameof(messageId)); + Preconditions.NotNull(args, nameof(args)); + if (args.Content.IsSpecified) + { + Preconditions.NotNullOrEmpty(args.Content, nameof(args.Content)); + if (args.Content.Value.Length > DiscordConfig.MaxMessageSize) + throw new ArgumentOutOfRangeException($"Message content is too long, length must be less or equal to {DiscordConfig.MaxMessageSize}.", nameof(args.Content)); + } if (guildId != 0) return await Send("PATCH", $"channels/{channelId}/messages/{messageId}", args, GuildBucket.SendEditMessage, guildId).ConfigureAwait(false); @@ -742,14 +755,14 @@ namespace Discord.API } public async Task AckMessage(ulong channelId, ulong messageId) { - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); - if (messageId == 0) throw new ArgumentOutOfRangeException(nameof(messageId)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); + Preconditions.NotEqual(messageId, 0, nameof(messageId)); await Send("POST", $"channels/{channelId}/messages/{messageId}/ack").ConfigureAwait(false); } public async Task TriggerTypingIndicator(ulong channelId) { - if (channelId == 0) throw new ArgumentOutOfRangeException(nameof(channelId)); + Preconditions.NotEqual(channelId, 0, nameof(channelId)); await Send("POST", $"channels/{channelId}/typing").ConfigureAwait(false); } @@ -757,7 +770,7 @@ namespace Discord.API //Users public async Task GetUser(ulong userId) { - if (userId == 0) throw new ArgumentOutOfRangeException(nameof(userId)); + Preconditions.NotEqual(userId, 0, nameof(userId)); try { @@ -767,7 +780,7 @@ namespace Discord.API } public async Task GetUser(string username, ushort discriminator) { - if (string.IsNullOrEmpty(username)) throw new ArgumentOutOfRangeException(nameof(username)); + Preconditions.NotNullOrEmpty(username, nameof(username)); try { @@ -778,8 +791,8 @@ namespace Discord.API } public async Task> QueryUsers(string query, int limit) { - if (string.IsNullOrEmpty(query)) throw new ArgumentOutOfRangeException(nameof(query)); - if (limit <= 0) throw new ArgumentOutOfRangeException(nameof(limit)); + Preconditions.NotNullOrEmpty(query, nameof(query)); + Preconditions.AtLeast(limit, 0, nameof(limit)); return await Send>("GET", $"users?q={Uri.EscapeDataString(query)}&limit={limit}").ConfigureAwait(false); } @@ -803,23 +816,23 @@ namespace Discord.API } public async Task ModifyCurrentUser(ModifyCurrentUserParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (args.Email == "") throw new ArgumentOutOfRangeException(nameof(args.Email)); - if (args.Username == "") throw new ArgumentOutOfRangeException(nameof(args.Username)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.NotNullOrEmpty(args.Email, nameof(args.Email)); + Preconditions.NotNullOrEmpty(args.Username, nameof(args.Username)); return await Send("PATCH", "users/@me", args).ConfigureAwait(false); } public async Task ModifyCurrentUserNick(ulong guildId, ModifyCurrentUserNickParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (args.Nickname == "") throw new ArgumentOutOfRangeException(nameof(args.Nickname)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.NotEmpty(args.Nickname, nameof(args.Nickname)); await Send("PATCH", $"guilds/{guildId}/members/@me/nick", args).ConfigureAwait(false); } public async Task CreateDMChannel(CreateDMChannelParams args) { - if (args == null) throw new ArgumentNullException(nameof(args)); - if (args.RecipientId == 0) throw new ArgumentOutOfRangeException(nameof(args.RecipientId)); + Preconditions.NotNull(args, nameof(args)); + Preconditions.NotEqual(args.RecipientId, 0, nameof(args.RecipientId)); return await Send("POST", $"users/@me/channels", args).ConfigureAwait(false); } @@ -831,7 +844,7 @@ namespace Discord.API } public async Task> GetGuildVoiceRegions(ulong guildId) { - if (guildId == 0) throw new ArgumentOutOfRangeException(nameof(guildId)); + Preconditions.NotEqual(guildId, 0, nameof(guildId)); return await Send>("GET", $"guilds/{guildId}/regions").ConfigureAwait(false); } diff --git a/src/Discord.Net/API/Optional.cs b/src/Discord.Net/API/Optional.cs index 6baef029f..0fe96071c 100644 --- a/src/Discord.Net/API/Optional.cs +++ b/src/Discord.Net/API/Optional.cs @@ -46,6 +46,6 @@ namespace Discord.API private string DebuggerDisplay => IsSpecified ? _value.ToString() : ""; public static implicit operator Optional(T value) => new Optional(value); - public static implicit operator T(Optional value) => value.Value; + public static explicit operator T(Optional value) => value.Value; } } diff --git a/src/Discord.Net/API/Rest/ModifyGuildChannelsParams.cs b/src/Discord.Net/API/Rest/ModifyGuildChannelsParams.cs index 8444bb598..23d498f25 100644 --- a/src/Discord.Net/API/Rest/ModifyGuildChannelsParams.cs +++ b/src/Discord.Net/API/Rest/ModifyGuildChannelsParams.cs @@ -5,7 +5,7 @@ namespace Discord.API.Rest public class ModifyGuildChannelsParams { [JsonProperty("id")] - public Optional Id { get; set; } + public ulong Id { get; set; } [JsonProperty("position")] public Optional Position { get; set; } } diff --git a/src/Discord.Net/API/Rest/ModifyGuildRolesParams.cs b/src/Discord.Net/API/Rest/ModifyGuildRolesParams.cs index 286c2463d..7002079d5 100644 --- a/src/Discord.Net/API/Rest/ModifyGuildRolesParams.cs +++ b/src/Discord.Net/API/Rest/ModifyGuildRolesParams.cs @@ -5,6 +5,6 @@ namespace Discord.API.Rest public class ModifyGuildRolesParams : ModifyGuildRoleParams { [JsonProperty("id")] - public Optional Id { get; set; } + public ulong Id { get; set; } } } diff --git a/src/Discord.Net/Discord.Net.csproj b/src/Discord.Net/Discord.Net.csproj index 172157cbc..6a3f65685 100644 --- a/src/Discord.Net/Discord.Net.csproj +++ b/src/Discord.Net/Discord.Net.csproj @@ -107,6 +107,7 @@ + diff --git a/src/Discord.Net/Preconditions.cs b/src/Discord.Net/Preconditions.cs new file mode 100644 index 000000000..550f4beae --- /dev/null +++ b/src/Discord.Net/Preconditions.cs @@ -0,0 +1,257 @@ +using Discord.API; +using System; +using System.Runtime.CompilerServices; + +namespace Discord +{ + internal static class Preconditions + { + //Objects + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotNull(T obj, string name) where T : class { if (obj == null) throw new ArgumentNullException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotNull(Optional obj, string name) where T : class { if (obj.IsSpecified && obj.Value == null) throw new ArgumentNullException(name); } + + //Strings + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEmpty(string obj, string name) { if (obj.Length == 0) throw new ArgumentException("Argument cannot be empty.", name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEmpty(Optional obj, string name) { if (obj.IsSpecified && obj.Value.Length == 0) throw new ArgumentException("Argument cannot be empty.", name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotNullOrEmpty(string obj, string name) + { + if (obj == null) + throw new ArgumentNullException(name); + if (obj.Length == 0) + throw new ArgumentException("Argument cannot be empty.", name); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotNullOrEmpty(Optional obj, string name) + { + if (obj.IsSpecified) + { + if (obj.Value == null) + throw new ArgumentNullException(name); + if (obj.Value.Length == 0) + throw new ArgumentException("Argument cannot be empty.", name); + } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotNullOrWhitespace(string obj, string name) + { + if (obj == null) + throw new ArgumentNullException(name); + if (obj.Trim().Length == 0) + throw new ArgumentException("Argument cannot be blank.", name); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotNullOrWhitespace(Optional obj, string name) + { + if (obj.IsSpecified) + { + if (obj.Value == null) + throw new ArgumentNullException(name); + if (obj.Value.Trim().Length == 0) + throw new ArgumentException("Argument cannot be blank.", name); + } + } + + //Numerics + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(sbyte obj, sbyte value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(byte obj, byte value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(short obj, short value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(ushort obj, ushort value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(int obj, int value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(uint obj, uint value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(long obj, long value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(ulong obj, ulong value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, sbyte value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, byte value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, short value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, ushort value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, int value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, uint value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, long value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, ulong value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(sbyte? obj, sbyte value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(byte? obj, byte value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(short? obj, short value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(ushort? obj, ushort value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(int? obj, int value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(uint? obj, uint value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(long? obj, long value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(ulong? obj, ulong value, string name) { if (obj == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, sbyte value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, byte value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, short value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, ushort value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, int value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, uint value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, long value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void NotEqual(Optional obj, ulong value, string name) { if (obj.IsSpecified && obj.Value == value) throw new ArgumentOutOfRangeException(name); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(sbyte obj, sbyte value, string name) { if (obj < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(byte obj, byte value, string name) { if (obj < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(short obj, short value, string name) { if (obj < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(ushort obj, ushort value, string name) { if (obj < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(int obj, int value, string name) { if (obj < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(uint obj, uint value, string name) { if (obj < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(long obj, long value, string name) { if (obj < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(ulong obj, ulong value, string name) { if (obj < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(Optional obj, sbyte value, string name) { if (obj.IsSpecified && obj.Value < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(Optional obj, byte value, string name) { if (obj.IsSpecified && obj.Value < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(Optional obj, short value, string name) { if (obj.IsSpecified && obj.Value < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(Optional obj, ushort value, string name) { if (obj.IsSpecified && obj.Value < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(Optional obj, int value, string name) { if (obj.IsSpecified && obj.Value < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(Optional obj, uint value, string name) { if (obj.IsSpecified && obj.Value < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(Optional obj, long value, string name) { if (obj.IsSpecified && obj.Value < value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtLeast(Optional obj, ulong value, string name) { if (obj.IsSpecified && obj.Value < value) throw new ArgumentOutOfRangeException(name); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(sbyte obj, sbyte value, string name) { if (obj <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(byte obj, byte value, string name) { if (obj <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(short obj, short value, string name) { if (obj <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(ushort obj, ushort value, string name) { if (obj <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(int obj, int value, string name) { if (obj <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(uint obj, uint value, string name) { if (obj <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(long obj, long value, string name) { if (obj <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(ulong obj, ulong value, string name) { if (obj <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(Optional obj, sbyte value, string name) { if (obj.IsSpecified && obj.Value <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(Optional obj, byte value, string name) { if (obj.IsSpecified && obj.Value <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(Optional obj, short value, string name) { if (obj.IsSpecified && obj.Value <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(Optional obj, ushort value, string name) { if (obj.IsSpecified && obj.Value <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(Optional obj, int value, string name) { if (obj.IsSpecified && obj.Value <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(Optional obj, uint value, string name) { if (obj.IsSpecified && obj.Value <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(Optional obj, long value, string name) { if (obj.IsSpecified && obj.Value <= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void GreaterThan(Optional obj, ulong value, string name) { if (obj.IsSpecified && obj.Value <= value) throw new ArgumentOutOfRangeException(name); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(sbyte obj, sbyte value, string name) { if (obj > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(byte obj, byte value, string name) { if (obj > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(short obj, short value, string name) { if (obj > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(ushort obj, ushort value, string name) { if (obj > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(int obj, int value, string name) { if (obj > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(uint obj, uint value, string name) { if (obj > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(long obj, long value, string name) { if (obj > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(ulong obj, ulong value, string name) { if (obj > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(Optional obj, sbyte value, string name) { if (obj.IsSpecified && obj.Value > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(Optional obj, byte value, string name) { if (obj.IsSpecified && obj.Value > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(Optional obj, short value, string name) { if (obj.IsSpecified && obj.Value > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(Optional obj, ushort value, string name) { if (obj.IsSpecified && obj.Value > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(Optional obj, int value, string name) { if (obj.IsSpecified && obj.Value > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(Optional obj, uint value, string name) { if (obj.IsSpecified && obj.Value > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(Optional obj, long value, string name) { if (obj.IsSpecified && obj.Value > value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void AtMost(Optional obj, ulong value, string name) { if (obj.IsSpecified && obj.Value > value) throw new ArgumentOutOfRangeException(name); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(sbyte obj, sbyte value, string name) { if (obj >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(byte obj, byte value, string name) { if (obj >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(short obj, short value, string name) { if (obj >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(ushort obj, ushort value, string name) { if (obj >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(int obj, int value, string name) { if (obj >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(uint obj, uint value, string name) { if (obj >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(long obj, long value, string name) { if (obj >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(ulong obj, ulong value, string name) { if (obj >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(Optional obj, sbyte value, string name) { if (obj.IsSpecified && obj.Value >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(Optional obj, byte value, string name) { if (obj.IsSpecified && obj.Value >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(Optional obj, short value, string name) { if (obj.IsSpecified && obj.Value >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(Optional obj, ushort value, string name) { if (obj.IsSpecified && obj.Value >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(Optional obj, int value, string name) { if (obj.IsSpecified && obj.Value >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(Optional obj, uint value, string name) { if (obj.IsSpecified && obj.Value >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(Optional obj, long value, string name) { if (obj.IsSpecified && obj.Value >= value) throw new ArgumentOutOfRangeException(name); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void LessThan(Optional obj, ulong value, string name) { if (obj.IsSpecified && obj.Value >= value) throw new ArgumentOutOfRangeException(name); } + } +} diff --git a/src/Discord.Net/Rest/Entities/Users/GuildUser.cs b/src/Discord.Net/Rest/Entities/Users/GuildUser.cs index bc6dfa7a2..72c7d20a8 100644 --- a/src/Discord.Net/Rest/Entities/Users/GuildUser.cs +++ b/src/Discord.Net/Rest/Entities/Users/GuildUser.cs @@ -97,11 +97,11 @@ namespace Discord.Rest { await Discord.BaseClient.ModifyGuildMember(Guild.Id, Id, args).ConfigureAwait(false); if (args.Deaf.IsSpecified) - IsDeaf = args.Deaf; + IsDeaf = args.Deaf.Value; if (args.Mute.IsSpecified) - IsMute = args.Mute; + IsMute = args.Mute.Value; if (args.Nickname.IsSpecified) - Nickname = args.Nickname; + Nickname = args.Nickname.Value; if (args.Roles.IsSpecified) _roles = args.Roles.Value.Select(x => Guild.GetRole(x)).Where(x => x != null).ToImmutableArray(); } diff --git a/src/Discord.Net/WebSocket/Entities/Users/GuildUser.cs b/src/Discord.Net/WebSocket/Entities/Users/GuildUser.cs index 57b065a50..cafebe536 100644 --- a/src/Discord.Net/WebSocket/Entities/Users/GuildUser.cs +++ b/src/Discord.Net/WebSocket/Entities/Users/GuildUser.cs @@ -99,11 +99,11 @@ namespace Discord.WebSocket { await Discord.BaseClient.ModifyGuildMember(Guild.Id, Id, args).ConfigureAwait(false); if (args.Deaf.IsSpecified) - IsDeaf = args.Deaf; + IsDeaf = args.Deaf.Value; if (args.Mute.IsSpecified) - IsMute = args.Mute; + IsMute = args.Mute.Value; if (args.Nickname.IsSpecified) - Nickname = args.Nickname; + Nickname = args.Nickname.Value; if (args.Roles.IsSpecified) _roles = args.Roles.Value.Select(x => Guild.GetRole(x)).Where(x => x != null).ToImmutableArray(); }