public void Modify() { // asserts that a channel permission flag value can be checked // and that modify can set and unset each flag // and that ToList performs as expected void AssertUtil(ChannelPermission permission, Func <ChannelPermissions, bool> has, Func <ChannelPermissions, bool, ChannelPermissions> modify) { var perm = new ChannelPermissions(); // ensure permission initially false // use both the function and Has to ensure that the GetPermission // function is working Assert.False(has(perm)); Assert.False(perm.Has(permission)); // enable it, and ensure that it gets set perm = modify(perm, true); Assert.True(has(perm)); Assert.True(perm.Has(permission)); // check ToList behavior var list = perm.ToList(); Assert.Contains(permission, list); Assert.Single(list); // set it false again perm = modify(perm, false); Assert.False(has(perm)); Assert.False(perm.Has(permission)); // ensure that no perms are set now Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); } AssertUtil(ChannelPermission.CreateInstantInvite, x => x.CreateInstantInvite, (p, enable) => p.Modify(createInstantInvite: enable)); AssertUtil(ChannelPermission.ManageChannels, x => x.ManageChannel, (p, enable) => p.Modify(manageChannel: enable)); AssertUtil(ChannelPermission.AddReactions, x => x.AddReactions, (p, enable) => p.Modify(addReactions: enable)); AssertUtil(ChannelPermission.ViewChannel, x => x.ViewChannel, (p, enable) => p.Modify(viewChannel: enable)); AssertUtil(ChannelPermission.SendMessages, x => x.SendMessages, (p, enable) => p.Modify(sendMessages: enable)); AssertUtil(ChannelPermission.SendTTSMessages, x => x.SendTTSMessages, (p, enable) => p.Modify(sendTTSMessages: enable)); AssertUtil(ChannelPermission.ManageMessages, x => x.ManageMessages, (p, enable) => p.Modify(manageMessages: enable)); AssertUtil(ChannelPermission.EmbedLinks, x => x.EmbedLinks, (p, enable) => p.Modify(embedLinks: enable)); AssertUtil(ChannelPermission.AttachFiles, x => x.AttachFiles, (p, enable) => p.Modify(attachFiles: enable)); AssertUtil(ChannelPermission.ReadMessageHistory, x => x.ReadMessageHistory, (p, enable) => p.Modify(readMessageHistory: enable)); AssertUtil(ChannelPermission.MentionEveryone, x => x.MentionEveryone, (p, enable) => p.Modify(mentionEveryone: enable)); AssertUtil(ChannelPermission.UseExternalEmojis, x => x.UseExternalEmojis, (p, enable) => p.Modify(useExternalEmojis: enable)); AssertUtil(ChannelPermission.Connect, x => x.Connect, (p, enable) => p.Modify(connect: enable)); AssertUtil(ChannelPermission.Speak, x => x.Speak, (p, enable) => p.Modify(speak: enable)); AssertUtil(ChannelPermission.MuteMembers, x => x.MuteMembers, (p, enable) => p.Modify(muteMembers: enable)); AssertUtil(ChannelPermission.DeafenMembers, x => x.DeafenMembers, (p, enable) => p.Modify(deafenMembers: enable)); AssertUtil(ChannelPermission.MoveMembers, x => x.MoveMembers, (p, enable) => p.Modify(moveMembers: enable)); AssertUtil(ChannelPermission.UseVAD, x => x.UseVAD, (p, enable) => p.Modify(useVoiceActivation: enable)); AssertUtil(ChannelPermission.ManageRoles, x => x.ManageRoles, (p, enable) => p.Modify(manageRoles: enable)); AssertUtil(ChannelPermission.ManageWebhooks, x => x.ManageWebhooks, (p, enable) => p.Modify(manageWebhooks: enable)); AssertUtil(ChannelPermission.PrioritySpeaker, x => x.PrioritySpeaker, (p, enable) => p.Modify(prioritySpeaker: enable)); AssertUtil(ChannelPermission.Stream, x => x.Stream, (p, enable) => p.Modify(stream: enable)); }
public void RawValueConstructor() { // returns all of the values that will be tested // a Theory cannot be used here, because these values are not all constants IEnumerable <ulong> GetTestValues() { yield return(0); yield return(ChannelPermissions.Category.RawValue); yield return(ChannelPermissions.DM.RawValue); yield return(ChannelPermissions.Group.RawValue); yield return(ChannelPermissions.None.RawValue); yield return(ChannelPermissions.Text.RawValue); yield return(ChannelPermissions.Voice.RawValue); }; foreach (var rawValue in GetTestValues()) { var p = new ChannelPermissions(rawValue); Assert.Equal(rawValue, p.RawValue); } }
public ChannelPermissions(ChannelPermissions basePerms, bool?createInstantInvite = null, bool?managePermissions = null, bool?manageChannel = null, bool?readMessages = null, bool?sendMessages = null, bool?sendTTSMessages = null, bool?manageMessages = null, bool?embedLinks = null, bool?attachFiles = null, bool?readMessageHistory = null, bool?mentionEveryone = null, bool?connect = null, bool?speak = null, bool?muteMembers = null, bool?deafenMembers = null, bool?moveMembers = null, bool?useVoiceActivation = null) { uint value = basePerms.RawValue; PermissionsHelper.SetValue(ref value, createInstantInvite, PermissionBits.CreateInstantInvite); PermissionsHelper.SetValue(ref value, managePermissions, PermissionBits.ManageRolesOrPermissions); PermissionsHelper.SetValue(ref value, manageChannel, PermissionBits.ManageChannel); PermissionsHelper.SetValue(ref value, readMessages, PermissionBits.ReadMessages); PermissionsHelper.SetValue(ref value, sendMessages, PermissionBits.SendMessages); PermissionsHelper.SetValue(ref value, sendTTSMessages, PermissionBits.SendTTSMessages); PermissionsHelper.SetValue(ref value, manageMessages, PermissionBits.ManageMessages); PermissionsHelper.SetValue(ref value, embedLinks, PermissionBits.EmbedLinks); PermissionsHelper.SetValue(ref value, attachFiles, PermissionBits.AttachFiles); PermissionsHelper.SetValue(ref value, readMessageHistory, PermissionBits.ReadMessageHistory); PermissionsHelper.SetValue(ref value, mentionEveryone, PermissionBits.MentionEveryone); PermissionsHelper.SetValue(ref value, connect, PermissionBits.Connect); PermissionsHelper.SetValue(ref value, speak, PermissionBits.Speak); PermissionsHelper.SetValue(ref value, muteMembers, PermissionBits.MuteMembers); PermissionsHelper.SetValue(ref value, deafenMembers, PermissionBits.DeafenMembers); PermissionsHelper.SetValue(ref value, moveMembers, PermissionBits.MoveMembers); PermissionsHelper.SetValue(ref value, useVoiceActivation, PermissionBits.UseVoiceActivation); RawValue = value; }
public void TestChannelTypeResolution() { ITextChannel someChannel = null; // null channels will throw exception Assert.Throws <ArgumentException>(() => ChannelPermissions.All(someChannel)); }
public void DefaultConstructor() { var permission = new ChannelPermissions(); Assert.Equal((ulong)0, permission.RawValue); Assert.Equal(ChannelPermissions.None.RawValue, permission.RawValue); }
public ChannelPermissions GetPermissions(User user) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (_users != null) { Member member; if (_users.TryGetValue(user.Id, out member)) { return(member.Permissions); } else { return(ChannelPermissions.None); } } else { ChannelPermissions perms = new ChannelPermissions(); ResolvePermissions(user, ref perms); return(perms); } }
public void ChannelTypeResolution_Null() { Assert.Throws <ArgumentException>(() => { ChannelPermissions.All(null); }); }
public Task TestChannelTypeResolution() { ITextChannel someChannel = null; // null channels will throw exception Assert.Throws <ArgumentException>(() => ChannelPermissions.All(someChannel)); return(Task.CompletedTask); }
public Task SetChannelPermissions(Channel channel, Role role, ChannelPermissions allow = null, ChannelPermissions deny = null) { if (channel == null) throw new ArgumentNullException(nameof(channel)); if (role == null) throw new ArgumentNullException(nameof(role)); CheckReady(); return SetChannelPermissions(channel, role?.Id, PermissionTarget.Role, allow, deny); }
public Task SetChannelPermissions(Channel channel, User user, ChannelPermissions allow = null, ChannelPermissions deny = null) { if (channel == null) throw new ArgumentNullException(nameof(channel)); if (user == null) throw new ArgumentNullException(nameof(user)); CheckReady(); return SetChannelPermissions(channel, user?.Id, PermissionTarget.User, allow, deny); }
public Task AddPermissionsRule(Role role, ChannelPermissions allow, ChannelPermissions deny) { if (role == null) { throw new ArgumentNullException(nameof(role)); } return(AddPermissionsRule(role.Id, PermissionTarget.Role, allow.RawValue, deny.RawValue)); }
internal PermissionOverwrite(PermissionTarget targetType, string targetId, uint allow, uint deny) { TargetType = targetType; TargetId = targetId; Allow = new ChannelPermissions(allow); Allow.Lock(); Deny = new ChannelPermissions(deny); Deny.Lock(); }
internal PermissionOverwrite(PermissionTarget targetType, string targetId, uint allow, uint deny) { TargetType = targetType; TargetId = targetId; Allow = new ChannelPermissions(allow); Allow.Lock(); Deny = new ChannelPermissions(deny); Deny.Lock(); }
public Task AddPermissionsRule(User user, ChannelPermissions allow, ChannelPermissions deny) { if (user == null) { throw new ArgumentNullException(nameof(user)); } return(AddPermissionsRule(user.Id, PermissionTarget.User, allow.RawValue, deny.RawValue)); }
internal void AddChannel(Channel channel) { if (_server.Id != null) { var perms = new ChannelPermissions(); perms.Lock(); _channels.TryAdd(channel.Id, channel); _permissions.TryAdd(channel.Id, perms); UpdateChannelPermissions(channel); } }
public Task TestChannelPermissionModify() { // test that channel permissions could be modified correctly var perm = new ChannelPermissions(); void Check(ChannelPermission permission, Func <ChannelPermissions, bool> has, Func <ChannelPermissions, bool, ChannelPermissions> modify) { // ensure permission initially false // use both the function and Has to ensure that the GetPermission // function is working Assert.False(has(perm)); Assert.False(perm.Has(permission)); // enable it, and ensure that it gets set perm = modify(perm, true); Assert.True(has(perm)); Assert.True(perm.Has(permission)); // set it false again perm = modify(perm, false); Assert.False(has(perm)); Assert.False(perm.Has(permission)); // ensure that no perms are set now Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); } Check(ChannelPermission.CreateInstantInvite, x => x.CreateInstantInvite, (p, enable) => p.Modify(createInstantInvite: enable)); Check(ChannelPermission.ManageChannels, x => x.ManageChannel, (p, enable) => p.Modify(manageChannel: enable)); Check(ChannelPermission.AddReactions, x => x.AddReactions, (p, enable) => p.Modify(addReactions: enable)); Check(ChannelPermission.ViewChannel, x => x.ViewChannel, (p, enable) => p.Modify(viewChannel: enable)); Check(ChannelPermission.SendMessages, x => x.SendMessages, (p, enable) => p.Modify(sendMessages: enable)); Check(ChannelPermission.SendTTSMessages, x => x.SendTTSMessages, (p, enable) => p.Modify(sendTTSMessages: enable)); Check(ChannelPermission.ManageMessages, x => x.ManageMessages, (p, enable) => p.Modify(manageMessages: enable)); Check(ChannelPermission.EmbedLinks, x => x.EmbedLinks, (p, enable) => p.Modify(embedLinks: enable)); Check(ChannelPermission.AttachFiles, x => x.AttachFiles, (p, enable) => p.Modify(attachFiles: enable)); Check(ChannelPermission.ReadMessageHistory, x => x.ReadMessageHistory, (p, enable) => p.Modify(readMessageHistory: enable)); Check(ChannelPermission.MentionEveryone, x => x.MentionEveryone, (p, enable) => p.Modify(mentionEveryone: enable)); Check(ChannelPermission.UseExternalEmojis, x => x.UseExternalEmojis, (p, enable) => p.Modify(useExternalEmojis: enable)); Check(ChannelPermission.Connect, x => x.Connect, (p, enable) => p.Modify(connect: enable)); Check(ChannelPermission.Speak, x => x.Speak, (p, enable) => p.Modify(speak: enable)); Check(ChannelPermission.MuteMembers, x => x.MuteMembers, (p, enable) => p.Modify(muteMembers: enable)); Check(ChannelPermission.DeafenMembers, x => x.DeafenMembers, (p, enable) => p.Modify(deafenMembers: enable)); Check(ChannelPermission.MoveMembers, x => x.MoveMembers, (p, enable) => p.Modify(moveMembers: enable)); Check(ChannelPermission.UseVAD, x => x.UseVAD, (p, enable) => p.Modify(useVoiceActivation: enable)); Check(ChannelPermission.ManageRoles, x => x.ManageRoles, (p, enable) => p.Modify(manageRoles: enable)); Check(ChannelPermission.ManageWebhooks, x => x.ManageWebhooks, (p, enable) => p.Modify(manageWebhooks: enable)); Check(ChannelPermission.PrioritySpeaker, x => x.PrioritySpeaker, (p, enable) => p.Modify(prioritySpeaker: enable)); return(Task.CompletedTask); }
private Task AddPermissionsRule(ulong targetId, PermissionTarget targetType, ChannelPermissions allow = null, ChannelPermissions deny = null) { var request = new AddChannelPermissionsRequest(Id) { TargetId = targetId, TargetType = targetType.Value, Allow = allow?.RawValue ?? 0, Deny = deny?.RawValue ?? 0 }; return(Client.ClientAPI.Send(request)); }
/*public static ulong ResolveChannel(IGuildUser user, IGuildChannel channel) * { * return ResolveChannel(user, channel, ResolveGuild(user)); * }*/ public static ulong ResolveChannel(IGuildUser user, IGuildChannel channel, ulong guildPermissions) { ulong resolvedPermissions = 0; ulong mask = ChannelPermissions.All(channel).RawValue; if (/*user.Id == user.Guild.OwnerId || */ GetValue(guildPermissions, GuildPermission.Administrator)) { resolvedPermissions = mask; //Owners and administrators always have all permissions } else { //Start with this user's guild permissions resolvedPermissions = guildPermissions; OverwritePermissions?perms; var roles = user.Roles; if (roles.Count > 0) { ulong deniedPermissions = 0UL, allowedPermissions = 0UL; foreach (var role in roles) { perms = channel.GetPermissionOverwrite(role); if (perms != null) { deniedPermissions |= perms.Value.DenyValue; allowedPermissions |= perms.Value.AllowValue; } } resolvedPermissions = (resolvedPermissions & ~deniedPermissions) | allowedPermissions; } perms = channel.GetPermissionOverwrite(user); if (perms != null) { resolvedPermissions = (resolvedPermissions & ~perms.Value.DenyValue) | perms.Value.AllowValue; } //TODO: C#7 Typeswitch candidate var textChannel = channel as ITextChannel; var voiceChannel = channel as IVoiceChannel; if (textChannel != null && !GetValue(resolvedPermissions, ChannelPermission.ReadMessages)) { resolvedPermissions = 0; //No read permission on a text channel removes all other permissions } else if (voiceChannel != null && !GetValue(resolvedPermissions, ChannelPermission.Connect)) { resolvedPermissions = 0; //No connect permission on a voice channel removes all other permissions } resolvedPermissions &= mask; //Ensure we didnt get any permissions this channel doesnt support (from guildPerms, for example) } return(resolvedPermissions); }
public Task SetChannelPermissions(Channel channel, Role role, ChannelPermissions allow = null, ChannelPermissions deny = null) { if (channel == null) { throw new ArgumentNullException(nameof(channel)); } if (role == null) { throw new ArgumentNullException(nameof(role)); } CheckReady(); return(SetChannelPermissions(channel, role?.Id, PermissionTarget.Role, allow, deny)); }
internal void AddUser(User user) { if (!Client.Config.UsePermissionsCache) { return; } var perms = new ChannelPermissions(); UpdatePermissions(user, ref perms); var member = new Member(user, ChannelPermissions.None); _users[user.Id] = new Member(user, ChannelPermissions.None); }
public Task SetChannelPermissions(Channel channel, User user, ChannelPermissions allow = null, ChannelPermissions deny = null) { if (channel == null) { throw new ArgumentNullException(nameof(channel)); } if (user == null) { throw new ArgumentNullException(nameof(user)); } CheckReady(); return(SetChannelPermissions(channel, user?.Id, PermissionTarget.User, allow, deny)); }
public void AddUser(User user) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (_users != null) { var perms = new ChannelPermissions(); ResolvePermissions(user, ref perms); var member = new Member(user, ChannelPermissions.None); _users[user.Id] = new Member(user, ChannelPermissions.None); } }
internal ChannelPermissions GetPermissions(User user) { if (Client.Config.UsePermissionsCache) { Member member; if (_users.TryGetValue(user.Id, out member)) { return(member.Permissions); } else { return(ChannelPermissions.None); } } else { ChannelPermissions perms = new ChannelPermissions(); UpdatePermissions(user, ref perms); return(perms); } }
public User GetUser(ulong id) { if (IsPrivate) { if (id == Recipient.Id) { return(Recipient); } else if (id == Client.PrivateUser.Id) { return(Client.PrivateUser); } } else if (!Client.Config.UsePermissionsCache) { var user = Server.GetUser(id); if (user != null) { ChannelPermissions perms = new ChannelPermissions(); UpdatePermissions(user, ref perms); if (perms.ReadMessages) { return(user); } } } else { Member result; if (_users.TryGetValue(id, out result)) { return(result.User); } } return(null); }
public void ChannelTypeResolution_Voice() { Assert.Equal(ChannelPermissions.Voice.RawValue, ChannelPermissions.All(new MockedVoiceChannel()).RawValue); }
public Member(User user, ChannelPermissions permissions) { User = user; Permissions = permissions; }
public void ChannelTypeResolution_Text() { Assert.Equal(ChannelPermissions.Text.RawValue, ChannelPermissions.All(new MockedTextChannel()).RawValue); }
private Task SetChannelPermissions(Channel channel, string targetId, PermissionTarget targetType, ChannelPermissions allow = null, ChannelPermissions deny = null) => _api.SetChannelPermissions(channel.Id, targetId, targetType.Value, allow?.RawValue ?? 0, deny?.RawValue ?? 0);
internal void AddUser(User user) { if (!Client.Config.UsePermissionsCache) return; var perms = new ChannelPermissions(); UpdatePermissions(user, ref perms); var member = new Member(user, ChannelPermissions.None); _users[user.Id] = new Member(user, ChannelPermissions.None); }
public static ChannelPermissions ToChannelPerms(IGuildChannel channel, ulong guildPermissions) => new ChannelPermissions(guildPermissions & ChannelPermissions.All(channel).RawValue);
/*public static ulong ResolveChannel(IGuildUser user, IGuildChannel channel) * { * return ResolveChannel(user, channel, ResolveGuild(user)); * }*/ public static ulong ResolveChannel(IGuild guild, IGuildUser user, IGuildChannel channel, ulong guildPermissions) { ulong mask = ChannelPermissions.All(channel).RawValue; ulong resolvedPermissions; if (GetValue(guildPermissions, GuildPermission.Administrator)) //Includes owner { resolvedPermissions = mask; //Owners and administrators always have all permissions } else { //Start with this user's guild permissions resolvedPermissions = guildPermissions; //Give/Take Everyone permissions var perms = channel.GetPermissionOverwrite(guild.EveryoneRole); if (perms != null) { resolvedPermissions = (resolvedPermissions & ~perms.Value.DenyValue) | perms.Value.AllowValue; } //Give/Take Role permissions ulong deniedPermissions = 0UL, allowedPermissions = 0UL; foreach (var roleId in user.RoleIds) { IRole role; if (roleId != guild.EveryoneRole.Id && (role = guild.GetRole(roleId)) != null) { perms = channel.GetPermissionOverwrite(role); if (perms != null) { allowedPermissions |= perms.Value.AllowValue; deniedPermissions |= perms.Value.DenyValue; } } } resolvedPermissions = (resolvedPermissions & ~deniedPermissions) | allowedPermissions; //Give/Take User permissions perms = channel.GetPermissionOverwrite(user); if (perms != null) { resolvedPermissions = (resolvedPermissions & ~perms.Value.DenyValue) | perms.Value.AllowValue; } if (channel is ITextChannel) { if (!GetValue(resolvedPermissions, ChannelPermission.ViewChannel)) { //No read permission on a text channel removes all other permissions resolvedPermissions = 0; } else if (!GetValue(resolvedPermissions, ChannelPermission.SendMessages)) { //No send permissions on a text channel removes all send-related permissions resolvedPermissions &= ~(ulong)ChannelPermission.SendTTSMessages; resolvedPermissions &= ~(ulong)ChannelPermission.MentionEveryone; resolvedPermissions &= ~(ulong)ChannelPermission.EmbedLinks; resolvedPermissions &= ~(ulong)ChannelPermission.AttachFiles; } } resolvedPermissions &= mask; //Ensure we didn't get any permissions this channel doesn't support (from guildPerms, for example) } return(resolvedPermissions); }
public Task TestChannelPermissionModify() { // test channel permission modify var perm = new ChannelPermissions(); // ensure that the permission is initially false Assert.False(perm.CreateInstantInvite); // ensure that when modified it works perm = perm.Modify(createInstantInvite: true); Assert.True(perm.CreateInstantInvite); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.CreateInstantInvite); // set false again, move on to next permission perm = perm.Modify(createInstantInvite: false); Assert.False(perm.CreateInstantInvite); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.ManageChannel); perm = perm.Modify(manageChannel: true); Assert.True(perm.ManageChannel); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.ManageChannels); perm = perm.Modify(manageChannel: false); Assert.False(perm.ManageChannel); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.AddReactions); perm = perm.Modify(addReactions: true); Assert.True(perm.AddReactions); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.AddReactions); perm = perm.Modify(addReactions: false); Assert.False(perm.AddReactions); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.ViewChannel); perm = perm.Modify(viewChannel: true); Assert.True(perm.ViewChannel); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.ViewChannel); perm = perm.Modify(viewChannel: false); Assert.False(perm.ViewChannel); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.SendMessages); perm = perm.Modify(sendMessages: true); Assert.True(perm.SendMessages); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.SendMessages); perm = perm.Modify(sendMessages: false); Assert.False(perm.SendMessages); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.SendTTSMessages); perm = perm.Modify(sendTTSMessages: true); Assert.True(perm.SendTTSMessages); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.SendTTSMessages); perm = perm.Modify(sendTTSMessages: false); Assert.False(perm.SendTTSMessages); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.ManageMessages); perm = perm.Modify(manageMessages: true); Assert.True(perm.ManageMessages); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.ManageMessages); perm = perm.Modify(manageMessages: false); Assert.False(perm.ManageMessages); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.EmbedLinks); perm = perm.Modify(embedLinks: true); Assert.True(perm.EmbedLinks); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.EmbedLinks); perm = perm.Modify(embedLinks: false); Assert.False(perm.EmbedLinks); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.AttachFiles); perm = perm.Modify(attachFiles: true); Assert.True(perm.AttachFiles); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.AttachFiles); perm = perm.Modify(attachFiles: false); Assert.False(perm.AttachFiles); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.ReadMessageHistory); perm = perm.Modify(readMessageHistory: true); Assert.True(perm.ReadMessageHistory); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.ReadMessageHistory); perm = perm.Modify(readMessageHistory: false); Assert.False(perm.ReadMessageHistory); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.MentionEveryone); perm = perm.Modify(mentionEveryone: true); Assert.True(perm.MentionEveryone); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.MentionEveryone); perm = perm.Modify(mentionEveryone: false); Assert.False(perm.MentionEveryone); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.UseExternalEmojis); perm = perm.Modify(useExternalEmojis: true); Assert.True(perm.UseExternalEmojis); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.UseExternalEmojis); perm = perm.Modify(useExternalEmojis: false); Assert.False(perm.UseExternalEmojis); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.Connect); perm = perm.Modify(connect: true); Assert.True(perm.Connect); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.Connect); perm = perm.Modify(connect: false); Assert.False(perm.Connect); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.Speak); perm = perm.Modify(speak: true); Assert.True(perm.Speak); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.Speak); perm = perm.Modify(speak: false); Assert.False(perm.Speak); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.MuteMembers); perm = perm.Modify(muteMembers: true); Assert.True(perm.MuteMembers); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.MuteMembers); perm = perm.Modify(muteMembers: false); Assert.False(perm.MuteMembers); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.DeafenMembers); perm = perm.Modify(deafenMembers: true); Assert.True(perm.DeafenMembers); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.DeafenMembers); perm = perm.Modify(deafenMembers: false); Assert.False(perm.DeafenMembers); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.MoveMembers); perm = perm.Modify(moveMembers: true); Assert.True(perm.MoveMembers); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.MoveMembers); perm = perm.Modify(moveMembers: false); Assert.False(perm.MoveMembers); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.UseVAD); perm = perm.Modify(useVoiceActivation: true); Assert.True(perm.UseVAD); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.UseVAD); perm = perm.Modify(useVoiceActivation: false); Assert.False(perm.UseVAD); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.ManageRoles); perm = perm.Modify(manageRoles: true); Assert.True(perm.ManageRoles); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.ManageRoles); perm = perm.Modify(manageRoles: false); Assert.False(perm.ManageRoles); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); // individual permission test Assert.False(perm.ManageWebhooks); perm = perm.Modify(manageWebhooks: true); Assert.True(perm.ManageWebhooks); Assert.Equal(perm.RawValue, (ulong)ChannelPermission.ManageWebhooks); perm = perm.Modify(manageWebhooks: false); Assert.False(perm.ManageWebhooks); Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue); return(Task.CompletedTask); }
public DualChannelPermissions(uint allow = 0, uint deny = 0) { Allow = new ChannelPermissions(allow); Deny = new ChannelPermissions(deny); }
public void ChannelTypeResolution_Group() { Assert.Equal(ChannelPermissions.Group.RawValue, ChannelPermissions.All(new MockedGroupChannel()).RawValue); }
public void ChannelTypeResolution_Category() { Assert.Equal(ChannelPermissions.Category.RawValue, ChannelPermissions.All(new MockedCategoryChannel()).RawValue); }
public ChannelPermissions(ChannelPermissions basePerms, bool? createInstantInvite = null, bool? manageChannel = null, bool? readMessages = null, bool? sendMessages = null, bool? sendTTSMessages = null, bool? manageMessages = null, bool? embedLinks = null, bool? attachFiles = null, bool? readMessageHistory = null, bool? mentionEveryone = null, bool? connect = null, bool? speak = null, bool? muteMembers = null, bool? deafenMembers = null, bool? moveMembers = null, bool? useVoiceActivation = null, bool? managePermissions = null) { uint value = basePerms.RawValue; PermissionsHelper.SetValue(ref value, createInstantInvite, PermissionBits.CreateInstantInvite); PermissionsHelper.SetValue(ref value, manageChannel, PermissionBits.ManageChannel); PermissionsHelper.SetValue(ref value, readMessages, PermissionBits.ReadMessages); PermissionsHelper.SetValue(ref value, sendMessages, PermissionBits.SendMessages); PermissionsHelper.SetValue(ref value, sendTTSMessages, PermissionBits.SendTTSMessages); PermissionsHelper.SetValue(ref value, manageMessages, PermissionBits.ManageMessages); PermissionsHelper.SetValue(ref value, embedLinks, PermissionBits.EmbedLinks); PermissionsHelper.SetValue(ref value, attachFiles, PermissionBits.AttachFiles); PermissionsHelper.SetValue(ref value, readMessageHistory, PermissionBits.ReadMessageHistory); PermissionsHelper.SetValue(ref value, mentionEveryone, PermissionBits.MentionEveryone); PermissionsHelper.SetValue(ref value, connect, PermissionBits.Connect); PermissionsHelper.SetValue(ref value, speak, PermissionBits.Speak); PermissionsHelper.SetValue(ref value, muteMembers, PermissionBits.MuteMembers); PermissionsHelper.SetValue(ref value, deafenMembers, PermissionBits.DeafenMembers); PermissionsHelper.SetValue(ref value, moveMembers, PermissionBits.MoveMembers); PermissionsHelper.SetValue(ref value, useVoiceActivation, PermissionBits.UseVoiceActivation); PermissionsHelper.SetValue(ref value, managePermissions, PermissionBits.ManageRolesOrPermissions); RawValue = value; }
public User GetUser(ulong id) { if (!Client.Config.UsePermissionsCache) { if (Server != null) { var user = Server.GetUser(id); if (user != null) { ChannelPermissions perms = new ChannelPermissions(); UpdatePermissions(user, perms); if (perms.ReadMessages) return user; } } else { if (id == Recipient.Id) return Recipient; else if (id == Client.PrivateUser.Id) return Client.PrivateUser; } return null; } Member result; _users.TryGetValue(id, out result); return result.User; }
private Task AddPermissionsRule(ulong targetId, PermissionTarget targetType, ChannelPermissions allow = null, ChannelPermissions deny = null) { var request = new AddChannelPermissionsRequest(Id) { TargetId = targetId, TargetType = targetType.Value, Allow = allow?.RawValue ?? 0, Deny = deny?.RawValue ?? 0 }; return Client.ClientAPI.Send(request); }
public Task AddPermissionsRule(Role role, ChannelPermissions allow = null, ChannelPermissions deny = null) { if (role == null) throw new ArgumentNullException(nameof(role)); return AddPermissionsRule(role.Id, PermissionTarget.Role, allow, deny); }
public Task AddPermissionsRule(User user, ChannelPermissions allow = null, ChannelPermissions deny = null) { if (user == null) throw new ArgumentNullException(nameof(user)); return AddPermissionsRule(user.Id, PermissionTarget.User, allow, deny); }
public void ChannelTypeResolution_DM() { Assert.Equal(ChannelPermissions.DM.RawValue, ChannelPermissions.All(new MockedDMChannel()).RawValue); }
public Member(User user) { User = user; Permissions = new ChannelPermissions(); Permissions.Lock(); }
public void ChannelTypeResolution_Invalid() { Assert.Throws <ArgumentException>(() => ChannelPermissions.All(new MockedInvalidChannel())); }
internal void UpdatePermissions(User user, ChannelPermissions permissions) { uint newPermissions = 0; var server = Server; //Load the mask of all permissions supported by this channel type var mask = ChannelPermissions.All(this).RawValue; if (server != null) { //Start with this user's server permissions newPermissions = server.GetPermissions(user).RawValue; if (IsPrivate || user == Server.Owner) newPermissions = mask; //Owners always have all permissions else { var channelOverwrites = PermissionOverwrites; var roles = user.Roles; foreach (var denyRole in channelOverwrites.Where(x => x.TargetType == PermissionTarget.Role && x.Permissions.Deny.RawValue != 0 && roles.Any(y => y.Id == x.TargetId))) newPermissions &= ~denyRole.Permissions.Deny.RawValue; foreach (var allowRole in channelOverwrites.Where(x => x.TargetType == PermissionTarget.Role && x.Permissions.Allow.RawValue != 0 && roles.Any(y => y.Id == x.TargetId))) newPermissions |= allowRole.Permissions.Allow.RawValue; foreach (var denyUser in channelOverwrites.Where(x => x.TargetType == PermissionTarget.User && x.TargetId == user.Id && x.Permissions.Deny.RawValue != 0)) newPermissions &= ~denyUser.Permissions.Deny.RawValue; foreach (var allowUser in channelOverwrites.Where(x => x.TargetType == PermissionTarget.User && x.TargetId == user.Id && x.Permissions.Allow.RawValue != 0)) newPermissions |= allowUser.Permissions.Allow.RawValue; if (newPermissions.HasBit((byte)PermissionsBits.ManageRolesOrPermissions)) newPermissions = mask; //ManageRolesOrPermissions gives all permisions else if (Type == ChannelType.Text && !newPermissions.HasBit((byte)PermissionsBits.ReadMessages)) newPermissions = 0; //No read permission on a text channel removes all other permissions else if (Type == ChannelType.Voice && !newPermissions.HasBit((byte)PermissionsBits.Connect)) newPermissions = 0; //No connect permissions on a voice channel removes all other permissions else newPermissions &= mask; //Ensure we didnt get any permissions this channel doesnt support (from serverPerms, for example) } } else newPermissions = mask; //Private messages always have all permissions if (newPermissions != permissions.RawValue) permissions.SetRawValueInternal(newPermissions); }
internal ChannelPermissions GetPermissions(User user) { if (Client.Config.UsePermissionsCache) { Member member; if (_users.TryGetValue(user.Id, out member)) return member.Permissions; else return null; } else { ChannelPermissions perms = new ChannelPermissions(); UpdatePermissions(user, perms); return perms; } }
internal void AddChannel(Channel channel) { if (_server.Id != null) { var perms = new ChannelPermissions(); perms.Lock(); _channels.TryAdd(channel.Id, channel); _permissions.TryAdd(channel.Id, perms); UpdateChannelPermissions(channel); } }