Exemplo n.º 1
0
        private async Task HandleLogChannelUpdate(SetUpRequest request, IUserMessage message)
        {
            var staffRoleId = _behaviourConfigurationService.GetStaffRoleId();

            if (staffRoleId is null)
            {
                await message.Channel.SendMessageAsync("Cannot set up log channel without staff role");

                await message.AddErrorEmote();

                return;
            }

            await message.Channel.SendMessageAsync("Enter name or Id of the channel for all logs to be sent to or reply `auto` to create a new channel, under the D2M category");

            while (true)
            {
                var response = await _discordMessageService.WaitForNextMessageFromUser(request.Message.Author.Id, TimeSpan.FromSeconds(30));

                if (response is null)
                {
                    break;
                }

                if (string.Equals(response.Content, "auto", StringComparison.InvariantCultureIgnoreCase))
                {
                    var hasExistingD2MLogChannel = _discordGuildService.HasChannel(Constants.DEFAULT_LOG_CHANNEL_NAME);

                    if (hasExistingD2MLogChannel)
                    {
                        var existingLogChannelId = _discordGuildService.GetChannelId(Constants.DEFAULT_LOG_CHANNEL_NAME);
                        await _behaviourConfigurationService.SetLogChannel(existingLogChannelId);
                    }
                    else
                    {
                        var categoryId = _behaviourConfigurationService.GetCategoryId();

                        if (categoryId is null)
                        {
                            await message.Channel.SendMessageAsync("Cannot set up log channel without D2M category");

                            await message.AddErrorEmote();

                            return;
                        }

                        var createdChannelId = await _discordGuildService.CreateChannel(Constants.DEFAULT_LOG_CHANNEL_NAME, "Logs for D2M", categoryId.Value, 1);

                        await _behaviourConfigurationService.SetLogChannel(createdChannelId);
                    }

                    await response.AddSuccessEmote();

                    break;
                }

                var isValidUlong = ulong.TryParse(response.Content, out var channelId) ||
                                   MentionUtils.TryParseChannel(response.Content, out channelId);

                if (isValidUlong)
                {
                    var isValidChannel = _discordGuildService.HasChannel(channelId);

                    if (!isValidChannel)
                    {
                        await response.AddErrorEmote();

                        continue;
                    }

                    await _behaviourConfigurationService.SetLogChannel(channelId);

                    await response.AddSuccessEmote();

                    break;
                }

                var sanitzedChannelMention = response.Content.Replace("#", string.Empty);

                var hasNamedCategory = _discordGuildService.HasChannel(sanitzedChannelMention);

                if (!hasNamedCategory)
                {
                    await response.AddErrorEmote();

                    continue;
                }

                var namedCategoryId = _discordGuildService.GetChannelId(sanitzedChannelMention);
                await _behaviourConfigurationService.SetLogChannel(namedCategoryId);

                await response.AddSuccessEmote();

                break;
            }

            var setUpLogChannelId = _behaviourConfigurationService.GetLogChannelId();

            if (setUpLogChannelId is null)
            {
                return;
            }

            await _permissionService.SynchronisePermissionsWithCategory(setUpLogChannelId.Value);

            await _permissionService.AddOverrideForLogChannel(setUpLogChannelId.Value, staffRoleId.Value);
        }
Exemplo n.º 2
0
        private async Task HandleCategoryUpdate(SetUpRequest request, IUserMessage message)
        {
            var staffRoleId = _behaviourConfigurationService.GetStaffRoleId();

            if (staffRoleId is null)
            {
                await message.Channel.SendMessageAsync("Cannot set up log channel without staff role");

                await message.AddErrorEmote();

                return;
            }

            await message.Channel.SendMessageAsync("Enter name or Id of the category for all new threads to be nested in or reply `auto` to create a new category");

            while (true)
            {
                // Loop until we have a response from the user, or break if we don't get a response, i.e. if
                // we hit the soft timeout of the waiting period for the next message

                var response = await _discordMessageService.WaitForNextMessageFromUser(request.Message.Author.Id, TimeSpan.FromSeconds(30));

                if (response is null)
                {
                    break;
                }

                // User has indicated they want the bot to set up the category
                if (string.Equals(response.Content, "auto", StringComparison.InvariantCultureIgnoreCase))
                {
                    // We need to check if there is a category with this name already, since we'll
                    // likely need to reuse this channel if it exists
                    var hasExistingD2MCategory = _discordGuildService.HasCategory(Constants.DEFAULT_CATEGORY_NAME);

                    if (hasExistingD2MCategory)
                    {
                        // If it exists, get the ID and hook up automagically for us, since the category
                        // was either originally created by D2M or the user self-created this category
                        var existingCategoryId = _discordGuildService.GetCategoryId(Constants.DEFAULT_CATEGORY_NAME);
                        await _behaviourConfigurationService.SetCategory(existingCategoryId);
                    }
                    else
                    {
                        // Create the category
                        var createdCategoryId = await _discordGuildService.CreateCategory(Constants.DEFAULT_CATEGORY_NAME, 1);

                        await _behaviourConfigurationService.SetCategory(createdCategoryId);
                    }

                    await response.AddSuccessEmote();

                    break;
                }

                var isValidUlong = ulong.TryParse(response.Content, out var categoryId);

                if (isValidUlong)
                {
                    var isValidCategory = _discordGuildService.HasCategory(categoryId);

                    if (!isValidCategory)
                    {
                        await response.AddErrorEmote();

                        continue;
                    }

                    await _behaviourConfigurationService.SetCategory(categoryId);

                    await response.AddSuccessEmote();

                    break;
                }

                // Assume anything else is the raw name of the category

                var sanitizedCategoryMention = response.Content.Replace("#", string.Empty);

                var hasNamedCategory = _discordGuildService.HasCategory(sanitizedCategoryMention);

                if (!hasNamedCategory)
                {
                    await response.AddErrorEmote();

                    continue;
                }

                var namedCategoryId = _discordGuildService.GetCategoryId(sanitizedCategoryMention);
                await _behaviourConfigurationService.SetCategory(namedCategoryId);

                await response.AddSuccessEmote();

                break;
            }

            var setUpCategoryId = _behaviourConfigurationService.GetCategoryId();

            if (setUpCategoryId is null)
            {
                return;
            }

            await _permissionService.RestrictCategoryToStaff(setUpCategoryId.Value, staffRoleId.Value);
        }