Ejemplo n.º 1
0
        /// <summary>
        /// Tests that flag of the given permissions value to be the expected output
        /// and then tries cycling through the states of the allow and deny values
        /// for that flag
        /// </summary>
        /// <param name="value"></param>
        /// <param name="flag"></param>
        /// <param name="expected"></param>
        private void TestHelper(OverwritePermissions value, ChannelPermission flag, PermValue expected)
        {
            // check that the value matches
            Assert.Equal(expected, Permissions.GetValue(value.AllowValue, value.DenyValue, flag));

            // check toggling bits for both allow and deny
            // have to make copies to get around read only property
            ulong allow = value.AllowValue;
            ulong deny  = value.DenyValue;

            // both unset should be inherit
            Permissions.UnsetFlag(ref allow, (ulong)flag);
            Permissions.UnsetFlag(ref deny, (ulong)flag);
            Assert.Equal(PermValue.Inherit, Permissions.GetValue(allow, deny, flag));

            // allow set should be allow
            Permissions.SetFlag(ref allow, (ulong)flag);
            Permissions.UnsetFlag(ref deny, (ulong)flag);
            Assert.Equal(PermValue.Allow, Permissions.GetValue(allow, deny, flag));

            // deny should be deny
            Permissions.UnsetFlag(ref allow, (ulong)flag);
            Permissions.SetFlag(ref deny, (ulong)flag);
            Assert.Equal(PermValue.Deny, Permissions.GetValue(allow, deny, flag));

            // allow takes precedence
            Permissions.SetFlag(ref allow, (ulong)flag);
            Permissions.SetFlag(ref deny, (ulong)flag);
            Assert.Equal(PermValue.Allow, Permissions.GetValue(allow, deny, flag));
        }
Ejemplo n.º 2
0
 /// <summary> Creates a new ChannelPermissions with the provided permissions. </summary>
 public OverwritePermissions(
     PermValue createInstantInvite = PermValue.Inherit,
     PermValue manageChannel       = PermValue.Inherit,
     PermValue addReactions        = PermValue.Inherit,
     PermValue readMessages        = PermValue.Inherit,
     PermValue sendMessages        = PermValue.Inherit,
     PermValue sendTTSMessages     = PermValue.Inherit,
     PermValue manageMessages      = PermValue.Inherit,
     PermValue embedLinks          = PermValue.Inherit,
     PermValue attachFiles         = PermValue.Inherit,
     PermValue readMessageHistory  = PermValue.Inherit,
     PermValue mentionEveryone     = PermValue.Inherit,
     PermValue useExternalEmojis   = PermValue.Inherit,
     PermValue connect             = PermValue.Inherit,
     PermValue speak              = PermValue.Inherit,
     PermValue muteMembers        = PermValue.Inherit,
     PermValue deafenMembers      = PermValue.Inherit,
     PermValue moveMembers        = PermValue.Inherit,
     PermValue useVoiceActivation = PermValue.Inherit,
     PermValue manageRoles        = PermValue.Inherit,
     PermValue manageWebhooks     = PermValue.Inherit)
     : this(0, 0, createInstantInvite, manageChannel, addReactions, readMessages, sendMessages, sendTTSMessages, manageMessages,
            embedLinks, attachFiles, readMessageHistory, mentionEveryone, useExternalEmojis, connect, speak, muteMembers, deafenMembers,
            moveMembers, useVoiceActivation, manageRoles, manageWebhooks)
 {
 }
Ejemplo n.º 3
0
        public async Task LockModule()
        {
            IGuildChannel channel = (IGuildChannel)Context.Channel;

            PermValue permission        = channel.GetPermissionOverwrite(Context.Guild.EveryoneRole)?.SendMessages ?? PermValue.Allow;
            PermValue flippedPermission = permission == PermValue.Allow ? PermValue.Deny : PermValue.Allow;

            await ReplyAsync(flippedPermission == PermValue.Allow? "> Channel Has Been Unlocked" : "> Channel Has Been Locked");

            await channel.AddPermissionOverwriteAsync(Context.Guild.EveryoneRole, new OverwritePermissions(sendMessages : flippedPermission));
        }
Ejemplo n.º 4
0
        private void InitiailzeTeamIdToName()
        {
            lock (this.teamIdToNameLock)
            {
                OverwritePermissions?everyonePermissions     = this.Channel.GetPermissionOverwrite(this.Guild.EveryoneRole);
                PermValue            everyoneViewPermissions = everyonePermissions?.ViewChannel ?? PermValue.Inherit;
                PermValue            everyoneSendPermissions = everyonePermissions?.SendMessages ?? PermValue.Inherit;

                IEnumerable <IRole> teamRoles = this.Guild.Roles
                                                .Where(role => role.Name.StartsWith(this.TeamRolePrefix, StringComparison.InvariantCultureIgnoreCase));

                this.ServerTeamIdToName = teamRoles
                                          .ToDictionary(
                    role => role.Id.ToString(CultureInfo.InvariantCulture),
                    role => role.Name.Substring(this.TeamRolePrefix.Length).Trim());

                this.ChannelTeamIdToName = teamRoles
                                           .Where(role =>
                {
                    // Players need both View and Send permissions to play, so make sure either the role or the
                    // everyone role has it
                    OverwritePermissions?permissions = this.Channel.GetPermissionOverwrite(role);
                    if (!permissions.HasValue)
                    {
                        // No specific permissions, so inherit it from everyone
                        return(everyonePermissions?.ViewChannel != PermValue.Deny &&
                               everyonePermissions?.SendMessages != PermValue.Deny);
                    }

                    OverwritePermissions permissionsValue = permissions.Value;
                    PermValue viewPermissions             = permissionsValue.ViewChannel != PermValue.Inherit ?
                                                            permissionsValue.ViewChannel :
                                                            everyoneViewPermissions;
                    PermValue sendPermissions = permissionsValue.SendMessages != PermValue.Inherit ?
                                                permissionsValue.SendMessages :
                                                everyoneSendPermissions;

                    return(viewPermissions != PermValue.Deny && sendPermissions != PermValue.Deny);
                })
                                           .ToDictionary(
                    role => role.Id.ToString(CultureInfo.InvariantCulture),
                    role => role.Name.Substring(this.TeamRolePrefix.Length).Trim());
            }
        }
Ejemplo n.º 5
0
        public async override Task Set()
        {
            var existing = Channel.GetPermissionOverwrite(Target);

            var overwrite = existing.GetValueOrDefault(new OverwritePermissions());

            Prior     = overwrite.SendMessages;
            overwrite = overwrite.Modify(sendMessages: PermValue.Deny);
            await Channel.AddPermissionOverwriteAsync(Target, overwrite, new RequestOptions()
            {
                AuditLogReason = "User muted from this channel specifically"
            });

            var txt = await Service.GetAdminChannel(Guild);

            await txt.SendMessageAsync(embed : GetBuilder()
                                       .WithTitle("User Channel Muted")
                                       .Build());
        }
 /// <summary>
 ///     Initializes a new <see cref="ChannelPermissions"/> struct with the provided permissions.
 /// </summary>
 public OverwritePermissions(
     PermValue createInstantInvite = PermValue.Inherit,
     PermValue manageChannel       = PermValue.Inherit,
     PermValue addReactions        = PermValue.Inherit,
     PermValue viewChannel         = PermValue.Inherit,
     PermValue sendMessages        = PermValue.Inherit,
     PermValue sendTTSMessages     = PermValue.Inherit,
     PermValue manageMessages      = PermValue.Inherit,
     PermValue embedLinks          = PermValue.Inherit,
     PermValue attachFiles         = PermValue.Inherit,
     PermValue readMessageHistory  = PermValue.Inherit,
     PermValue mentionEveryone     = PermValue.Inherit,
     PermValue useExternalEmojis   = PermValue.Inherit,
     PermValue connect             = PermValue.Inherit,
     PermValue speak                   = PermValue.Inherit,
     PermValue muteMembers             = PermValue.Inherit,
     PermValue deafenMembers           = PermValue.Inherit,
     PermValue moveMembers             = PermValue.Inherit,
     PermValue useVoiceActivation      = PermValue.Inherit,
     PermValue manageRoles             = PermValue.Inherit,
     PermValue manageWebhooks          = PermValue.Inherit,
     PermValue prioritySpeaker         = PermValue.Inherit,
     PermValue stream                  = PermValue.Inherit,
     PermValue useSlashCommands        = PermValue.Inherit,
     PermValue useApplicationCommands  = PermValue.Inherit,
     PermValue requestToSpeak          = PermValue.Inherit,
     PermValue manageThreads           = PermValue.Inherit,
     PermValue createPublicThreads     = PermValue.Inherit,
     PermValue createPrivateThreads    = PermValue.Inherit,
     PermValue usePublicThreads        = PermValue.Inherit,
     PermValue usePrivateThreads       = PermValue.Inherit,
     PermValue useExternalStickers     = PermValue.Inherit,
     PermValue sendMessagesInThreads   = PermValue.Inherit,
     PermValue startEmbeddedActivities = PermValue.Inherit)
     : this(0, 0, createInstantInvite, manageChannel, addReactions, viewChannel, sendMessages, sendTTSMessages, manageMessages,
            embedLinks, attachFiles, readMessageHistory, mentionEveryone, useExternalEmojis, connect, speak, muteMembers, deafenMembers,
            moveMembers, useVoiceActivation, manageRoles, manageWebhooks, prioritySpeaker, stream, useSlashCommands, useApplicationCommands,
            requestToSpeak, manageThreads, createPublicThreads, createPrivateThreads, usePublicThreads, usePrivateThreads, useExternalStickers,
            sendMessagesInThreads, startEmbeddedActivities)
 {
 }
Ejemplo n.º 7
0
 public static void SetValue(ref uint allow, ref uint deny, PermValue? value, PermissionBits bit)
 {
     if (value.HasValue)
     {
         switch (value)
         {
             case PermValue.Allow:
                 SetBit(ref allow, bit);
                 UnsetBit(ref deny, bit);
                 break;
             case PermValue.Deny:
                 UnsetBit(ref allow, bit);
                 SetBit(ref deny, bit);
                 break;
             default:
                 UnsetBit(ref allow, bit);
                 UnsetBit(ref deny, bit);
                 break;
         }
     }
 }
Ejemplo n.º 8
0
        public ChannelPermissionOverrides(ChannelPermissionOverrides basePerms, PermValue? createInstantInvite = null, 
            PermValue? manageChannel = null, PermValue? readMessages = null, PermValue? sendMessages = null, PermValue? sendTTSMessages = null,
            PermValue? manageMessages = null, PermValue? embedLinks = null, PermValue? attachFiles = null, PermValue? readMessageHistory = null,
            PermValue? mentionEveryone = null, PermValue? connect = null, PermValue? speak = null, PermValue? muteMembers = null, PermValue? deafenMembers = null,
            PermValue? moveMembers = null, PermValue? useVoiceActivation = null, PermValue? changeNickname = null, PermValue? manageNicknames = null, 
            PermValue? managePermissions = null)
        {
            uint allow = basePerms.AllowValue, deny = basePerms.DenyValue;

            PermissionsHelper.SetValue(ref allow, ref deny, createInstantInvite, PermissionBits.CreateInstantInvite);
            PermissionsHelper.SetValue(ref allow, ref deny, manageChannel, PermissionBits.ManageChannel);
            PermissionsHelper.SetValue(ref allow, ref deny, readMessages, PermissionBits.ReadMessages);
            PermissionsHelper.SetValue(ref allow, ref deny, sendMessages, PermissionBits.SendMessages);
            PermissionsHelper.SetValue(ref allow, ref deny, sendTTSMessages, PermissionBits.SendTTSMessages);
            PermissionsHelper.SetValue(ref allow, ref deny, manageMessages, PermissionBits.ManageMessages);
            PermissionsHelper.SetValue(ref allow, ref deny, embedLinks, PermissionBits.EmbedLinks);
            PermissionsHelper.SetValue(ref allow, ref deny, attachFiles, PermissionBits.AttachFiles);
            PermissionsHelper.SetValue(ref allow, ref deny, readMessageHistory, PermissionBits.ReadMessageHistory);
            PermissionsHelper.SetValue(ref allow, ref deny, mentionEveryone, PermissionBits.MentionEveryone);
            PermissionsHelper.SetValue(ref allow, ref deny, connect, PermissionBits.Connect);
            PermissionsHelper.SetValue(ref allow, ref deny, speak, PermissionBits.Speak);
            PermissionsHelper.SetValue(ref allow, ref deny, muteMembers, PermissionBits.MuteMembers);
            PermissionsHelper.SetValue(ref allow, ref deny, deafenMembers, PermissionBits.DeafenMembers);
            PermissionsHelper.SetValue(ref allow, ref deny, moveMembers, PermissionBits.MoveMembers);
            PermissionsHelper.SetValue(ref allow, ref deny, useVoiceActivation, PermissionBits.UseVoiceActivation);
            PermissionsHelper.SetValue(ref allow, ref deny, changeNickname, PermissionBits.ChangeNickname);
            PermissionsHelper.SetValue(ref allow, ref deny, manageNicknames, PermissionBits.ManageNicknames);
            PermissionsHelper.SetValue(ref allow, ref deny, managePermissions, PermissionBits.ManageRolesOrPermissions);

            AllowValue = allow;
            DenyValue = deny;
        }
Ejemplo n.º 9
0
 public ChannelPermissionOverrides(PermValue? createInstantInvite = null, 
     PermValue? manageChannel = null, PermValue? readMessages = null, PermValue? sendMessages = null, PermValue? sendTTSMessages = null,
     PermValue? manageMessages = null, PermValue? embedLinks = null, PermValue? attachFiles = null, PermValue? readMessageHistory = null,
     PermValue? mentionEveryone = null, PermValue? connect = null, PermValue? speak = null, PermValue? muteMembers = null, PermValue? deafenMembers = null,
     PermValue? moveMembers = null, PermValue? useVoiceActivation = null, PermValue? changeNickname = null, PermValue? manageNicknames = null,
     PermValue? managePermissions = null)
     : this(new ChannelPermissionOverrides(), createInstantInvite, manageChannel, readMessages, sendMessages, sendTTSMessages,
           manageMessages, embedLinks, attachFiles, readMessageHistory, mentionEveryone, connect, speak, muteMembers, deafenMembers, 
           moveMembers, useVoiceActivation, managePermissions)
 {
 }