Skip to content

Commit

Permalink
Fix #854 Added ViewChannel enum and property to channel permissions (#…
Browse files Browse the repository at this point in the history
…874)

* Fix #854 Added ViewChannel enum and property to channel permissions

* replaced usages of ChannelPermission#ReadMessages with ViewChannel

* rename parameter of ChannelPermissions constructor

* made OverwritePermissions#ReadMessages obsolete, use ViewChannel instead

* Fix #854 Added ViewChannel enum and property to channel permissions

replaced usages of ChannelPermission#ReadMessages with ViewChannel

rename parameter of ChannelPermissions constructor

made OverwritePermissions#ReadMessages obsolete, use ViewChannel instead

* renamed readMessages parameter in ChannelPermissions constructor and Modify

* fixed channel permission tests to use ChannelPermission enum instead of GuildPermission enum

* replaced usages of readmessages in channel permission tests

* resolve build warnings for permission tests
  • Loading branch information
Chris-Johnston authored and foxbot committed Jan 7, 2018
1 parent 804d918 commit edfbd05
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public enum ChannelPermission : ulong

// Text
AddReactions = 0x00_00_00_40,
ReadMessages = 0x00_00_04_00,
[Obsolete("Use ViewChannel instead.")]
ReadMessages = ViewChannel,
ViewChannel = 0x00_00_04_00,
SendMessages = 0x00_00_08_00,
SendTTSMessages = 0x00_00_10_00,
ManageMessages = 0x00_00_20_00,
Expand Down
18 changes: 11 additions & 7 deletions src/Discord.Net.Core/Entities/Permissions/ChannelPermissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ public static ChannelPermissions All(IChannel channel)
/// <summary> If true, a user may add reactions. </summary>
public bool AddReactions => Permissions.GetValue(RawValue, ChannelPermission.AddReactions);
/// <summary> If True, a user may join channels. </summary>
public bool ReadMessages => Permissions.GetValue(RawValue, ChannelPermission.ReadMessages);
[Obsolete("Use ViewChannel instead.")]
public bool ReadMessages => ViewChannel;
/// <summary> If True, a user may view channels. </summary>
public bool ViewChannel => Permissions.GetValue(RawValue, ChannelPermission.ViewChannel);

/// <summary> If True, a user may send messages. </summary>
public bool SendMessages => Permissions.GetValue(RawValue, ChannelPermission.SendMessages);
/// <summary> If True, a user may send text-to-speech messages. </summary>
Expand Down Expand Up @@ -82,7 +86,7 @@ public static ChannelPermissions All(IChannel channel)

private ChannelPermissions(ulong initialValue, bool? createInstantInvite = null, bool? manageChannel = null,
bool? addReactions = null,
bool? readMessages = null, bool? sendMessages = null, bool? sendTTSMessages = null, bool? manageMessages = null,
bool? viewChannel = null, bool? sendMessages = null, bool? sendTTSMessages = null, bool? manageMessages = null,
bool? embedLinks = null, bool? attachFiles = null, bool? readMessageHistory = null, bool? mentionEveryone = null,
bool? useExternalEmojis = null, bool? connect = null, bool? speak = null, bool? muteMembers = null, bool? deafenMembers = null,
bool? moveMembers = null, bool? useVoiceActivation = null, bool? manageRoles = null, bool? manageWebhooks = null)
Expand All @@ -92,7 +96,7 @@ private ChannelPermissions(ulong initialValue, bool? createInstantInvite = null,
Permissions.SetValue(ref value, createInstantInvite, ChannelPermission.CreateInstantInvite);
Permissions.SetValue(ref value, manageChannel, ChannelPermission.ManageChannels);
Permissions.SetValue(ref value, addReactions, ChannelPermission.AddReactions);
Permissions.SetValue(ref value, readMessages, ChannelPermission.ReadMessages);
Permissions.SetValue(ref value, viewChannel, ChannelPermission.ViewChannel);
Permissions.SetValue(ref value, sendMessages, ChannelPermission.SendMessages);
Permissions.SetValue(ref value, sendTTSMessages, ChannelPermission.SendTTSMessages);
Permissions.SetValue(ref value, manageMessages, ChannelPermission.ManageMessages);
Expand All @@ -116,23 +120,23 @@ private ChannelPermissions(ulong initialValue, bool? createInstantInvite = null,
/// <summary> Creates a new ChannelPermissions with the provided permissions. </summary>
public ChannelPermissions(bool createInstantInvite = false, bool manageChannel = false,
bool addReactions = false,
bool readMessages = false, bool sendMessages = false, bool sendTTSMessages = false, bool manageMessages = false,
bool viewChannel = false, bool sendMessages = false, bool sendTTSMessages = false, bool manageMessages = false,
bool embedLinks = false, bool attachFiles = false, bool readMessageHistory = false, bool mentionEveryone = false,
bool useExternalEmojis = false, bool connect = false, bool speak = false, bool muteMembers = false, bool deafenMembers = false,
bool moveMembers = false, bool useVoiceActivation = false, bool manageRoles = false, bool manageWebhooks = false)
: this(0, createInstantInvite, manageChannel, addReactions, readMessages, sendMessages, sendTTSMessages, manageMessages,
: this(0, createInstantInvite, manageChannel, addReactions, viewChannel, sendMessages, sendTTSMessages, manageMessages,
embedLinks, attachFiles, readMessageHistory, mentionEveryone, useExternalEmojis, connect,
speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation, manageRoles, manageWebhooks)
{ }

/// <summary> Creates a new ChannelPermissions from this one, changing the provided non-null permissions. </summary>
public ChannelPermissions Modify(bool? createInstantInvite = null, bool? manageChannel = null,
bool? addReactions = null,
bool? readMessages = null, bool? sendMessages = null, bool? sendTTSMessages = null, bool? manageMessages = null,
bool? viewChannel = null, bool? sendMessages = null, bool? sendTTSMessages = null, bool? manageMessages = null,
bool? embedLinks = null, bool? attachFiles = null, bool? readMessageHistory = null, bool? mentionEveryone = null,
bool useExternalEmojis = false, bool? connect = null, bool? speak = null, bool? muteMembers = null, bool? deafenMembers = null,
bool? moveMembers = null, bool? useVoiceActivation = null, bool? manageRoles = null, bool? manageWebhooks = null)
=> new ChannelPermissions(RawValue, createInstantInvite, manageChannel, addReactions, readMessages, sendMessages, sendTTSMessages, manageMessages,
=> new ChannelPermissions(RawValue, createInstantInvite, manageChannel, addReactions, viewChannel, sendMessages, sendTTSMessages, manageMessages,
embedLinks, attachFiles, readMessageHistory, mentionEveryone, useExternalEmojis, connect,
speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation, manageRoles, manageWebhooks);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Discord
Expand Down Expand Up @@ -27,7 +28,10 @@ public static OverwritePermissions DenyAll(IChannel channel)
/// <summary> If Allowed, a user may add reactions. </summary>
public PermValue AddReactions => Permissions.GetValue(AllowValue, DenyValue, ChannelPermission.AddReactions);
/// <summary> If Allowed, a user may join channels. </summary>
public PermValue ReadMessages => Permissions.GetValue(AllowValue, DenyValue, ChannelPermission.ReadMessages);
[Obsolete("Use ViewChannel instead.")]
public PermValue ReadMessages => ViewChannel;
/// <summary> If Allowed, a user may join channels. </summary>
public PermValue ViewChannel => Permissions.GetValue(AllowValue, DenyValue, ChannelPermission.ViewChannel);
/// <summary> If Allowed, a user may send messages. </summary>
public PermValue SendMessages => Permissions.GetValue(AllowValue, DenyValue, ChannelPermission.SendMessages);
/// <summary> If Allowed, a user may send text-to-speech messages. </summary>
Expand Down Expand Up @@ -72,7 +76,7 @@ public OverwritePermissions(ulong allowValue, ulong denyValue)

private OverwritePermissions(ulong allowValue, ulong denyValue, PermValue? createInstantInvite = null, PermValue? manageChannel = null,
PermValue? addReactions = null,
PermValue? readMessages = null, PermValue? sendMessages = null, PermValue? sendTTSMessages = null, PermValue? manageMessages = null,
PermValue? viewChannel = null, PermValue? sendMessages = null, PermValue? sendTTSMessages = null, PermValue? manageMessages = null,
PermValue? embedLinks = null, PermValue? attachFiles = null, PermValue? readMessageHistory = null, PermValue? mentionEveryone = null,
PermValue? useExternalEmojis = null, PermValue? connect = null, PermValue? speak = null, PermValue? muteMembers = null,
PermValue? deafenMembers = null, PermValue? moveMembers = null, PermValue? useVoiceActivation = null, PermValue? manageRoles = null,
Expand All @@ -81,7 +85,7 @@ private OverwritePermissions(ulong allowValue, ulong denyValue, PermValue? creat
Permissions.SetValue(ref allowValue, ref denyValue, createInstantInvite, ChannelPermission.CreateInstantInvite);
Permissions.SetValue(ref allowValue, ref denyValue, manageChannel, ChannelPermission.ManageChannels);
Permissions.SetValue(ref allowValue, ref denyValue, addReactions, ChannelPermission.AddReactions);
Permissions.SetValue(ref allowValue, ref denyValue, readMessages, ChannelPermission.ReadMessages);
Permissions.SetValue(ref allowValue, ref denyValue, viewChannel, ChannelPermission.ViewChannel);
Permissions.SetValue(ref allowValue, ref denyValue, sendMessages, ChannelPermission.SendMessages);
Permissions.SetValue(ref allowValue, ref denyValue, sendTTSMessages, ChannelPermission.SendTTSMessages);
Permissions.SetValue(ref allowValue, ref denyValue, manageMessages, ChannelPermission.ManageMessages);
Expand Down
2 changes: 1 addition & 1 deletion src/Discord.Net.Core/Utils/Permissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static ulong ResolveChannel(IGuild guild, IGuildUser user, IGuildChannel

if (channel is ITextChannel textChannel)
{
if (!GetValue(resolvedPermissions, ChannelPermission.ReadMessages))
if (!GetValue(resolvedPermissions, ChannelPermission.ViewChannel))
{
//No read permission on a text channel removes all other permissions
resolvedPermissions = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public static async Task<RestGuildUser> GetUserAsync(IGuildChannel channel, IGui
if (model == null)
return null;
var user = RestGuildUser.Create(client, guild, model);
if (!user.GetPermissions(channel).ReadMessages)
if (!user.GetPermissions(channel).ViewChannel)
return null;

return user;
Expand All @@ -265,7 +265,7 @@ public static IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync
var models = await client.ApiClient.GetGuildMembersAsync(guild.Id, args, options).ConfigureAwait(false);
return models
.Select(x => RestGuildUser.Create(client, guild, x))
.Where(x => x.GetPermissions(channel).ReadMessages)
.Where(x => x.GetPermissions(channel).ViewChannel)
.ToImmutableArray();
},
nextPage: (info, lastPage) =>
Expand Down
2 changes: 1 addition & 1 deletion src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public async Task<RestTextChannel> GetDefaultChannelAsync(RequestOptions options
var channels = await GetTextChannelsAsync(options).ConfigureAwait(false);
var user = await GetCurrentUserAsync(options).ConfigureAwait(false);
return channels
.Where(c => user.GetPermissions(c).ReadMessages)
.Where(c => user.GetPermissions(c).ViewChannel)
.OrderBy(c => c.Position)
.FirstOrDefault();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SocketTextChannel : SocketGuildChannel, ITextChannel, ISocketMessag
public override IReadOnlyCollection<SocketGuildUser> Users
=> Guild.Users.Where(x => Permissions.GetValue(
Permissions.ResolveChannel(Guild, x, this, Permissions.ResolveGuild(Guild, x)),
ChannelPermission.ReadMessages)).ToImmutableArray();
ChannelPermission.ViewChannel)).ToImmutableArray();

internal SocketTextChannel(DiscordSocketClient discord, ulong id, SocketGuild guild)
: base(discord, id, guild)
Expand Down Expand Up @@ -107,7 +107,7 @@ public override SocketGuildUser GetUser(ulong id)
{
var guildPerms = Permissions.ResolveGuild(Guild, user);
var channelPerms = Permissions.ResolveChannel(Guild, user, this, guildPerms);
if (Permissions.GetValue(channelPerms, ChannelPermission.ReadMessages))
if (Permissions.GetValue(channelPerms, ChannelPermission.ViewChannel))
return user;
}
return null;
Expand Down
Loading

0 comments on commit edfbd05

Please sign in to comment.