Example #1
0
        /// <summary>
        /// Creates an opt-in channel in the given guild.
        /// </summary>
        /// <param name="guildConnection">
        /// The connection to the guild this channel is being created in. May not be null.
        /// </param>
        /// <param name="guildData">Information about this guild. May not be null.</param>
        /// <param name="requestAuthor">The author of the channel create request. May not be null.</param>
        /// <param name="channelName">The requested name of the new channel. May not be null.</param>
        /// <param name="description">The requested description of the new channel.</param>
        /// <returns>The result of the request.</returns>
        public static async Task <CreateResult> Create(
            SocketGuild guildConnection,
            Guild guildData,
            SocketGuildUser requestAuthor,
            string channelName,
            string description)
        {
            ValidateArg.IsNotNullOrWhiteSpace(channelName, nameof(channelName));

            if (!guildData.OptinParentCategory.HasValue)
            {
                return(CreateResult.NoOptinCategory);
            }
            var optinsCategory = guildData.OptinParentCategory.GetValueOrDefault();

            // TODO: requestAuthor.Roles gets cached. How do I refresh this value so that it's accurate?

            var hasPermission = PermissionsUtilities.HasPermission(
                userRoles: requestAuthor.Roles.Select(x => new Snowflake(x.Id)),
                allowedRoles: guildData.OptinCreatorsRoles);

            if (!hasPermission)
            {
                return(CreateResult.NoPermissions);
            }

            var optinsCategoryConnection = guildConnection.GetCategoryChannel(optinsCategory.Value);
            var alreadyExists            = optinsCategoryConnection.Channels
                                           .Select(x => x.Name)
                                           .Any(x => string.Compare(x, channelName, ignoreCase: false) == 0);

            if (alreadyExists)
            {
                return(CreateResult.ChannelNameUsed);
            }

            var createdTextChannel = await guildConnection.CreateTextChannelAsync(channelName, settings =>
            {
                settings.CategoryId = optinsCategory.Value;
                settings.Topic      = description ?? string.Empty;
            }).ConfigureAwait(false);

            var createdRole = await guildConnection.CreateRoleAsync(
                name : OptinChannel.GetRoleName(createdTextChannel.Id),
                permissions : null,
                color : null,
                isHoisted : false,
                isMentionable : false)
                              .ConfigureAwait(false);

            var newPermissions = createdTextChannel.AddPermissionOverwriteAsync(
                role: createdRole,
                permissions: new OverwritePermissions(viewChannel: PermValue.Allow));

            await requestAuthor.AddRoleAsync(createdRole).ConfigureAwait(false);

            return(CreateResult.Success);
        }
Example #2
0
        /// <summary>
        /// Renames an opt-in channel in the given guild.
        /// </summary>
        /// <param name="guildConnection">
        /// The connection to the guild this channel is being created in. May not be null.
        /// </param>
        /// <param name="guildData">Information about this guild. May not be null.</param>
        /// <param name="requestAuthor">The author of the channel create request. May not be null.</param>
        /// <param name="currentChannelName">The current name of the channel to rename. May not be null.</param>
        /// <param name="newChannelName">The requested new name of the channel. May not be null.</param>
        /// <returns>The result of the request.</returns>
        public static async Task <RenameResult> Rename(
            SocketGuild guildConnection,
            Guild guildData,
            SocketGuildUser requestAuthor,
            string currentChannelName,
            string newChannelName)
        {
            ValidateArg.IsNotNullOrWhiteSpace(currentChannelName, nameof(currentChannelName));
            ValidateArg.IsNotNullOrWhiteSpace(newChannelName, nameof(newChannelName));

            if (!guildData.OptinParentCategory.HasValue)
            {
                return(RenameResult.NoOptinCategory);
            }
            var optinsCategory = guildData.OptinParentCategory.GetValueOrDefault();

            // Check that the request author has permission to create opt-ins (which means they can rename them as well)
            var hasPermission = PermissionsUtilities.HasPermission(
                userRoles: requestAuthor.Roles.Select(x => new Snowflake(x.Id)),
                allowedRoles: guildData.OptinCreatorsRoles);

            if (!hasPermission)
            {
                return(RenameResult.NoPermissions);
            }

            var optinsCategoryConnection = guildConnection.GetCategoryChannel(optinsCategory.Value);

            // Try to get the channel to rename and verify it exists
            var currentChannel = optinsCategoryConnection.Channels
                                 .Where(x => string.Compare(x.Name, currentChannelName, ignoreCase: false) == 0 && x is SocketTextChannel)
                                 .Cast <SocketTextChannel>()
                                 .SingleOrDefault();

            if (currentChannel == default)
            {
                return(RenameResult.NoSuchChannel);
            }

            // Verify the new channel name is not already in use
            var newChannelAlreadyExists = optinsCategoryConnection.Channels
                                          .Select(x => x.Name)
                                          .Any(x => string.Compare(x, newChannelName, ignoreCase: false) == 0);

            if (newChannelAlreadyExists)
            {
                return(RenameResult.NewChannelNameUsed);
            }

            // Modify the channel name
            await currentChannel.ModifyAsync(settings =>
            {
                settings.Name = newChannelName;
            }).ConfigureAwait(false);

            return(RenameResult.Success);
        }
Example #3
0
        /// <summary>
        /// Updates the description of an opt-in channel in the given guild.
        /// </summary>
        /// <param name="guildConnection">
        /// The connection to the guild this channel is being created in. May not be null.
        /// </param>
        /// <param name="guildData">Information about this guild. May not be null.</param>
        /// <param name="requestAuthor">The author of the channel create request. May not be null.</param>
        /// <param name="channelName">The current name of the channel to rename. May not be null.</param>
        /// <param name="description">The requested description of the channel.</param>
        /// <returns>The result of the request.</returns>
        public static async Task <UpdateDescriptionResult> UpdateDescription(
            SocketGuild guildConnection,
            Guild guildData,
            SocketGuildUser requestAuthor,
            string channelName,
            string description)
        {
            ValidateArg.IsNotNullOrWhiteSpace(channelName, nameof(channelName));

            if (!guildData.OptinParentCategory.HasValue)
            {
                return(UpdateDescriptionResult.NoOptinCategory);
            }
            var optinsCategory = guildData.OptinParentCategory.GetValueOrDefault();

            // Check that the request author has permission to create opt-ins (which means they can update their description as well)
            var hasPermission = PermissionsUtilities.HasPermission(
                userRoles: requestAuthor.Roles.Select(x => new Snowflake(x.Id)),
                allowedRoles: guildData.OptinCreatorsRoles);

            if (!hasPermission)
            {
                return(UpdateDescriptionResult.NoPermissions);
            }

            var optinsCategoryConnection = guildConnection.GetCategoryChannel(optinsCategory.Value);

            // Try to get the channel to update and verify it exists
            var currentChannel = optinsCategoryConnection.Channels
                                 .Where(x => string.Compare(x.Name, channelName, ignoreCase: false) == 0 && x is SocketTextChannel)
                                 .Cast <SocketTextChannel>()
                                 .SingleOrDefault();

            if (currentChannel == default)
            {
                return(UpdateDescriptionResult.NoSuchChannel);
            }

            // Modify the channel description
            await currentChannel.ModifyAsync(settings =>
            {
                settings.Topic = description ?? string.Empty;
            }).ConfigureAwait(false);

            return(UpdateDescriptionResult.Success);
        }