diff --git a/backend/Role Reactions/Commands/AddAssignedRole.cs b/backend/Role Reactions/Commands/AddAssignedRole.cs index 752e49214..8bb8e7d9e 100644 --- a/backend/Role Reactions/Commands/AddAssignedRole.cs +++ b/backend/Role Reactions/Commands/AddAssignedRole.cs @@ -15,7 +15,7 @@ public class AddAssignedRole : RoleMenuCommand [SlashCommand("add-rm-role", "Assigns a role to a role menu")] [Require(RequireCheck.GuildAdmin)] public async Task AddAssignedRoleCommand([Autocomplete(typeof(MenuHandler))] string menuStr, - string emote, IRole role) + string emote, IRole roleToAssign, IRole prerequesiteRole) { var menuArray = menuStr.Split(','); var menuId = int.Parse(menuArray[0]); @@ -37,9 +37,9 @@ public async Task AddAssignedRoleCommand([Autocomplete(typeof(MenuHandler))] str return; } - if (menu.RoleToEmote.ContainsKey(role.Id)) + if (menu.RoleToEmote.ContainsKey(roleToAssign.Id)) { - await RespondInteraction($"Role `{role.Name}` already exists for role menu `{menu.Name}`!"); + await RespondInteraction($"Role `{roleToAssign.Name}` already exists for role menu `{menu.Name}`!"); return; } @@ -72,12 +72,16 @@ await RespondInteraction($"Too many roles in manu `{menu.Name}`! " + return; } - menu.RoleToEmote.Add(role.Id, emote); + menu.RoleToEmote.Add(roleToAssign.Id, emote); + + if (prerequesiteRole != null) + menu.RoleToPrerequesite.Add(roleToAssign.Id, prerequesiteRole.Id); + await Database.SaveChangesAsync(); await CreateRoleMenu(menu, userMessage); - await RespondInteraction($"Successfully added role `{role.Name}` to menu `{menu.Name}`!"); + await RespondInteraction($"Successfully added role `{roleToAssign.Name}` to menu `{menu.Name}`!"); } [ComponentInteraction("add-rm-role:*,*")] @@ -93,6 +97,11 @@ public async Task AddRole(string sRoleId, string sMenuId) var user = Context.Guild.GetUser(userId); var userInfo = Database.UserRoles.Find(Context.Guild.Id, Context.Channel.Id, menuId, userId); + IRole preRequesiteRole = null; + + if (menu.RoleToPrerequesite.TryGetValue(roleId, out var preRoleId)) + preRequesiteRole = Context.Guild.GetRole(preRoleId); + if (userInfo == null) { userInfo = new UserRoles() @@ -126,15 +135,32 @@ public async Task AddRole(string sRoleId, string sMenuId) if (rolesInCat < menu.MaximumRoles || menu.MaximumRoles <= 0) { - await user.AddRoleAsync(role); - - if (!userInfo.RoleIds.Contains(roleId)) - userInfo.RoleIds.Add(roleId); - - embed - .WithColor(Color.Green) - .WithTitle("Added Role") - .WithDescription($"{role.Mention} has been added to {user.Mention}!"); + var meetsPrerequesite = true; + + if (preRequesiteRole != null) + if (!user.Roles.Any(r => r.Id == preRequesiteRole.Id)) + meetsPrerequesite = false; + + if (meetsPrerequesite) + { + await user.AddRoleAsync(role); + + if (!userInfo.RoleIds.Contains(roleId)) + userInfo.RoleIds.Add(roleId); + + embed + .WithColor(Color.Green) + .WithTitle("Added Role") + .WithDescription($"{role.Mention} has been added to {user.Mention}!"); + } + else + { + embed + .WithColor(Color.Red) + .WithTitle("Could Not Add Role") + .WithDescription($"{user.Mention} does not have the prerequesite role " + + $"of {preRequesiteRole.Mention} to assign this role!"); + } } else { diff --git a/backend/Role Reactions/Commands/CreateRoleMenu.cs b/backend/Role Reactions/Commands/CreateRoleMenu.cs index 999151956..6161e9754 100644 --- a/backend/Role Reactions/Commands/CreateRoleMenu.cs +++ b/backend/Role Reactions/Commands/CreateRoleMenu.cs @@ -60,7 +60,8 @@ public async Task CreateRoleMenuCommand(string title, string description, int ma Id = lowestId, Name = title, MaximumRoles = maxRoles, - RoleToEmote = [] + RoleToEmote = [], + RoleToPrerequesite = [] }; var embed = new EmbedBuilder() diff --git a/backend/Role Reactions/Data/RoleReactionsDatabase.cs b/backend/Role Reactions/Data/RoleReactionsDatabase.cs index 2915b6989..216ca007b 100644 --- a/backend/Role Reactions/Data/RoleReactionsDatabase.cs +++ b/backend/Role Reactions/Data/RoleReactionsDatabase.cs @@ -23,6 +23,12 @@ public override void OverrideModelCreating(ModelBuilder modelBuilder) .HasConversion(new DictionaryDataConverter(), new DictionaryDataComparer()); + modelBuilder + .Entity() + .Property(e => e.RoleToPrerequesite) + .HasConversion(new DictionaryDataConverter(), + new DictionaryDataComparer()); + modelBuilder .Entity() .Property(e => e.RoleIds) diff --git a/backend/Role Reactions/Migrations/20231119201930_PrerequesiteRoles.Designer.cs b/backend/Role Reactions/Migrations/20231119201930_PrerequesiteRoles.Designer.cs new file mode 100644 index 000000000..69bfdcd2f --- /dev/null +++ b/backend/Role Reactions/Migrations/20231119201930_PrerequesiteRoles.Designer.cs @@ -0,0 +1,80 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using RoleReactions.Data; + +#nullable disable + +namespace RoleReactions.Migrations +{ + [DbContext(typeof(RoleReactionsDatabase))] + [Migration("20231119201930_PrerequesiteRoles")] + partial class PrerequesiteRoles + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasDefaultSchema("RoleReactions") + .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 64); + + modelBuilder.Entity("RoleReactions.Models.RoleMenu", b => + { + b.Property("GuildId") + .HasColumnType("bigint unsigned"); + + b.Property("ChannelId") + .HasColumnType("bigint unsigned"); + + b.Property("Id") + .HasColumnType("int"); + + b.Property("MaximumRoles") + .HasColumnType("int"); + + b.Property("MessageId") + .HasColumnType("bigint unsigned"); + + b.Property("Name") + .HasColumnType("longtext"); + + b.Property("RoleToEmote") + .HasColumnType("longtext"); + + b.Property("RoleToPrerequesite") + .HasColumnType("longtext"); + + b.HasKey("GuildId", "ChannelId", "Id"); + + b.ToTable("RoleReactionsMenu", "RoleReactions"); + }); + + modelBuilder.Entity("RoleReactions.Models.UserRoles", b => + { + b.Property("GuildId") + .HasColumnType("bigint unsigned"); + + b.Property("ChannelId") + .HasColumnType("bigint unsigned"); + + b.Property("Id") + .HasColumnType("int"); + + b.Property("UserId") + .HasColumnType("bigint unsigned"); + + b.Property("RoleIds") + .HasColumnType("longtext"); + + b.HasKey("GuildId", "ChannelId", "Id", "UserId"); + + b.ToTable("UserRoles", "RoleReactions"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/backend/Role Reactions/Migrations/20231119201930_PrerequesiteRoles.cs b/backend/Role Reactions/Migrations/20231119201930_PrerequesiteRoles.cs new file mode 100644 index 000000000..e15d37a23 --- /dev/null +++ b/backend/Role Reactions/Migrations/20231119201930_PrerequesiteRoles.cs @@ -0,0 +1,25 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace RoleReactions.Migrations +{ + /// + public partial class PrerequesiteRoles : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) => migrationBuilder.AddColumn( + name: "RoleToPrerequesite", + schema: "RoleReactions", + table: "RoleReactionsMenu", + type: "longtext", + nullable: true) + .Annotation("MySql:CharSet", "utf8mb4"); + + /// + protected override void Down(MigrationBuilder migrationBuilder) => migrationBuilder.DropColumn( + name: "RoleToPrerequesite", + schema: "RoleReactions", + table: "RoleReactionsMenu"); + } +} diff --git a/backend/Role Reactions/Migrations/RoleReactionsDatabaseModelSnapshot.cs b/backend/Role Reactions/Migrations/RoleReactionsDatabaseModelSnapshot.cs index 8cda0123f..ca1b78eef 100644 --- a/backend/Role Reactions/Migrations/RoleReactionsDatabaseModelSnapshot.cs +++ b/backend/Role Reactions/Migrations/RoleReactionsDatabaseModelSnapshot.cs @@ -16,7 +16,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) #pragma warning disable 612, 618 modelBuilder .HasDefaultSchema("RoleReactions") - .HasAnnotation("ProductVersion", "7.0.12") + .HasAnnotation("ProductVersion", "8.0.0") .HasAnnotation("Relational:MaxIdentifierLength", 64); modelBuilder.Entity("RoleReactions.Models.RoleMenu", b => @@ -42,6 +42,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("RoleToEmote") .HasColumnType("longtext"); + b.Property("RoleToPrerequesite") + .HasColumnType("longtext"); + b.HasKey("GuildId", "ChannelId", "Id"); b.ToTable("RoleReactionsMenu", "RoleReactions"); diff --git a/backend/Role Reactions/Models/RoleMenu.cs b/backend/Role Reactions/Models/RoleMenu.cs index eb113511c..579b5fbca 100644 --- a/backend/Role Reactions/Models/RoleMenu.cs +++ b/backend/Role Reactions/Models/RoleMenu.cs @@ -11,5 +11,6 @@ public class RoleMenu public string Name { get; set; } public ulong MessageId { get; set; } public int MaximumRoles { get; set; } + public Dictionary RoleToPrerequesite { get; set; } public Dictionary RoleToEmote { get; set; } }