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)); }
/// <summary> /// Initialize the module on GuildMemberUpdated event. /// </summary> /// <param name="client">Client for the module to be registered to.</param> public override Task Init(DiscordSocketClient client) { // Subscribe to the GuildMemberUpdated event. client.GuildMemberUpdated += async(user, guildUser) => { var settings = await _settingsService.GetSettingsByGuild(guildUser.Guild.Id); if (settings != null && settings.Enabled) { await CheckUser(guildUser); } }; return(Task.CompletedTask); }
public async Task <StreamerChannelSettingsViewModel> GetChannelSettingsAnnouncer( [FromServices] IDiscordUserGuildService userGuildService, [FromServices] IModuleSettingsService <StreamerSettings> streamerSettingsService, ulong guildId, ulong channelId) { // All channel's settings in guild. var allChannelSettings = await streamerSettingsService.GetSettingsByGuild(guildId, x => x.ChannelSettings); if (allChannelSettings == null) { return new StreamerChannelSettingsViewModel { Enabled = false, RemoveMessages = false } } ; // Settings for specific channel. var channelSettings = allChannelSettings.ChannelSettings.FirstOrDefault(x => x.ChannelId == channelId); var isEnabled = channelSettings != null; var settings = new StreamerChannelSettingsViewModel { Enabled = isEnabled, RemoveMessages = isEnabled && channelSettings.RemoveMessage }; return(settings); }
/// <summary> /// Handler for <see cref="RecurringReminderMessage"/>s that have either been created or updated. Checks the "Enabled" property to start or stop the recurring job. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private async void OnReminderChanged(object sender, EntityChangedEventArgs <RecurringReminderMessage> args) { var reminder = args.Entity; // Don't do anything with faulted reminders. User needs to correct // the fault before reminders can continue. if (reminder.Fault != RecurringReminderMessage.FaultType.None) { return; } var settings = await _moduleSettings.GetSettingsByGuild(args.Entity.GuildId); if (settings.Enabled) { if (reminder.Enabled) { StartOrUpdateJob(reminder); } else { StopJobIfExists(reminder); } } else { StopJobIfExists(reminder); } }
/// <summary> /// Announces the user's stream to the appropriate channel. /// </summary> /// <param name="user">User to be announced.</param> private async Task AnnounceUser(SocketGuildUser user) { // Build the embedded message. var embed = new EmbedBuilder() .WithTitle($"{user.Username} is now live!") .WithDescription($"{user.Game?.StreamUrl} - {user.Mention}") .WithColor(new Color(0x4A90E2)) .WithThumbnailUrl(user.GetAvatarUrl()) .AddInlineField("Title", user.Game?.Name).Build(); // Get the settings from the database. var settings = await _settingsService.GetSettingsByGuild(user.Guild.Id).ConfigureAwait(false); if (settings != null) { var announceChannelId = settings.AnnouncementChannelId; if (announceChannelId != 0) { logger.LogDebug($"StreamAnnouncer Module: Announcing {user.Username}"); // Announce the user to the channel specified in settings. await user.Guild.GetTextChannel(announceChannelId) .SendMessageAsync("", embed: embed).ConfigureAwait(false); } } }
public async Task <IActionResult> RemembotSettings(ulong guildId, [FromServices] IDiscordGuildService guildService) { var settings = await _reminderSettingsService.GetSettingsByGuild(guildId); var channels = await guildService.GetChannels(guildId); var textChannels = channels.Where(x => x.Type == 0).ToList(); if (settings is null) { settings = new RemembotSettings { Enabled = false, GuildId = guildId }; await _reminderSettingsService.SaveSettings(settings); } var reminders = await _recurringReminderService.Get(x => x.GuildId == guildId); settings.RecurringReminders = reminders; var viewModel = ReminderSettingsViewModel.FromData(settings, channels); return(View(viewModel)); }
public async Task InvokeAsync(DiscordFacingContext discordFacingContext) { if (discordFacingContext.Channel is IGuildChannel guildChannel) { var settings = await settingsService.GetSettingsByGuild(guildChannel.GuildId); if (settings.Enabled) { await this.command.InvokeAsync(discordFacingContext); } } }
public async Task <IActionResult> DadSettings(ulong guildId) { var settings = await _dadSettingsService.GetSettingsByGuild(guildId); if (settings is null) { settings = new DadModuleSettings { GuildId = guildId }; await _dadSettingsService.SaveSettings(settings); } var vm = new DadModuleSettingViewModel { Enabled = settings.Enabled, GuildId = guildId, ResponseCooldownMinutes = settings.DadResponseCooldownMinutes }; return(View(vm)); }
public async Task <IActionResult> ChatTrackerSettings(ulong guildId) { var viewModel = new ChatTrackerSettingsViewModel(); var settings = await _chatTrackerSettingsService.GetSettingsByGuild(guildId); if (settings == null) { return(View(viewModel)); } viewModel.Enabled = settings.Enabled; return(View(viewModel)); }
/// <summary> /// Initialize the module on GuildMemberUpdated event. /// </summary> /// <param name="client">Client for the module to be registered to.</param> public override async Task Init(DiscordSocketClient client) { // Populate streaming list from database. This is in case the bot restarts, the messages won't be lost. using (var scope = _scopeFactory.CreateScope()) { var messageService = scope.ServiceProvider.GetRequiredService <IEntityService <StreamAnnouncerMessage> >(); var messages = await messageService.GetAll(x => x.StreamerSettings); foreach (var m in messages) { if (!StreamingList.ContainsKey(m.StreamerSettings.GuildId)) { StreamingList.Add(m.StreamerSettings.GuildId, new HashSet <StreamAnnouncerMessage>()); } StreamingList[m.StreamerSettings.GuildId].Add(m); } } // Subscribe to the GuildMemberUpdated event. client.GuildMemberUpdated += async(user, guildUser) => { var settings = await _settingsService.GetSettingsByGuild(guildUser.Guild.Id, x => x.ChannelSettings); if (settings != null && settings.Enabled) { try { // Streamer Role if (settings.StreamerRoleEnabled) { await HandleStreamerRole(guildUser, settings); } // Stream Announcer if (settings.ChannelSettings != null) { await CheckUser(guildUser, settings.ChannelSettings); } } catch (Exception e) { Logger.LogError($"Streamer Module: Error occurred '{e.Message}'"); } } }; }
public async Task <IActionResult> StreamerRoleSettings(ulong guildId, [FromServices] IDiscordGuildService guildService) { var roles = await guildService.GetRoles(guildId); var viewModel = new StreamerRoleSettingsViewModel { Roles = new SelectList(roles.RemoveManaged(), "Id", "Name") }; var settings = await _streamerRoleSettingsService.GetSettingsByGuild(guildId); if (settings == null) { return(View(viewModel)); } viewModel.RoleId = settings.RoleId; viewModel.Enabled = settings.Enabled; return(View(viewModel)); }
public async Task <IActionResult> CleanChatSettings(ulong guildId, [FromServices] IDiscordGuildService guildService) { var viewModel = new CleanChatSettingsViewModel { Enabled = false, GuildId = guildId, CleanChatChannels = new List <ulong>() }; var settings = await _cleanchatSettingsService.GetSettingsByGuild(guildId, x => x.Channels); if (settings == null) { return(View(viewModel)); } viewModel.GuildId = settings.GuildId; viewModel.Enabled = settings.Enabled; viewModel.CleanChatChannels = settings.Channels?.Select(c => c.Id).ToList(); viewModel.MessageDuration = settings.MessageDuration; return(View(viewModel)); }
public async Task <IActionResult> StreamAnnouncerSettings(ulong guildId, [FromServices] IDiscordGuildService guildService) { var channels = await guildService.GetChannels(guildId); var viewModel = new StreamAnnouncerSettingsViewModel { Channels = new SelectList(channels.FilterChannelType(0), "Id", "Name") }; var settings = await _streamAnnouncerSettingsService.GetSettingsByGuild(guildId); if (settings == null) { return(View(viewModel)); } viewModel.ChannelId = settings.AnnouncementChannelId; viewModel.Enabled = settings.Enabled; viewModel.RemoveMessages = settings.RemoveMessages; return(View(viewModel)); }
/// <summary> /// Initialize the module on GuildMemberUpdated event. /// </summary> /// <param name="client">Client that the module will be registered to.</param> public override Task Init(DiscordSocketClient client) { client.GuildMemberUpdated += async(user, guildUser) => { var settings = await _settingsService.GetSettingsByGuild(guildUser.Guild.Id); if (settings != null && settings.Enabled) { // Get the streaming role. var streamingRole = guildUser.Guild.Roles.FirstOrDefault(r => r.Id == settings.RoleId); // Remove the streaming role if it does not exist. if (streamingRole == null) { await _settingsService.RemoveSetting(settings); Logger.LogError("StreamingRole Module: Role could not be found!"); } else { // Add use to role. if (guildUser.Game != null && guildUser.Game.Value.StreamType == StreamType.Twitch) { await AddUserToStreamingRole(guildUser, streamingRole); } // Remove user from role. else if (guildUser.Roles.Any(r => r == streamingRole)) { await RemoveUserFromStreamingRole(guildUser, streamingRole); } } } }; return(Task.CompletedTask); }
/// <summary> /// Returns true if the module is enabled for the specified guild and false if not. /// </summary> /// <param name="guildId">Id if the guild to check.</param> /// <returns>True if the module is enabled for the specified guild and false if not.</returns> public override async Task <bool> IsEnabledForGuild(ulong guildId) { var settings = await _settingsService.GetSettingsByGuild(guildId); return(settings != null && settings.Enabled); }
private void ClearCacheById(ulong guildId) { _moderationSettings.GetSettingsByGuild(guildId); }
/// <inheritdoc /> public async override Task <bool> IsEnabledForGuild(ulong guildId) { var settings = await _moduleSettings.GetSettingsByGuild(guildId); return(settings?.Enabled ?? false); }
/// <inheritdoc /> public async Task <ModerationSettings> GetModerationSettings(ulong guildId) { return(await _settingsService.GetSettingsByGuild(guildId, BuildSettingsQuery())); }