Skip to content

Commit

Permalink
Add crown functionality toggle to config
Browse files Browse the repository at this point in the history
  • Loading branch information
th0mk committed Jan 15, 2025
1 parent ed0fc0e commit 2452a34
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 41 deletions.
42 changes: 42 additions & 0 deletions src/FMBot.Bot/Builders/GuildSettingBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -963,4 +963,46 @@ public async Task<ResponseModel> ToggleChannelCommand(ContextModel context, ulon

return response;
}

public async Task<ResponseModel> 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;
}
}
6 changes: 6 additions & 0 deletions src/FMBot.Bot/Resources/InteractionConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
38 changes: 21 additions & 17 deletions src/FMBot.Bot/Services/Guild/GuildService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class GuildService
private readonly IMemoryCache _cache;
private readonly BotSettings _botSettings;

public GuildService(IDbContextFactory<FMBotDbContext> contextFactory, IMemoryCache cache, IOptions<BotSettings> botSettings)
public GuildService(IDbContextFactory<FMBotDbContext> contextFactory, IMemoryCache cache,
IOptions<BotSettings> botSettings)
{
this._contextFactory = contextFactory;
this._cache = cache;
Expand All @@ -51,7 +52,8 @@ public bool CheckIfDM(ICommandContext context)
.FirstOrDefaultAsync(f => f.DiscordGuildId == discordGuildId);
}

public async Task<Persistence.Domain.Models.Guild> GetFullGuildAsync(ulong? discordGuildId = null, bool filterBots = true)
public async Task<Persistence.Domain.Models.Guild> GetFullGuildAsync(ulong? discordGuildId = null,
bool filterBots = true)
{
if (discordGuildId == null)
{
Expand Down Expand Up @@ -211,6 +213,7 @@ public static (FilterStats Stats, IDictionary<int, FullGuildUser> FilteredGuildU

stats.ActivityThresholdFiltered = preFilterCount - users.Count;
}

if (guild.UserActivityThresholdDays.HasValue)
{
var preFilterCount = users.Count;
Expand All @@ -222,6 +225,7 @@ public static (FilterStats Stats, IDictionary<int, FullGuildUser> FilteredGuildU

stats.GuildActivityThresholdFiltered = preFilterCount - users.Count;
}

if (guild.GuildBlockedUsers != null && guild.GuildBlockedUsers.Any(a => a.BlockedFromWhoKnows))
{
var preFilterCount = users.Count;
Expand All @@ -238,6 +242,7 @@ public static (FilterStats Stats, IDictionary<int, FullGuildUser> FilteredGuildU

stats.BlockedFiltered = preFilterCount - users.Count;
}

if (guild.AllowedRoles != null && guild.AllowedRoles.Any())
{
var preFilterCount = users.Count;
Expand All @@ -248,6 +253,7 @@ public static (FilterStats Stats, IDictionary<int, FullGuildUser> FilteredGuildU

stats.AllowedRolesFiltered = preFilterCount - users.Count;
}

if (guild.BlockedRoles != null && guild.BlockedRoles.Any())
{
var preFilterCount = users.Count;
Expand All @@ -258,13 +264,15 @@ public static (FilterStats Stats, IDictionary<int, FullGuildUser> 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;
}
Expand Down Expand Up @@ -426,23 +434,15 @@ public async Task SetGuildReactionsAsync(IGuild discordGuild, string[] reactions
await RemoveGuildFromCache(discordGuild.Id);
}

public async Task<bool?> ToggleCrownsAsync(IGuild discordGuild)
public async Task<bool?> ToggleCrownsAsync(IGuild discordGuild, bool disabled)
{
await using var db = await this._contextFactory.CreateDbContextAsync();
var existingGuild = await db.Guilds
.AsQueryable()
.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;

Expand Down Expand Up @@ -860,7 +860,8 @@ public async Task<Channel> GetChannel(ulong discordChannelId)
return existingChannel;
}

public async Task DisableChannelCommandsAsync(IChannel discordChannel, int guildId, List<string> commands, ulong discordGuildId)
public async Task DisableChannelCommandsAsync(IChannel discordChannel, int guildId, List<string> commands,
ulong discordGuildId)
{
await using var db = await this._contextFactory.CreateDbContextAsync();
var existingChannel = await db.Channels
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -946,7 +948,8 @@ public async Task SetChannelEmbedType(IChannel discordChannel, int guildId, FmEm
await db.SaveChangesAsync();
}

public async Task<string[]> EnableChannelCommandsAsync(IChannel discordChannel, List<string> commands, ulong discordGuildId)
public async Task<string[]> EnableChannelCommandsAsync(IChannel discordChannel, List<string> commands,
ulong discordGuildId)
{
await using var db = await this._contextFactory.CreateDbContextAsync();
var existingChannel = await db.Channels
Expand Down Expand Up @@ -1056,7 +1059,8 @@ public async Task EnableChannelAsync(IChannel discordChannel, ulong discordGuild
return existingChannel?.FmCooldown;
}

public async Task<int?> SetChannelCooldownAsync(IChannel discordChannel, int guildId, int? cooldown, ulong discordGuildId)
public async Task<int?> SetChannelCooldownAsync(IChannel discordChannel, int guildId, int? cooldown,
ulong discordGuildId)
{
await using var db = await this._contextFactory.CreateDbContextAsync();
var existingChannel = await db.Channels
Expand Down
22 changes: 22 additions & 0 deletions src/FMBot.Bot/SlashCommands/GuildSettingSlashCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
26 changes: 4 additions & 22 deletions src/FMBot.Bot/TextCommands/Guild/CrownGuildSettingCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
{
Expand All @@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions src/FMBot.Domain/Enums/GuildSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2452a34

Please sign in to comment.