Esempio n. 1
0
        public async Task <IActionResult> StreamerSettings(ulong guildId,
                                                           [FromServices] IDiscordGuildService guildService,
                                                           [FromServices] IEntityService <StreamerChannelSettings> channelSettingsService)
        {
            // All channels in guild.
            var channels = await guildService.GetChannels(guildId);

            // Text channels in guild.
            var textChannels = channels.Where(x => x.Type == 0).ToList();

            // Roles in guild.
            var roles = await guildService.GetRoles(guildId);

            var viewModel = new StreamerSettingsViewModel
            {
                Channels        = new SelectList(textChannels, "Id", "Name"),
                ChannelSettings = new StreamerChannelSettingsViewModel(),
                GuildId         = guildId.ToString(),
                Roles           = new SelectList(roles.RemoveManaged(), "Id", "Name")
            };

            // Get general module settings for guild, from database.
            var settings = await _streamAnnouncerSettingsService.GetSettingsByGuild(guildId, x => x.WhiteListedRoleIds);

            if (settings == null)
            {
                return(View(viewModel));
            }

            viewModel.Enabled             = settings.Enabled;
            viewModel.StreamerRoleEnabled = settings.StreamerRoleEnabled;
            viewModel.RoleId             = settings.RoleId;
            viewModel.WhiteListedRoleIds = settings.WhiteListedRoleIds?.Select(r => r.RoleId).ToList();

            if (settings.ChannelSettings == null)
            {
                settings.ChannelSettings = await channelSettingsService.Get(c => c.GuildId == guildId);
            }

            // Gets first text channel's settings to prepopulate view with.
            var defaultChannel = settings.ChannelSettings?.FirstOrDefault(x => x.ChannelId == textChannels[0].Id);

            // No channels setting saved, return view model as is.
            if (defaultChannel == null)
            {
                return(View(viewModel));
            }

            viewModel.ChannelSettings.RemoveMessages = defaultChannel.RemoveMessage;

            // Channel settings only exist if the module is enabled.
            viewModel.ChannelSettings.Enabled = true;

            viewModel.RoleId  = settings.RoleId;
            viewModel.Enabled = settings.Enabled;

            return(View(viewModel));
        }
Esempio n. 2
0
        public async Task <IActionResult> StreamerSettings(ulong guildId,
                                                           StreamerSettingsViewModel viewModel, [FromServices] IBot bot,
                                                           [FromServices] IDiscordGuildService guildService)
        {
            var botRolePosition = bot.GetBotRoleHierarchy(guildId);

            var roles = await guildService.GetRoles(guildId);

            var selectedRolePosition = roles.FirstOrDefault(r => r.Id == viewModel.RoleId)?.Position;

            // Discord bots cannot assign to roles that are higher then them in the hierarchy.
            if (selectedRolePosition > botRolePosition)
            {
                ModelState.AddModelError("RolePosition",
                                         "The bots managed role must be positioned higher then the selected role");

                // All channels in guild.
                var channels = await guildService.GetChannels(guildId);

                // Text channels in guild.
                var textChannels = channels.Where(x => x.Type == 0).ToList();

                viewModel.Channels = new SelectList(textChannels, "Id", "Name");
                viewModel.Roles    = new SelectList(roles.RemoveManaged(), "Id", "Name");

                return(View(viewModel));
            }

            var settings = await _streamAnnouncerChannelSettingsService.Find(viewModel.ChannelId);

            // Remember if there were settings in db, as settings will be populated later if they aren't.
            var isSettingsInDb = settings != null;

            var saveSettingsTasks = new List <Task>
            {
                _streamAnnouncerSettingsService.SaveSettings(new StreamerSettings
                {
                    GuildId             = guildId,
                    Enabled             = viewModel.Enabled,
                    StreamerRoleEnabled = viewModel.StreamerRoleEnabled,
                    RoleId = viewModel.RoleId
                })
            };

            // Save general module settings to the database
            if (!isSettingsInDb)
            {
                settings = new StreamerChannelSettings
                {
                    GuildId   = guildId,
                    ChannelId = viewModel.ChannelId
                }
            }
            ;

            settings.RemoveMessage = viewModel.ChannelSettings.RemoveMessages;

            // Save specific channel settings to the database.
            if (viewModel.ChannelSettings.Enabled)
            {
                saveSettingsTasks.Add(!isSettingsInDb
                    ? _streamAnnouncerChannelSettingsService.Create(settings)
                    : _streamAnnouncerChannelSettingsService.Update(settings));
            }
            else
            {
                if (isSettingsInDb)
                {
                    saveSettingsTasks.Add(_streamAnnouncerChannelSettingsService.Remove(settings));
                }
            }

            await Task.WhenAll(saveSettingsTasks.ToArray());

            return(RedirectToAction("Index"));
        }
Esempio n. 3
0
        public async Task <IActionResult> StreamerSettings(ulong guildId,
                                                           StreamerSettingsViewModel viewModel, [FromServices] IBot bot,
                                                           [FromServices] IDiscordGuildService guildService)
        {
            var botRolePosition = bot.GetBotRoleHierarchy(guildId);
            var roles           = await guildService.GetRoles(guildId);

            var selectedRolePosition = roles.FirstOrDefault(r => r.Id == viewModel.RoleId)?.Position;

            // Discord bots cannot assign to roles that are higher then them in the hierarchy.
            if (selectedRolePosition > botRolePosition)
            {
                ModelState.AddModelError("RolePosition",
                                         "The bots managed role must be positioned higher than the selected role");

                // All channels in guild.
                var channels = await guildService.GetChannels(guildId);

                // Text channels in guild.
                var textChannels = channels.Where(x => x.Type == 0).ToList();

                viewModel.Channels = new SelectList(textChannels, "Id", "Name");
                viewModel.Roles    = new SelectList(roles.RemoveManaged(), "Id", "Name");

                return(View(viewModel));
            }

            var moduleSettings = await _streamAnnouncerSettingsService.GetSettingsByGuild(guildId);

            var saveSettingsTasks = new List <Task>();

            // If the guild doesn't already have a settings DB entry, we want to add one before we do anything else.
            // Running that operation along side adding individual channel settings risks throwing an FK exception.
            if (moduleSettings == null)
            {
                var streamerSettings = new StreamerSettings
                {
                    GuildId             = guildId,
                    Enabled             = viewModel.Enabled,
                    StreamerRoleEnabled = viewModel.StreamerRoleEnabled,
                    RoleId             = viewModel.RoleId,
                    WhiteListedRoleIds = new List <WhiteListedRole>()
                };

                if (viewModel.WhiteListedRoleIds != null)
                {
                    streamerSettings.WhiteListedRoleIds = new List <WhiteListedRole>(viewModel.WhiteListedRoleIds
                                                                                     .Select(r =>
                                                                                             new WhiteListedRole
                    {
                        RoleId = r
                    }));
                }

                await _streamAnnouncerSettingsService.SaveSettings(streamerSettings);
            }
            else
            {
                // Clear all white listed roles from the database
                var whiteListedRoles = await _whiteListedRoleEntityService.Get(w => w.GuildId == guildId);

                if (whiteListedRoles != null)
                {
                    await _whiteListedRoleEntityService.RemoveBulk(whiteListedRoles);
                }

                if (viewModel.WhiteListedRoleIds != null)
                {
                    await _whiteListedRoleEntityService.CreateBulk(viewModel.WhiteListedRoleIds.Select(r =>
                                                                                                       new WhiteListedRole
                    {
                        RoleId  = r,
                        GuildId = guildId
                    }));
                }

                saveSettingsTasks.Add(_streamAnnouncerSettingsService.SaveSettings(new StreamerSettings
                {
                    GuildId             = guildId,
                    Enabled             = viewModel.Enabled,
                    StreamerRoleEnabled = viewModel.StreamerRoleEnabled,
                    RoleId = viewModel.RoleId
                }));
            }

            // Value defaults to 0, if the value is 0, EF will try to auto increment the ID, throwing an error.
            if (viewModel.ChannelId != 0)
            {
                // Settings for the specified channel of a guild.
                var settings = await _streamAnnouncerChannelSettingsService.Find(viewModel.ChannelId);

                // Remember if there were settings in db, as settings will be populated later if they aren't.
                var isSettingsInDb = settings != null;

                // Save general module settings to the database
                if (!isSettingsInDb)
                {
                    settings = new StreamerChannelSettings
                    {
                        GuildId   = guildId,
                        ChannelId = viewModel.ChannelId
                    }
                }
                ;

                settings.RemoveMessage = viewModel.ChannelSettings.RemoveMessages;

                // Save specific channel settings to the database.
                if (viewModel.ChannelSettings.Enabled)
                {
                    saveSettingsTasks.Add(!isSettingsInDb
                        ? _streamAnnouncerChannelSettingsService.Create(settings)
                        : _streamAnnouncerChannelSettingsService.Update(settings));
                }
                else
                {
                    if (isSettingsInDb)
                    {
                        saveSettingsTasks.Add(_streamAnnouncerChannelSettingsService.Remove(settings));
                    }
                }
            }

            await Task.WhenAll(saveSettingsTasks.ToArray());

            return(RedirectToAction("Index"));
        }