Skip to content

Commit

Permalink
Added edit role menu command
Browse files Browse the repository at this point in the history
  • Loading branch information
FeroxFoxxo committed Oct 22, 2023
1 parent fdfb8d0 commit bd7d043
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 225 deletions.
166 changes: 81 additions & 85 deletions backend/Role Reactions/Commands/AddAssignedRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BotTranslator>().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<Dictionary<ulong, string>>();
var tempComp = new Dictionary<ulong, string>();
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<Dictionary<ulong, string>>();
var tempComp = new Dictionary<ulong, string>();

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<BotTranslator>().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:*,*,*")]
Expand Down
80 changes: 39 additions & 41 deletions backend/Role Reactions/Commands/CreateRoleMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,65 @@ public class CreateRoleMenu : RoleMenuCommand<CreateRoleMenu>

[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<BotTranslator>().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<ulong, string>()
}
);
Database.RoleReactionsMenu.Add(
new RoleMenu()
{
ChannelId = channel.Id,
GuildId = channel.GuildId,
Id = lowestId,
Name = title,
MessageId = msg.Id,
RoleToEmote = new Dictionary<ulong, string>()
}
);

await Database.SaveChangesAsync();
await Database.SaveChangesAsync();

await RespondInteraction($"Role menu `{title}` is now set up!");
}
else
{
await RespondInteraction(Translator.Get<BotTranslator>().OnlyTextChannel());
return;
}
await RespondInteraction($"Role menu `{title}` is now set up!");
}
}
51 changes: 24 additions & 27 deletions backend/Role Reactions/Commands/DeleteRoleMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,43 @@ public class DeleteRoleMenu : RoleMenuCommand<DeleteRoleMenu>

[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<BotTranslator>().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<BotTranslator>().OnlyTextChannel());
return;
}
await RespondInteraction($"Role menu `{menu.Name}` is now deleted!");
}
}
Loading

0 comments on commit bd7d043

Please sign in to comment.