Skip to content

Commit

Permalink
Create prerequestites to assign role
Browse files Browse the repository at this point in the history
  • Loading branch information
FeroxFoxxo committed Nov 19, 2023
1 parent c4ad9c6 commit b59cb7f
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 16 deletions.
54 changes: 40 additions & 14 deletions backend/Role Reactions/Commands/AddAssignedRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AddAssignedRole : RoleMenuCommand<AddAssignedRole>
[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]);
Expand All @@ -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;
}

Expand Down Expand Up @@ -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:*,*")]
Expand All @@ -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()
Expand Down Expand Up @@ -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
{
Expand Down
3 changes: 2 additions & 1 deletion backend/Role Reactions/Commands/CreateRoleMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions backend/Role Reactions/Data/RoleReactionsDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public override void OverrideModelCreating(ModelBuilder modelBuilder)
.HasConversion(new DictionaryDataConverter<ulong, string>(),
new DictionaryDataComparer<ulong, string>());

modelBuilder
.Entity<RoleMenu>()
.Property(e => e.RoleToPrerequesite)
.HasConversion(new DictionaryDataConverter<ulong, ulong>(),
new DictionaryDataComparer<ulong, ulong>());

modelBuilder
.Entity<UserRoles>()
.Property(e => e.RoleIds)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace RoleReactions.Migrations
{
/// <inheritdoc />
public partial class PrerequesiteRoles : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder) => migrationBuilder.AddColumn<string>(
name: "RoleToPrerequesite",
schema: "RoleReactions",
table: "RoleReactionsMenu",
type: "longtext",
nullable: true)
.Annotation("MySql:CharSet", "utf8mb4");

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder) => migrationBuilder.DropColumn(
name: "RoleToPrerequesite",
schema: "RoleReactions",
table: "RoleReactionsMenu");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -42,6 +42,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<string>("RoleToEmote")
.HasColumnType("longtext");

b.Property<string>("RoleToPrerequesite")
.HasColumnType("longtext");

b.HasKey("GuildId", "ChannelId", "Id");

b.ToTable("RoleReactionsMenu", "RoleReactions");
Expand Down
1 change: 1 addition & 0 deletions backend/Role Reactions/Models/RoleMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class RoleMenu
public string Name { get; set; }
public ulong MessageId { get; set; }
public int MaximumRoles { get; set; }
public Dictionary<ulong, ulong> RoleToPrerequesite { get; set; }
public Dictionary<ulong, string> RoleToEmote { get; set; }
}

0 comments on commit b59cb7f

Please sign in to comment.