diff --git a/src/FMBot.Bot/Builders/GuildSettingBuilder.cs b/src/FMBot.Bot/Builders/GuildSettingBuilder.cs index 2c1dd3bb..e1ce9149 100644 --- a/src/FMBot.Bot/Builders/GuildSettingBuilder.cs +++ b/src/FMBot.Bot/Builders/GuildSettingBuilder.cs @@ -963,4 +963,46 @@ public async Task ToggleChannelCommand(ContextModel context, ulon return response; } + + public async Task ToggleCrowns(ContextModel context, bool? disabled = null) + { + var response = new ResponseModel + { + ResponseType = ResponseType.Embed, + }; + + response.Embed.WithColor(DiscordConstants.InformationColorBlue); + + if (disabled.HasValue) + { + await this._guildService.ToggleCrownsAsync(context.DiscordGuild, disabled.Value); + } + + var guild = await this._guildService.GetGuildAsync(context.DiscordGuild.Id); + var crownsDisabled = guild.CrownsDisabled == true; + + var description = new StringBuilder(); + if (crownsDisabled) + { + response.Embed.WithTitle("Enabling crowns on this server"); + description.AppendLine($"Crown functionality is disabled on this server. "); + description.AppendLine(); + description.AppendLine("Use the button below to re-enable crowns."); + + response.Components = new ComponentBuilder().WithButton("Enable crowns", $"{InteractionConstants.ToggleCrowns.Enable}", style: ButtonStyle.Secondary); + } + else + { + response.Embed.WithTitle("Disabling crowns on this server"); + description.AppendLine($"Crowns can be earned when someone is the #1 listener for an artist and has {guild.CrownsMinimumPlaycountThreshold ?? Constants.DefaultPlaysForCrown} plays or more. "); + description.AppendLine(); + description.AppendLine("Use the button below to disable crown functionality server-wide. Crown history will be preserved, but it will no longer be visible."); + + response.Components = new ComponentBuilder().WithButton("Disable crowns", $"{InteractionConstants.ToggleCrowns.Disable}", style: ButtonStyle.Secondary); + } + + response.Embed.WithDescription(description.ToString()); + + return response; + } } diff --git a/src/FMBot.Bot/Resources/InteractionConstants.cs b/src/FMBot.Bot/Resources/InteractionConstants.cs index 5193a633..f9a5173b 100644 --- a/src/FMBot.Bot/Resources/InteractionConstants.cs +++ b/src/FMBot.Bot/Resources/InteractionConstants.cs @@ -143,6 +143,12 @@ public static class SupporterLinks public const string RunCrownseeder = "run-crownseeder"; + public static class ToggleCrowns + { + public const string Enable = "enable-crowns"; + public const string Disable = "disable-crowns"; + } + public static class ToggleCommand { public const string ToggleCommandMove = "toggle-command-move"; diff --git a/src/FMBot.Bot/Services/Guild/GuildService.cs b/src/FMBot.Bot/Services/Guild/GuildService.cs index 6ed4bfdf..ab48e4e4 100644 --- a/src/FMBot.Bot/Services/Guild/GuildService.cs +++ b/src/FMBot.Bot/Services/Guild/GuildService.cs @@ -25,7 +25,8 @@ public class GuildService private readonly IMemoryCache _cache; private readonly BotSettings _botSettings; - public GuildService(IDbContextFactory contextFactory, IMemoryCache cache, IOptions botSettings) + public GuildService(IDbContextFactory contextFactory, IMemoryCache cache, + IOptions botSettings) { this._contextFactory = contextFactory; this._cache = cache; @@ -51,7 +52,8 @@ public bool CheckIfDM(ICommandContext context) .FirstOrDefaultAsync(f => f.DiscordGuildId == discordGuildId); } - public async Task GetFullGuildAsync(ulong? discordGuildId = null, bool filterBots = true) + public async Task GetFullGuildAsync(ulong? discordGuildId = null, + bool filterBots = true) { if (discordGuildId == null) { @@ -211,6 +213,7 @@ public static (FilterStats Stats, IDictionary FilteredGuildU stats.ActivityThresholdFiltered = preFilterCount - users.Count; } + if (guild.UserActivityThresholdDays.HasValue) { var preFilterCount = users.Count; @@ -222,6 +225,7 @@ public static (FilterStats Stats, IDictionary FilteredGuildU stats.GuildActivityThresholdFiltered = preFilterCount - users.Count; } + if (guild.GuildBlockedUsers != null && guild.GuildBlockedUsers.Any(a => a.BlockedFromWhoKnows)) { var preFilterCount = users.Count; @@ -238,6 +242,7 @@ public static (FilterStats Stats, IDictionary FilteredGuildU stats.BlockedFiltered = preFilterCount - users.Count; } + if (guild.AllowedRoles != null && guild.AllowedRoles.Any()) { var preFilterCount = users.Count; @@ -248,6 +253,7 @@ public static (FilterStats Stats, IDictionary FilteredGuildU stats.AllowedRolesFiltered = preFilterCount - users.Count; } + if (guild.BlockedRoles != null && guild.BlockedRoles.Any()) { var preFilterCount = users.Count; @@ -258,13 +264,15 @@ public static (FilterStats Stats, IDictionary FilteredGuildU stats.BlockedRolesFiltered = preFilterCount - users.Count; } + if (roles != null && roles.Any()) { var preFilterCount = users.Count; users = users .Where(w => w.Value.Roles != null && roles.Any(a => w.Value.Roles.Contains(a))) - .ToDictionary(i => i.Key, i => i.Value); ; + .ToDictionary(i => i.Key, i => i.Value); + ; stats.ManualRoleFilter = preFilterCount - users.Count; } @@ -426,7 +434,7 @@ public async Task SetGuildReactionsAsync(IGuild discordGuild, string[] reactions await RemoveGuildFromCache(discordGuild.Id); } - public async Task ToggleCrownsAsync(IGuild discordGuild) + public async Task ToggleCrownsAsync(IGuild discordGuild, bool disabled) { await using var db = await this._contextFactory.CreateDbContextAsync(); var existingGuild = await db.Guilds @@ -434,15 +442,7 @@ public async Task SetGuildReactionsAsync(IGuild discordGuild, string[] reactions .FirstAsync(f => f.DiscordGuildId == discordGuild.Id); existingGuild.Name = discordGuild.Name; - - if (existingGuild.CrownsDisabled == true) - { - existingGuild.CrownsDisabled = false; - } - else - { - existingGuild.CrownsDisabled = true; - } + existingGuild.CrownsDisabled = disabled; db.Entry(existingGuild).State = EntityState.Modified; @@ -860,7 +860,8 @@ public async Task GetChannel(ulong discordChannelId) return existingChannel; } - public async Task DisableChannelCommandsAsync(IChannel discordChannel, int guildId, List commands, ulong discordGuildId) + public async Task DisableChannelCommandsAsync(IChannel discordChannel, int guildId, List commands, + ulong discordGuildId) { await using var db = await this._contextFactory.CreateDbContextAsync(); var existingChannel = await db.Channels @@ -911,7 +912,8 @@ public async Task DisableChannelCommandsAsync(IChannel discordChannel, int guild await db.SaveChangesAsync(); } - public async Task SetChannelEmbedType(IChannel discordChannel, int guildId, FmEmbedType? embedType, ulong discordGuildId) + public async Task SetChannelEmbedType(IChannel discordChannel, int guildId, FmEmbedType? embedType, + ulong discordGuildId) { await using var db = await this._contextFactory.CreateDbContextAsync(); var existingChannel = await db.Channels @@ -946,7 +948,8 @@ public async Task SetChannelEmbedType(IChannel discordChannel, int guildId, FmEm await db.SaveChangesAsync(); } - public async Task EnableChannelCommandsAsync(IChannel discordChannel, List commands, ulong discordGuildId) + public async Task EnableChannelCommandsAsync(IChannel discordChannel, List commands, + ulong discordGuildId) { await using var db = await this._contextFactory.CreateDbContextAsync(); var existingChannel = await db.Channels @@ -1056,7 +1059,8 @@ public async Task EnableChannelAsync(IChannel discordChannel, ulong discordGuild return existingChannel?.FmCooldown; } - public async Task SetChannelCooldownAsync(IChannel discordChannel, int guildId, int? cooldown, ulong discordGuildId) + public async Task SetChannelCooldownAsync(IChannel discordChannel, int guildId, int? cooldown, + ulong discordGuildId) { await using var db = await this._contextFactory.CreateDbContextAsync(); var existingChannel = await db.Channels diff --git a/src/FMBot.Bot/SlashCommands/GuildSettingSlashCommands.cs b/src/FMBot.Bot/SlashCommands/GuildSettingSlashCommands.cs index 2297afc1..e16ecc6a 100644 --- a/src/FMBot.Bot/SlashCommands/GuildSettingSlashCommands.cs +++ b/src/FMBot.Bot/SlashCommands/GuildSettingSlashCommands.cs @@ -230,6 +230,12 @@ public async Task GetGuildSetting(string[] inputs) await this.Context.SendResponse(this.Interactivity, response, ephemeral: false); } break; + case GuildSetting.CrownsDisabled: + { + response = await this._guildSettingBuilder.ToggleCrowns(new ContextModel(this.Context)); + await this.Context.SendResponse(this.Interactivity, response, ephemeral: false); + } + break; case GuildSetting.DisabledCommands: { response = await this._guildSettingBuilder.ToggleChannelCommand(new ContextModel(this.Context), this.Context.Channel.Id); @@ -789,4 +795,20 @@ public async Task ClearDisabledChannelCommand() await this.Context.UpdateInteractionEmbed(response); } + + [ComponentInteraction(InteractionConstants.ToggleCrowns.Enable)] + [ServerStaffOnly] + public async Task EnableCrowns() + { + var response = await this._guildSettingBuilder.ToggleCrowns(new ContextModel(this.Context), false); + await this.Context.UpdateInteractionEmbed(response); + } + + [ComponentInteraction(InteractionConstants.ToggleCrowns.Disable)] + [ServerStaffOnly] + public async Task DisableCrowns() + { + var response = await this._guildSettingBuilder.ToggleCrowns(new ContextModel(this.Context), true); + await this.Context.UpdateInteractionEmbed(response); + } } diff --git a/src/FMBot.Bot/TextCommands/Guild/CrownGuildSettingCommands.cs b/src/FMBot.Bot/TextCommands/Guild/CrownGuildSettingCommands.cs index 407bb447..bee47e63 100644 --- a/src/FMBot.Bot/TextCommands/Guild/CrownGuildSettingCommands.cs +++ b/src/FMBot.Bot/TextCommands/Guild/CrownGuildSettingCommands.cs @@ -306,11 +306,10 @@ public async Task CrownBlockedUsersAsync([Remainder] string searchValue = null) [GuildOnly] [RequiresIndex] [CommandCategories(CommandCategory.Crowns, CommandCategory.ServerSettings)] - public async Task ToggleCrownsAsync([Remainder] string confirmation = null) + public async Task ToggleCrownsAsync([Remainder] string unused = null) { var prfx = this._prefixService.GetPrefix(this.Context.Guild?.Id); _ = this.Context.Channel.TriggerTypingAsync(); - var guild = await this._guildService.GetGuildAsync(this.Context.Guild.Id); if (!await this._guildSettingBuilder.UserIsAllowed(new ContextModel(this.Context, prfx))) { @@ -319,27 +318,10 @@ public async Task ToggleCrownsAsync([Remainder] string confirmation = null) return; } - if (guild.CrownsDisabled != true && (confirmation == null || confirmation.ToLower() != "confirm")) - { - await ReplyAsync($"Disabling crowns will remove all existing crowns and crown history for this server.\n" + - $"Type `{prfx}togglecrowns confirm` to confirm."); - this.Context.LogCommandUsed(CommandResponse.WrongInput); - return; - } - - var crownsDisabled = await this._guildService.ToggleCrownsAsync(this.Context.Guild); + var response = await this._guildSettingBuilder.ToggleCrowns(new ContextModel(this.Context, prfx)); - if (crownsDisabled == true) - { - await this._crownService.RemoveAllCrownsFromGuild(guild.GuildId); - await ReplyAsync("All crowns have been removed and crowns have been disabled for this server."); - } - else - { - await ReplyAsync($"Crowns have been enabled for this server."); - } - - this.Context.LogCommandUsed(); + await this.Context.SendResponse(this.Interactivity, response); + this.Context.LogCommandUsed(response.CommandResponse); } [Command("killcrown", RunMode = RunMode.Async)] diff --git a/src/FMBot.Domain/Enums/GuildSetting.cs b/src/FMBot.Domain/Enums/GuildSetting.cs index b928d558..aa515414 100644 --- a/src/FMBot.Domain/Enums/GuildSetting.cs +++ b/src/FMBot.Domain/Enums/GuildSetting.cs @@ -24,8 +24,8 @@ public enum GuildSetting CrownMinimumPlaycount = 22, [Option("Crownseeder", "Automatically generate all crowns for your server")] CrownSeeder = 23, - //[Option("Crown functionality", "Completely enable or disable crowns on your server")] - //CrownsDisabled = 23, + [Option("Crown functionality", "Completely enable or disable crowns on your server")] + CrownsDisabled = 24, [Option("Disabled channel commands", "Toggle commands or the bot per channel")] DisabledCommands = 30,