Browse Source

Added Channel Permissions

tags/docs-0.9
Brandon Smith 9 years ago
parent
commit
f5bf74b500
4 changed files with 87 additions and 1 deletions
  1. +61
    -0
      src/Discord.Net/DiscordClient.API.cs
  2. +11
    -0
      src/Discord.Net/Net/API/DiscordAPIClient.cs
  3. +2
    -1
      src/Discord.Net/Net/API/Endpoints.cs
  4. +13
    -0
      src/Discord.Net/Net/API/Requests.cs

+ 61
- 0
src/Discord.Net/DiscordClient.API.cs View File

@@ -423,6 +423,67 @@ namespace Discord
return null; return null;
} }


//Permissions
public Task SetChannelUserPermissions(Channel channel, User user, PackedPermissions allow, PackedPermissions deny)
=> SetChannelPermissions(channel?.Id, user?.Id, "member", allow, deny);
public Task SetChannelUserPermissions(string channelId, User user, PackedPermissions allow, PackedPermissions deny)
=> SetChannelPermissions(channelId, user?.Id, "member", allow, deny);
public Task SetChannelUserPermissions(Channel channel, string userId, PackedPermissions allow, PackedPermissions deny)
=> SetChannelPermissions(channel?.Id, userId, "member", allow, deny);
public Task SetChannelUserPermissions(string channelId, string userId, PackedPermissions allow, PackedPermissions deny)
=> SetChannelPermissions(channelId, userId, "member", allow, deny);

public Task SetChannelRolePermissions(Channel channel, Role role, PackedPermissions allow, PackedPermissions deny)
=> SetChannelPermissions(channel?.Id, role?.Id, "role", allow, deny);
public Task SetChannelRolePermissions(string channelId, Role role, PackedPermissions allow, PackedPermissions deny)
=> SetChannelPermissions(channelId, role?.Id, "role", allow, deny);
public Task SetChannelRolePermissions(Channel channel, string userId, PackedPermissions allow, PackedPermissions deny)
=> SetChannelPermissions(channel?.Id, userId, "role", allow, deny);
public Task SetChannelRolePermissions(string channelId, string userId, PackedPermissions allow, PackedPermissions deny)
=> SetChannelPermissions(channelId, userId, "role", allow, deny);

private Task SetChannelPermissions(string channelId, string userOrRoleId, string idType, PackedPermissions allow, PackedPermissions deny)
{
CheckReady();
if (channelId == null) throw new NullReferenceException(nameof(channelId));
if (userOrRoleId == null) throw new NullReferenceException(nameof(userOrRoleId));

return _api.SetChannelPermissions(channelId, userOrRoleId, idType, allow, deny);
//TODO: Remove permission from cache
}

public Task RemoveChannelUserPermissions(Channel channel, User user)
=> RemoveChannelPermissions(channel?.Id, user?.Id);
public Task RemoveChannelUserPermissions(string channelId, User user)
=> RemoveChannelPermissions(channelId, user?.Id);
public Task RemoveChannelPermissions(Channel channel, string userId)
=> RemoveChannelUserPermissions(channel?.Id, userId);
public Task RemoveChannelUserPermissions(string channelId, string userId)
=> RemoveChannelPermissions(channelId, userId);

public Task RemoveChannelRolePermissions(Channel channel, Role role)
=> RemoveChannelPermissions(channel?.Id, role?.Id);
public Task RemoveChannelRolePermissions(string channelId, Role role)
=> RemoveChannelPermissions(channelId, role?.Id);
public Task RemoveChannelRolePermissions(Channel channel, string roleId)
=> RemoveChannelUserPermissions(channel?.Id, roleId);
public Task RemoveChannelRolePermissions(string channelId, string roleId)
=> RemoveChannelPermissions(channelId, roleId);

private async Task RemoveChannelPermissions(string channelId, string userOrRoleId)
{
CheckReady();
if (channelId == null) throw new NullReferenceException(nameof(channelId));
if (userOrRoleId == null) throw new NullReferenceException(nameof(userOrRoleId));

try
{
await _api.DeleteChannelPermissions(channelId, userOrRoleId).ConfigureAwait(false);
//TODO: Remove permission from cache
}
catch (HttpException ex) when (ex.StatusCode == HttpStatusCode.NotFound) { }
}

//Voice //Voice
/// <summary> Mutes a user on the provided server. </summary> /// <summary> Mutes a user on the provided server. </summary>
public Task Mute(Server server, User user) public Task Mute(Server server, User user)


+ 11
- 0
src/Discord.Net/Net/API/DiscordAPIClient.cs View File

@@ -154,5 +154,16 @@ namespace Discord.Net.API
var request = new Requests.ChangeAvatar { Avatar = $"data:{type},/9j/{base64}", CurrentEmail = currentEmail, CurrentPassword = currentPassword }; var request = new Requests.ChangeAvatar { Avatar = $"data:{type},/9j/{base64}", CurrentEmail = currentEmail, CurrentPassword = currentPassword };
return _rest.Patch<Responses.ChangeProfile>(Endpoints.UserMe, request); return _rest.Patch<Responses.ChangeProfile>(Endpoints.UserMe, request);
} }

//Permissions
public Task SetChannelPermissions(string channelId, string userOrRoleId, string idType, PackedPermissions allow, PackedPermissions deny)
{
var request = new Requests.SetChannelPermissions { Id = userOrRoleId, Type = idType, Allow = allow.RawValue, Deny = deny.RawValue };
return _rest.Put(Endpoints.ChannelPermission(channelId, userOrRoleId), request);
}
public Task DeleteChannelPermissions(string channelId, string userOrRoleId)
{
return _rest.Delete(Endpoints.ChannelPermission(channelId, userOrRoleId), null);
}
} }
} }

+ 2
- 1
src/Discord.Net/Net/API/Endpoints.cs View File

@@ -19,7 +19,8 @@
public static string ChannelMessages(string channelId, int limit) => $"channels/{channelId}/messages?limit={limit}"; public static string ChannelMessages(string channelId, int limit) => $"channels/{channelId}/messages?limit={limit}";
public static string ChannelMessage(string channelId, string msgId) => $"channels/{channelId}/messages/{msgId}"; public static string ChannelMessage(string channelId, string msgId) => $"channels/{channelId}/messages/{msgId}";
public static string ChannelInvites(string channelId) => $"channels/{channelId}/invites"; public static string ChannelInvites(string channelId) => $"channels/{channelId}/invites";
public static string ChannelPermission(string channelId, string userOrRoleId) => $"channels/{channelId}/permissions/{userOrRoleId}";

public const string Servers = "guilds"; public const string Servers = "guilds";
public static string Server(string serverId) => $"guilds/{serverId}"; public static string Server(string serverId) => $"guilds/{serverId}";
public static string ServerChannels(string serverId) => $"guilds/{serverId}/channels"; public static string ServerChannels(string serverId) => $"guilds/{serverId}/channels";


+ 13
- 0
src/Discord.Net/Net/API/Requests.cs View File

@@ -125,5 +125,18 @@ namespace Discord.Net.API
[JsonProperty(PropertyName = "avatar")] [JsonProperty(PropertyName = "avatar")]
public string Avatar; public string Avatar;
} }

//Permissions
public sealed class SetChannelPermissions
{
[JsonProperty(PropertyName = "id")]
public string Id;
[JsonProperty(PropertyName = "type")]
public string Type;
[JsonProperty(PropertyName = "allow")]
public uint Allow;
[JsonProperty(PropertyName = "deny")]
public uint Deny;
}
} }
} }

Loading…
Cancel
Save