diff --git a/backend/Role Reactions/Commands/AddAssignedRole.cs b/backend/Role Reactions/Commands/AddAssignedRole.cs index 16069ab3d..98548801f 100644 --- a/backend/Role Reactions/Commands/AddAssignedRole.cs +++ b/backend/Role Reactions/Commands/AddAssignedRole.cs @@ -22,117 +22,113 @@ public async Task AddAssignedRoleCommand(string emote, [Autocomplete(typeof(Menu if (Context.Channel is ITextChannel txtChannel) channel = txtChannel; - if (channel != null) + if (channel == null) { - var menu = Database.RoleReactionsMenu.Find(channel.GuildId, channel.Id, menuId); - - if (menu == null) - { - await RespondInteraction($"Role menu `{menuId}` does not exist in this channel!"); - return; - } - - if (menu.RoleToEmote.ContainsKey(role.Id)) - { - await RespondInteraction($"Role `{role.Name}` already exists for role menu `{menu.Name}`!"); - return; - } + await RespondInteraction(Translator.Get().OnlyTextChannel()); + return; + } - var message = await channel.GetMessageAsync(menu.MessageId); + var menu = Database.RoleReactionsMenu.Find(channel.GuildId, channel.Id, menuId); - if (message == null) - { - await RespondInteraction($"Role menu `{menu.Name}` does not have a message related to it! " + - $"Please delete and recreate the menu."); - return; - } + if (menu == null) + { + await RespondInteraction($"Role menu `{menuId}` does not exist in this channel!"); + return; + } - if (!Emote.TryParse(emote, out var _) && !Emoji.TryParse(emote, out var _)) - { - await RespondInteraction($"Emote `{emote}` could not be found!"); - return; - } + if (menu.RoleToEmote.ContainsKey(role.Id)) + { + await RespondInteraction($"Role `{role.Name}` already exists for role menu `{menu.Name}`!"); + return; + } - if (message is IUserMessage userMessage) - { - var rows = new List>(); - var tempComp = new Dictionary(); + var message = await channel.GetMessageAsync(menu.MessageId); - var count = 1; + if (message == null) + { + await RespondInteraction($"Role menu `{menu.Name}` does not have a message related to it! " + + $"Please delete and recreate the menu."); + return; + } - foreach (var storeRole in menu.RoleToEmote) - { - tempComp.Add(storeRole.Key, storeRole.Value); + if (!Emote.TryParse(emote, out var _) && !Emoji.TryParse(emote, out var _)) + { + await RespondInteraction($"Emote `{emote}` could not be found!"); + return; + } - if (tempComp.Count >= 5) - { - rows.Add(tempComp); - tempComp = []; - } + if (message is not IUserMessage userMessage) + { + await RespondInteraction($"Message for role menu `{menu.Name}` was not created by me! " + + $"Please delete and recreate the menu."); + return; + } - count++; - } + var rows = new List>(); + var tempComp = new Dictionary(); - if (count > 25) - { - await RespondInteraction($"Too many roles in manu `{menu.Name}`! " + - $"Please create a new role menu."); - return; - } + var count = 1; - tempComp.Add(role.Id, emote); + foreach (var storeRole in menu.RoleToEmote) + { + tempComp.Add(storeRole.Key, storeRole.Value); + if (tempComp.Count >= 5) + { rows.Add(tempComp); + tempComp = []; + } - var components = new ComponentBuilder(); - - foreach(var row in rows) - { - var aRow = new ActionRowBuilder(); + count++; + } - foreach (var col in row) - { - IEmote intEmote = null; + if (count > 25) + { + await RespondInteraction($"Too many roles in manu `{menu.Name}`! " + + $"Please create a new role menu."); + return; + } - if (Emote.TryParse(col.Value, out var pEmote)) - intEmote = pEmote; + tempComp.Add(role.Id, emote); - if (Emoji.TryParse(col.Value, out var pEmoji)) - intEmote = pEmoji; + rows.Add(tempComp); - var intRole = Context.Guild.GetRole(col.Key); + var components = new ComponentBuilder(); - if (intRole != null) - aRow.WithButton( - intRole.Name, - $"add-rm-role:{intRole.Id},{Context.User.Id},{menu.Id}", - emote: intEmote - ); - } + foreach (var row in rows) + { + var aRow = new ActionRowBuilder(); - components.AddRow(aRow); - } + foreach (var col in row) + { + IEmote intEmote = null; - await userMessage.ModifyAsync(m => m.Components = components.Build()); + if (Emote.TryParse(col.Value, out var pEmote)) + intEmote = pEmote; - menu.RoleToEmote.Add(role.Id, emote); + if (Emoji.TryParse(col.Value, out var pEmoji)) + intEmote = pEmoji; - await Database.SaveChangesAsync(); + var intRole = Context.Guild.GetRole(col.Key); - await RespondInteraction($"Successfully added role `{role.Name}` to menu `{menu.Name}`!"); + if (intRole != null) + aRow.WithButton( + intRole.Name, + $"add-rm-role:{intRole.Id},{Context.User.Id},{menu.Id}", + emote: intEmote + ); } - else - { - await RespondInteraction($"Message for role menu `{menu.Name}` was not created by me! " + - $"Please delete and recreate the menu."); - return; - } - } - else - { - await RespondInteraction(Translator.Get().OnlyTextChannel()); - return; + + components.AddRow(aRow); } + + await userMessage.ModifyAsync(m => m.Components = components.Build()); + + menu.RoleToEmote.Add(role.Id, emote); + + await Database.SaveChangesAsync(); + + await RespondInteraction($"Successfully added role `{role.Name}` to menu `{menu.Name}`!"); } [ComponentInteraction("add-rm-role:*,*,*")] diff --git a/backend/Role Reactions/Commands/CreateRoleMenu.cs b/backend/Role Reactions/Commands/CreateRoleMenu.cs index e372698f7..f42237a70 100644 --- a/backend/Role Reactions/Commands/CreateRoleMenu.cs +++ b/backend/Role Reactions/Commands/CreateRoleMenu.cs @@ -15,67 +15,65 @@ public class CreateRoleMenu : RoleMenuCommand [SlashCommand("create-rm", "Create a menu that users can pick their roles from!")] [Require(RequireCheck.GuildAdmin)] - public async Task RoleMenuCommand(string title, string description, + public async Task CreateRoleMenuCommand(string title, string description, ITextChannel channel = null, string colorHex = default) { if (channel == null) if (Context.Channel is ITextChannel txtChannel) channel = txtChannel; - if (channel != null) + if (channel == null) { - var allMenus = Database.RoleReactionsMenu.Where( + await RespondInteraction(Translator.Get().OnlyTextChannel()); + return; + } + + var allMenus = Database.RoleReactionsMenu.Where( x => x.GuildId == Context.Guild.Id && x.ChannelId == channel.Id ); - var menu = allMenus.FirstOrDefault(x => x.Name == title); + var menu = allMenus.FirstOrDefault(x => x.Name == title); - if (menu != null) - { - await RespondInteraction($"Role menu `{title}` already exists in this channel!"); - return; - } + if (menu != null) + { + await RespondInteraction($"Role menu `{title}` already exists in this channel!"); + return; + } - var color = Color.Teal; + var color = Color.Teal; - if (!string.IsNullOrEmpty(colorHex)) - color = new Color( - Convert.ToUInt32(colorHex.ToUpper().Replace("#", ""), 16) - ); + if (!string.IsNullOrEmpty(colorHex)) + color = new Color( + Convert.ToUInt32(colorHex.ToUpper().Replace("#", ""), 16) + ); - var embed = new EmbedBuilder() - .WithTitle(title) - .WithDescription(description) - .WithColor(color); + var embed = new EmbedBuilder() + .WithTitle(title) + .WithDescription(description) + .WithColor(color); - var msg = await channel.SendMessageAsync(embed: embed.Build()); + var msg = await channel.SendMessageAsync(embed: embed.Build()); - var lowestId = 1; + var lowestId = 1; - if (allMenus.Any()) - lowestId += allMenus.Max(x => x.Id); + if (allMenus.Any()) + lowestId += allMenus.Max(x => x.Id); - Database.RoleReactionsMenu.Add( - new RoleMenu() - { - ChannelId = channel.Id, - GuildId = channel.GuildId, - Id = lowestId, - Name = title, - MessageId = msg.Id, - RoleToEmote = new Dictionary() - } - ); + Database.RoleReactionsMenu.Add( + new RoleMenu() + { + ChannelId = channel.Id, + GuildId = channel.GuildId, + Id = lowestId, + Name = title, + MessageId = msg.Id, + RoleToEmote = new Dictionary() + } + ); - await Database.SaveChangesAsync(); + await Database.SaveChangesAsync(); - await RespondInteraction($"Role menu `{title}` is now set up!"); - } - else - { - await RespondInteraction(Translator.Get().OnlyTextChannel()); - return; - } + await RespondInteraction($"Role menu `{title}` is now set up!"); } } diff --git a/backend/Role Reactions/Commands/DeleteRoleMenu.cs b/backend/Role Reactions/Commands/DeleteRoleMenu.cs index de39a1bdd..97b780ca4 100644 --- a/backend/Role Reactions/Commands/DeleteRoleMenu.cs +++ b/backend/Role Reactions/Commands/DeleteRoleMenu.cs @@ -14,46 +14,43 @@ public class DeleteRoleMenu : RoleMenuCommand [SlashCommand("delete-rm", "Deletes a menu that users can pick their roles from!")] [Require(RequireCheck.GuildAdmin)] - public async Task RoleMenuCommand([Autocomplete(typeof(MenuHandler))] int menuId, ITextChannel channel = null) + public async Task DeleteRoleMenuCommand([Autocomplete(typeof(MenuHandler))] int menuId, ITextChannel channel = null) { if (channel == null) if (Context.Channel is ITextChannel txtChannel) channel = txtChannel; - if (channel != null) + if (channel == null) { - var menu = Database.RoleReactionsMenu.Find(channel.GuildId, channel.Id, menuId); + await RespondInteraction(Translator.Get().OnlyTextChannel()); + return; + } + var menu = Database.RoleReactionsMenu.Find(channel.GuildId, channel.Id, menuId); - if (menu == null) - { - await RespondInteraction($"Role menu `{menuId}` does not exist in this channel!"); - return; - } + if (menu == null) + { + await RespondInteraction($"Role menu `{menuId}` does not exist in this channel!"); + return; + } - var message = await channel.GetMessageAsync(menu.MessageId); + var message = await channel.GetMessageAsync(menu.MessageId); - if (message != null) - await channel.DeleteMessageAsync(menu.MessageId); + if (message != null) + await channel.DeleteMessageAsync(menu.MessageId); - Database.RoleReactionsMenu.Remove(menu); + Database.RoleReactionsMenu.Remove(menu); - var addedRoles = Database.UserRoles.Where( - x => x.GuildId == Context.Guild.Id && - x.ChannelId == Context.Channel.Id && - x.Id == menuId - ); + var addedRoles = Database.UserRoles.Where( + x => x.GuildId == Context.Guild.Id && + x.ChannelId == Context.Channel.Id && + x.Id == menuId + ); - foreach (var role in addedRoles) - Database.UserRoles.Remove(role); + foreach (var role in addedRoles) + Database.UserRoles.Remove(role); - await Database.SaveChangesAsync(); + await Database.SaveChangesAsync(); - await RespondInteraction($"Role menu `{menu.Name}` is now deleted!"); - } - else - { - await RespondInteraction(Translator.Get().OnlyTextChannel()); - return; - } + await RespondInteraction($"Role menu `{menu.Name}` is now deleted!"); } } diff --git a/backend/Role Reactions/Commands/EditRoleMenu.cs b/backend/Role Reactions/Commands/EditRoleMenu.cs new file mode 100644 index 000000000..3d895f915 --- /dev/null +++ b/backend/Role Reactions/Commands/EditRoleMenu.cs @@ -0,0 +1,94 @@ +using Bot.Attributes; +using Bot.Enums; +using Bot.Translators; +using Discord; +using Discord.Interactions; +using RoleReactions.Abstractions; +using RoleReactions.Data; + +namespace RoleReactions.Commands; + +public class EditRoleMenu : RoleMenuCommand +{ + public RoleReactionsDatabase Database { get; set; } + + [SlashCommand("edit-rm", "Edits a menu that users can pick their roles from!")] + [Require(RequireCheck.GuildAdmin)] + public async Task EditRoleMenuCommand([Autocomplete(typeof(MenuHandler))] int menuId, ITextChannel channel = null, + string title = default, string description = default, string colorHex = default) + { + if (channel == null) + if (Context.Channel is ITextChannel txtChannel) + channel = txtChannel; + + if (channel == null) + { + await RespondInteraction(Translator.Get().OnlyTextChannel()); + return; + } + + var menu = Database.RoleReactionsMenu.Find(channel.GuildId, channel.Id, menuId); + + if (menu == null) + { + await RespondInteraction($"Role menu `{menuId}` does not exist in this channel!"); + return; + } + + var message = await channel.GetMessageAsync(menu.MessageId); + + if (message == null) + { + await RespondInteraction($"Role menu `{menu.Name}` does not have a message related to it! " + + $"Please delete and recreate the menu."); + return; + } + + if (message is not IUserMessage userMessage) + { + await RespondInteraction($"Message for role menu `{menu.Name}` was not created by me! " + + $"Please delete and recreate the menu."); + return; + } + + var embed = userMessage.Embeds.FirstOrDefault(); + + if (embed == null) + { + await RespondInteraction($"Embed for role menu `{menu.Name}` could not be found!"); + return; + } + + var embedBuilder = embed.ToEmbedBuilder(); + + if (!string.IsNullOrEmpty(title)) + { + menu.Name = title; + embedBuilder.WithTitle(title); + await Database.SaveChangesAsync(); + } + + if (!string.IsNullOrEmpty(description)) + embedBuilder.WithDescription(description); + + if (!string.IsNullOrEmpty(colorHex)) + { + var color = Color.Teal; + + if (!string.IsNullOrEmpty(colorHex)) + color = new Color( + Convert.ToUInt32(colorHex.ToUpper().Replace("#", ""), 16) + ); + + embedBuilder.WithColor(color); + } + + var newEmbed = embedBuilder.Build(); + + await userMessage.ModifyAsync(m => m.Embed = newEmbed); + + await Database.SaveChangesAsync(); + + await RespondInteraction($"Role menu `{menu.Name}` is has now been edited!"); + } +} diff --git a/backend/Role Reactions/Commands/RemoveAssignedRole.cs b/backend/Role Reactions/Commands/RemoveAssignedRole.cs index d78e40e27..dbb238add 100644 --- a/backend/Role Reactions/Commands/RemoveAssignedRole.cs +++ b/backend/Role Reactions/Commands/RemoveAssignedRole.cs @@ -21,99 +21,95 @@ public async Task RemoveAssignedRoleCommand([Autocomplete(typeof(MenuHandler))] if (Context.Channel is ITextChannel txtChannel) channel = txtChannel; - if (channel != null) + if (channel == null) { - var menu = Database.RoleReactionsMenu.Find(channel.GuildId, channel.Id, menuId); - - if (menu == null) - { - await RespondInteraction($"Role menu `{menuId}` does not exist in this channel!"); - return; - } + await RespondInteraction(Translator.Get().OnlyTextChannel()); + return; + } - if (!menu.RoleToEmote.ContainsKey(role.Id)) - { - await RespondInteraction($"Role `{role.Name}` does not exist for role menu `{menu.Name}`!"); - return; - } + var menu = Database.RoleReactionsMenu.Find(channel.GuildId, channel.Id, menuId); - menu.RoleToEmote.Remove(role.Id); - await Database.SaveChangesAsync(); + if (menu == null) + { + await RespondInteraction($"Role menu `{menuId}` does not exist in this channel!"); + return; + } - var message = await channel.GetMessageAsync(menu.MessageId); + if (!menu.RoleToEmote.ContainsKey(role.Id)) + { + await RespondInteraction($"Role `{role.Name}` does not exist for role menu `{menu.Name}`!"); + return; + } - if (message == null) - { - await RespondInteraction($"Role menu `{menu.Name}` does not have a message related to it! " + - $"Please delete and recreate the menu."); - return; - } + menu.RoleToEmote.Remove(role.Id); + await Database.SaveChangesAsync(); - if (message is IUserMessage userMessage) - { - var rows = new List>(); - var tempComp = new Dictionary(); + var message = await channel.GetMessageAsync(menu.MessageId); - foreach (var storeRole in menu.RoleToEmote) - { - if (storeRole.Key == role.Id) - continue; + if (message == null) + { + await RespondInteraction($"Role menu `{menu.Name}` does not have a message related to it! " + + $"Please delete and recreate the menu."); + return; + } - tempComp.Add(storeRole.Key, storeRole.Value); + if (message is not IUserMessage userMessage) + { + await RespondInteraction($"Message for role menu `{menu.Name}` was not created by me! " + + $"Please delete and recreate the menu."); + return; + } - if (tempComp.Count >= 5) - { - rows.Add(tempComp); - tempComp = []; - } - } + var rows = new List>(); + var tempComp = new Dictionary(); - rows.Add(tempComp); + foreach (var storeRole in menu.RoleToEmote) + { + if (storeRole.Key == role.Id) + continue; - var components = new ComponentBuilder(); + tempComp.Add(storeRole.Key, storeRole.Value); - foreach (var row in rows) - { - var aRow = new ActionRowBuilder(); + if (tempComp.Count >= 5) + { + rows.Add(tempComp); + tempComp = []; + } + } - foreach (var col in row) - { - IEmote intEmote = null; + rows.Add(tempComp); - if (Emote.TryParse(col.Value, out var pEmote)) - intEmote = pEmote; + var components = new ComponentBuilder(); - if (Emoji.TryParse(col.Value, out var pEmoji)) - intEmote = pEmoji; + foreach (var row in rows) + { + var aRow = new ActionRowBuilder(); - var intRole = Context.Guild.GetRole(col.Key); + foreach (var col in row) + { + IEmote intEmote = null; - if (intRole != null) - aRow.WithButton( - intRole.Name, - $"add-rm-role:{intRole.Id},{Context.User.Id},{menu.Id}", - emote: intEmote - ); - } + if (Emote.TryParse(col.Value, out var pEmote)) + intEmote = pEmote; - components.AddRow(aRow); - } + if (Emoji.TryParse(col.Value, out var pEmoji)) + intEmote = pEmoji; - await userMessage.ModifyAsync(m => m.Components = components.Build()); + var intRole = Context.Guild.GetRole(col.Key); - await RespondInteraction($"Successfully removed role `{role.Name}` from menu `{menu.Name}`!"); - } - else - { - await RespondInteraction($"Message for role menu `{menu.Name}` was not created by me! " + - $"Please delete and recreate the menu."); - return; + if (intRole != null) + aRow.WithButton( + intRole.Name, + $"add-rm-role:{intRole.Id},{Context.User.Id},{menu.Id}", + emote: intEmote + ); } + + components.AddRow(aRow); } - else - { - await RespondInteraction(Translator.Get().OnlyTextChannel()); - return; - } + + await userMessage.ModifyAsync(m => m.Components = components.Build()); + + await RespondInteraction($"Successfully removed role `{role.Name}` from menu `{menu.Name}`!"); } }