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