コード例 #1
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);
        }