Ejemplo n.º 1
0
        /// <summary>
        /// Leave an opt-in channel.
        /// </summary>
        /// <param name="guildConnection">
        /// The connection to the guild the user is trying to join a channel 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 join channel request. May not be null.</param>
        /// <param name="channelName">The name of the channel to join.</param>
        /// <returns>The result of the request.</returns>
        public static async Task <LeaveResult> Leave(
            SocketGuild guildConnection,
            Guild guildData,
            SocketGuildUser requestAuthor,
            string channelName)
        {
            if (!guildData.OptinParentCategory.HasValue)
            {
                return(LeaveResult.NoOptinCategory);
            }
            var optinsCategory = guildData.OptinParentCategory.GetValueOrDefault();

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

            if (requestedChannel == null)
            {
                return(LeaveResult.NoSuchChannel);
            }

            var associatedRoleName = OptinChannel.GetRoleName(requestedChannel.Id);
            var role = guildConnection.Roles
                       .FirstOrDefault(x => string.Compare(x.Name, associatedRoleName, ignoreCase: false) == 0);

            if (role == null)
            {
                return(LeaveResult.RoleMissing);
            }

            await requestAuthor.RemoveRoleAsync(role).ConfigureAwait(false);

            return(LeaveResult.Success);
        }
Ejemplo n.º 2
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);
        }