Skip to content

Commit

Permalink
Throw when attempting to add or remove a member's EveryoneRole (#781)
Browse files Browse the repository at this point in the history
* Throw when attempting to add or remove a member's EveryoneRole

This resolves #780

* Removed braces
  • Loading branch information
foxbot authored and Auralytical committed Aug 17, 2017
1 parent f997089 commit 506a6c9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Discord.Net.Core/Utils/Preconditions.cs
Expand Up @@ -192,5 +192,13 @@ public static void YoungerThanTwoWeeks(ulong[] collection, string name)
throw new ArgumentOutOfRangeException(name, "Messages must be younger than two weeks old.");
}
}
public static void NotEveryoneRole(ulong[] roles, ulong guildId, string name)
{
for (var i = 0; i < roles.Length; i++)
{
if (roles[i] == guildId)
throw new ArgumentException($"The everyone role cannot be assigned to a user", name);
}
}
}
}
4 changes: 4 additions & 0 deletions src/Discord.Net.Rest/DiscordRestApiClient.cs
Expand Up @@ -392,6 +392,7 @@ public async Task AddRoleAsync(ulong guildId, ulong userId, ulong roleId, Reques
Preconditions.NotEqual(guildId, 0, nameof(guildId));
Preconditions.NotEqual(userId, 0, nameof(userId));
Preconditions.NotEqual(roleId, 0, nameof(roleId));
Preconditions.NotEqual(roleId, guildId, nameof(roleId), "The Everyone role cannot be added to a user.");
options = RequestOptions.CreateOrClone(options);

var ids = new BucketIds(guildId: guildId);
Expand All @@ -402,6 +403,7 @@ public async Task RemoveRoleAsync(ulong guildId, ulong userId, ulong roleId, Req
Preconditions.NotEqual(guildId, 0, nameof(guildId));
Preconditions.NotEqual(userId, 0, nameof(userId));
Preconditions.NotEqual(roleId, 0, nameof(roleId));
Preconditions.NotEqual(roleId, guildId, nameof(roleId), "The Everyone role cannot be removed from a user.");
options = RequestOptions.CreateOrClone(options);

var ids = new BucketIds(guildId: guildId);
Expand Down Expand Up @@ -1000,6 +1002,8 @@ public async Task ModifyGuildMemberAsync(ulong guildId, ulong userId, Rest.Modif

bool isCurrentUser = userId == CurrentUserId;

if (args.RoleIds.IsSpecified)
Preconditions.NotEveryoneRole(args.RoleIds.Value, guildId, nameof(args.RoleIds));
if (isCurrentUser && args.Nickname.IsSpecified)
{
var nickArgs = new Rest.ModifyCurrentUserNickParams(args.Nickname.Value ?? "");
Expand Down

0 comments on commit 506a6c9

Please sign in to comment.