Beispiel #1
0
        public async Task <IActionResult> StreamerRoleSettings(ulong guildId, StreamerRoleSettingsViewModel 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");

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

                return(View(viewModel));
            }

            // Save the settings to the database
            await _streamerRoleSettingsService.SaveSettings(new StreamerRoleSettings
            {
                GuildId = guildId,
                Enabled = viewModel.Enabled,
                RoleId  = viewModel.RoleId
            });

            return(RedirectToAction("Index"));
        }
Beispiel #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"));
        }
Beispiel #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"));
        }