From af56af49e776397b917dd41b0a1c924c936cbce7 Mon Sep 17 00:00:00 2001 From: Emotion Date: Sat, 8 Jul 2023 22:00:16 +1200 Subject: [PATCH] It is finished --- .gitignore | 6 +- Akheemah/Akheemah.csproj | 2 +- Akheemah/Commands/AddCategoryCommand.cs | 71 +++++ Akheemah/Commands/AddCommand.cs | 82 ++++++ Akheemah/Commands/AddMessageCommand.cs | 53 ++++ Akheemah/Commands/AddStaffCommand.cs | 67 +++++ Akheemah/Commands/AdminCommands.cs | 200 +++++++++++++ Akheemah/Commands/AssignCommand.cs | 102 +++++++ Akheemah/Commands/BlacklistCommand.cs | 54 ++++ Akheemah/Commands/CloseCommand.cs | 134 +++++++++ Akheemah/Commands/CreateButtonPanelCommand.cs | 81 ++++++ .../CreateSelectionBoxPanelCommand.cs | 79 +++++ Akheemah/Commands/ListAssignedCommand.cs | 63 ++++ Akheemah/Commands/ListCommand.cs | 99 +++++++ Akheemah/Commands/ListOpen.cs | 60 ++++ Akheemah/Commands/ListUnassignedCommand.cs | 61 ++++ Akheemah/Commands/MoveCommand.cs | 83 ++++++ Akheemah/Commands/NewCommand.cs | 275 ++++++++++++++++++ Akheemah/Commands/RandomAssignCommand.cs | 139 +++++++++ Akheemah/Commands/RemoveCategoryCommand.cs | 41 +++ Akheemah/Commands/RemoveMessageCommand.cs | 41 +++ Akheemah/Commands/RemoveStaffCommand.cs | 49 ++++ Akheemah/Commands/SayCommand.cs | 67 +++++ Akheemah/Commands/SetSummaryCommand.cs | 42 +++ Akheemah/Commands/StatusCommand.cs | 26 ++ Akheemah/Commands/SummaryCommand.cs | 35 +++ Akheemah/Commands/ToggleActiveCommand.cs | 44 +++ Akheemah/Commands/TranscriptActiveCommand.cs | 129 ++++++++ Akheemah/Commands/UnassignCommand.cs | 52 ++++ Akheemah/Commands/UnblacklistCommand.cs | 54 ++++ Akheemah/EventHandler.cs | 10 +- 31 files changed, 2294 insertions(+), 7 deletions(-) create mode 100644 Akheemah/Commands/AddCategoryCommand.cs create mode 100644 Akheemah/Commands/AddCommand.cs create mode 100644 Akheemah/Commands/AddMessageCommand.cs create mode 100644 Akheemah/Commands/AddStaffCommand.cs create mode 100644 Akheemah/Commands/AdminCommands.cs create mode 100644 Akheemah/Commands/AssignCommand.cs create mode 100644 Akheemah/Commands/BlacklistCommand.cs create mode 100644 Akheemah/Commands/CloseCommand.cs create mode 100644 Akheemah/Commands/CreateButtonPanelCommand.cs create mode 100644 Akheemah/Commands/CreateSelectionBoxPanelCommand.cs create mode 100644 Akheemah/Commands/ListAssignedCommand.cs create mode 100644 Akheemah/Commands/ListCommand.cs create mode 100644 Akheemah/Commands/ListOpen.cs create mode 100644 Akheemah/Commands/ListUnassignedCommand.cs create mode 100644 Akheemah/Commands/MoveCommand.cs create mode 100644 Akheemah/Commands/NewCommand.cs create mode 100644 Akheemah/Commands/RandomAssignCommand.cs create mode 100644 Akheemah/Commands/RemoveCategoryCommand.cs create mode 100644 Akheemah/Commands/RemoveMessageCommand.cs create mode 100644 Akheemah/Commands/RemoveStaffCommand.cs create mode 100644 Akheemah/Commands/SayCommand.cs create mode 100644 Akheemah/Commands/SetSummaryCommand.cs create mode 100644 Akheemah/Commands/StatusCommand.cs create mode 100644 Akheemah/Commands/SummaryCommand.cs create mode 100644 Akheemah/Commands/ToggleActiveCommand.cs create mode 100644 Akheemah/Commands/TranscriptActiveCommand.cs create mode 100644 Akheemah/Commands/UnassignCommand.cs create mode 100644 Akheemah/Commands/UnblacklistCommand.cs diff --git a/.gitignore b/.gitignore index 5f0946f..bb7f05e 100644 --- a/.gitignore +++ b/.gitignore @@ -335,4 +335,8 @@ Akheemah/config\.yml Akheemah/credentials.json -Akheemah/token.json/ \ No newline at end of file +Akheemah/token.json/ + +/Windows-x64 + +/Linux-x64 diff --git a/Akheemah/Akheemah.csproj b/Akheemah/Akheemah.csproj index c1b1266..9ba3024 100644 --- a/Akheemah/Akheemah.csproj +++ b/Akheemah/Akheemah.csproj @@ -13,7 +13,7 @@ https://toastielab.dev/Dragonschildstudios/Akheemah Git LICENSE - https://karlofduty.com/img/tardisIcon.jpg + https://cdn.discordapp.com/attachments/765441543100170271/1127164720374370336/Dragons_child_studios.jpg A Discord support ticket bot to help out in the Dragon's child studios server. en 1.3.1 diff --git a/Akheemah/Commands/AddCategoryCommand.cs b/Akheemah/Commands/AddCategoryCommand.cs new file mode 100644 index 0000000..602e240 --- /dev/null +++ b/Akheemah/Commands/AddCategoryCommand.cs @@ -0,0 +1,71 @@ +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class AddCategoryCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("addcategory", "Adds a category to the ticket bot letting users open tickets in them.")] + public async Task OnExecute(InteractionContext command, [Option("Title", "The name to display on buttons and in selection boxes.")] string title, [Option("Category", "The category to add.")] DiscordChannel category) + { + if (!category.IsCategory) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "That channel is not a category." + }, true); + return; + } + + if (string.IsNullOrWhiteSpace(title)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Invalid category title specified." + }, true); + return; + } + + if (Database.TryGetCategory(category.Id, out Database.Category _)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "That category is already registered." + }, true); + return; + } + + if (Database.TryGetCategory(title, out Database.Category _)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "There is already a category with that title." + }, true); + return; + } + + if (Database.AddCategory(title, category.Id)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Category added." + }, true); + } + else + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: Failed adding the category to the database." + }, true); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/AddCommand.cs b/Akheemah/Commands/AddCommand.cs new file mode 100644 index 0000000..875713f --- /dev/null +++ b/Akheemah/Commands/AddCommand.cs @@ -0,0 +1,82 @@ +using System; +using System.Threading.Tasks; +using DSharpPlus; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class AddCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("add", "Adds a user to a ticket")] + public async Task OnExecute(InteractionContext command, [Option("User", "User to add to ticket.")] DiscordUser user) + { + // Check if ticket exists in the database + if (!Database.IsOpenTicket(command.Channel.Id)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "This channel is not a ticket." + }, true); + return; + } + + DiscordMember member; + try + { + member = (user == null ? command.Member : await command.Guild.GetMemberAsync(user.Id)); + + if (member == null) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Could not find that user in this server." + }, true); + return; + } + } + catch (Exception) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Could not find that user in this server." + }, true); + return; + } + + try + { + await command.Channel.AddOverwriteAsync(member, Permissions.AccessChannels); + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Added " + member.Mention + " to ticket." + }); + + // Log it if the log channel exists + DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel); + if (logChannel != null) + { + await logChannel.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = member.Mention + " was added to " + command.Channel.Mention + + " by " + command.Member.Mention + "." + }); + } + } + catch (Exception) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Could not add " + member.Mention + " to ticket, unknown error occured." + }, true); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/AddMessageCommand.cs b/Akheemah/Commands/AddMessageCommand.cs new file mode 100644 index 0000000..8cf2529 --- /dev/null +++ b/Akheemah/Commands/AddMessageCommand.cs @@ -0,0 +1,53 @@ +using DSharpPlus.Entities; +using System.Threading.Tasks; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class AddMessageCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("addmessage", "Adds a new message for the 'say' command.")] + public async Task OnExecute(InteractionContext command, + [Option("Identifier", "The identifier word used in the /say command.")] string identifier, + [Option("Message", "The message the /say command will return.")] string message) + { + if (string.IsNullOrEmpty(message)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "No message specified." + }, true); + return; + } + + if (Database.TryGetMessage(identifier.ToLower(), out Database.Message _)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "There is already a message with that identifier." + }, true); + return; + } + + if (Database.AddMessage(identifier, command.Member.Id, message)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Message added." + }, true); + } + else + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: Failed adding the message to the database." + }, true); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/AddStaffCommand.cs b/Akheemah/Commands/AddStaffCommand.cs new file mode 100644 index 0000000..a4378ae --- /dev/null +++ b/Akheemah/Commands/AddStaffCommand.cs @@ -0,0 +1,67 @@ +using System; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; +using MySqlConnector; + +namespace Akheemah.Commands; + +public class AddStaffCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("addstaff", "Adds a new staff member.")] + public async Task OnExecute(InteractionContext command, [Option("User", "User to add to staff.")] DiscordUser user) + { + DiscordMember staffMember = null; + try + { + staffMember = user == null ? command.Member : await command.Guild.GetMemberAsync(user.Id); + + if (staffMember == null) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Could not find that user in this server." + }, true); + return; + } + } + catch (Exception) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Could not find that user in this server." + }, true); + return; + } + + await using MySqlConnection c = Database.GetConnection(); + MySqlCommand cmd = Database.IsStaff(staffMember.Id) ? new MySqlCommand(@"UPDATE staff SET name = @name WHERE user_id = @user_id", c) : new MySqlCommand(@"INSERT INTO staff (user_id, name) VALUES (@user_id, @name);", c); + + c.Open(); + cmd.Parameters.AddWithValue("@user_id", staffMember.Id); + cmd.Parameters.AddWithValue("@name", staffMember.DisplayName); + cmd.ExecuteNonQuery(); + cmd.Dispose(); + + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = staffMember.Mention + " was added to staff." + }); + + // Log it if the log channel exists + DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel); + if (logChannel != null) + { + await logChannel.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = staffMember.Mention + " was added to staff.\n" + }); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/AdminCommands.cs b/Akheemah/Commands/AdminCommands.cs new file mode 100644 index 0000000..2d35d6c --- /dev/null +++ b/Akheemah/Commands/AdminCommands.cs @@ -0,0 +1,200 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.Interactivity; +using DSharpPlus.Interactivity.Extensions; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +[SlashCommandGroup("admin", "Administrative commands.")] +public class AdminCommands : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("listinvalid", "List tickets which channels have been deleted. Use /admin unsetticket to remove them.")] + public async Task ListInvalid(InteractionContext command) + { + if (!Database.TryGetOpenTickets(out List openTickets)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Could not get any open tickets from database." + }, true); + } + + // Get all channels in all guilds the bot is part of + List allChannels = new List(); + foreach (KeyValuePair guild in Akheemah.discordClient.Guilds) + { + try + { + allChannels.AddRange(await guild.Value.GetChannelsAsync()); + } + catch (Exception) { /*ignored*/ } + } + + // Check which tickets channels no longer exist + List listItems = new List(); + foreach (Database.Ticket ticket in openTickets) + { + if (allChannels.All(channel => channel.Id != ticket.channelID)) + { + listItems.Add("ID: **" + ticket.id.ToString("00000") + ":** <#" + ticket.channelID + ">\n"); + } + } + + if (listItems.Count == 0) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "All tickets are valid!" + }, true); + return; + } + + List embeds = new List(); + foreach (string message in Utilities.ParseListIntoMessages(listItems)) + { + embeds.Add(new DiscordEmbedBuilder + { + Title = "Invalid tickets:", + Color = DiscordColor.Red, + Description = message + }); + } + + // Add the footers + for (int i = 0; i < embeds.Count; i++) + { + embeds[i].Footer = new DiscordEmbedBuilder.EmbedFooter + { + Text = $"Page {i + 1} / {embeds.Count}" + }; + } + + List listPages = new List(); + foreach (DiscordEmbedBuilder embed in embeds) + { + listPages.Add(new Page("", embed)); + } + + await command.Interaction.SendPaginatedResponseAsync(true, command.User, listPages); + } + + [SlashRequireGuild] + [SlashCommand("setticket", "Turns a channel into a ticket WARNING: Anyone will be able to delete the channel using /close.")] + public async Task SetTicket(InteractionContext command, [Option("User", "(Optional) The owner of the ticket.")] DiscordUser user = null) + { + // Check if ticket exists in the database + if (Database.IsOpenTicket(command.Channel.Id)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "This channel is already a ticket." + }, true); + return; + } + + DiscordUser ticketUser = (user == null ? command.User : user); + + long id = Database.NewTicket(ticketUser.Id, 0, command.Channel.Id); + string ticketID = id.ToString("00000"); + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Channel has been designated ticket " + ticketID + "." + }); + + // Log it if the log channel exists + DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel); + if (logChannel != null) + { + await logChannel.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = command.Channel.Mention + " has been designated ticket " + ticketID + " by " + command.Member.Mention + "." + }); + } + } + + [SlashRequireGuild] + [SlashCommand("unsetticket", "Deletes a ticket from the ticket system without deleting the channel.")] + public async Task UnsetTicket(InteractionContext command, [Option("TicketID", "(Optional) Ticket to unset. Uses the channel you are in by default.")] long ticketID = 0) + { + Database.Ticket ticket; + + if (ticketID == 0) + { + // Check if ticket exists in the database + if (!Database.TryGetOpenTicket(command.Channel.Id, out ticket)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "This channel is not a ticket!" + }, true); + return; + } + } + else + { + // Check if ticket exists in the database + if (!Database.TryGetOpenTicketByID((uint)ticketID, out ticket)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "There is no ticket with this ticket ID." + }, true); + return; + } + } + + + if (Database.DeleteOpenTicket(ticket.id)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Channel has been undesignated as a ticket." + }); + + // Log it if the log channel exists + DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel); + if (logChannel != null) + { + await logChannel.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = command.Channel.Mention + " has been undesignated as a ticket by " + command.Member.Mention + "." + }); + } + } + else + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: Failed removing ticket from database." + }, true); + } + } + + [SlashCommand("reload", "Reloads the bot config.")] + public async Task Reload(InteractionContext command) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Reloading bot application..." + }); + Logger.Log("Reloading bot..."); + Akheemah.Reload(); + } +} \ No newline at end of file diff --git a/Akheemah/Commands/AssignCommand.cs b/Akheemah/Commands/AssignCommand.cs new file mode 100644 index 0000000..1d25431 --- /dev/null +++ b/Akheemah/Commands/AssignCommand.cs @@ -0,0 +1,102 @@ +using System; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.Exceptions; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class AssignCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("assign", "Assigns a staff member to this ticket.")] + public async Task OnExecute(InteractionContext command, [Option("User", "(Optional) User to assign to this ticket.")] DiscordUser user = null) + { + DiscordMember member = null; + try + { + member = user == null ? command.Member : await command.Guild.GetMemberAsync(user.Id); + + if (member == null) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Could not find that user in this server." + }, true); + return; + } + } + catch (Exception) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Could not find that user in this server." + }, true); + return; + } + + // Check if ticket exists in the database + if (!Database.TryGetOpenTicket(command.Channel.Id, out Database.Ticket ticket)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "This channel is not a ticket." + }, true); + return; + } + + if (!Database.IsStaff(member.Id)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: User is not registered as staff." + }, true); + return; + } + + if (!Database.AssignStaff(ticket, member.Id)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: Failed to assign " + member.Mention + " to ticket." + }, true); + return; + } + + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Assigned " + member.Mention + " to ticket." + }); + + if (Config.assignmentNotifications) + { + try + { + await member.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "You have been assigned to a support ticket: " + command.Channel.Mention + }); + } + catch (UnauthorizedException) { } + } + + // Log it if the log channel exists + DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel); + if (logChannel != null) + { + await logChannel.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = member.Mention + " was assigned to " + command.Channel.Mention + " by " + command.Member.Mention + "." + }); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/BlacklistCommand.cs b/Akheemah/Commands/BlacklistCommand.cs new file mode 100644 index 0000000..0186c4b --- /dev/null +++ b/Akheemah/Commands/BlacklistCommand.cs @@ -0,0 +1,54 @@ +using System; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class BlacklistCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("blacklist", "Blacklists a user from opening tickets.")] + public async Task OnExecute(InteractionContext command, [Option("User", "User to blacklist.")] DiscordUser user) + { + try + { + if (!Database.Blacklist(user.Id, command.User.Id)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = user.Mention + " is already blacklisted." + }, true); + return; + } + + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Blacklisted " + user.Mention + "." + }, true); + + // Log it if the log channel exists + DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel); + if (logChannel != null) + { + await logChannel.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = user.Mention + " was blacklisted from opening tickets by " + command.Member.Mention + "." + }); + } + } + catch (Exception) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error occured while blacklisting " + user.Mention + "." + }, true); + throw; + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/CloseCommand.cs b/Akheemah/Commands/CloseCommand.cs new file mode 100644 index 0000000..8a579a1 --- /dev/null +++ b/Akheemah/Commands/CloseCommand.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using DSharpPlus; +using DSharpPlus.Entities; +using DSharpPlus.Exceptions; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class CloseCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("close", "Closes a ticket.")] + public async Task OnExecute(InteractionContext command) + { + // Check if ticket exists in the database + if (!Database.TryGetOpenTicket(command.Channel.Id, out Database.Ticket _)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "This channel is not a ticket." + }); + return; + } + + DiscordInteractionResponseBuilder confirmation = new DiscordInteractionResponseBuilder() + .AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Cyan, + Description = "Are you sure you wish to close this ticket? You cannot re-open it again later." + }) + .AddComponents(new DiscordButtonComponent(ButtonStyle.Danger, "akheemah_closeconfirm", "Confirm")); + + + await command.CreateResponseAsync(confirmation); + } + + public static async Task OnConfirmed(DiscordInteraction interaction) + { + await interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate); + ulong channelID = interaction.Channel.Id; + string channelName = interaction.Channel.Name; + + // Check if ticket exists in the database + if (!Database.TryGetOpenTicket(channelID, out Database.Ticket ticket)) + { + await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "This channel is not a ticket." + })); + return; + } + + // Build transcript + try + { + await Transcriber.ExecuteAsync(interaction.Channel.Id, ticket.id); + } + catch (Exception e) + { + Logger.Error("Exception occured when trying to save transcript while closing ticket: " + e); + await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "ERROR: Could not save transcript file. Aborting..." + })); + return; + } + + // Log it if the log channel exists + DiscordChannel logChannel = interaction.Guild.GetChannel(Config.logChannel); + if (logChannel != null) + { + DiscordEmbed embed = new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Ticket " + ticket.id.ToString("00000") + " closed by " + interaction.User.Mention + ".\n", + Footer = new DiscordEmbedBuilder.EmbedFooter { Text = '#' + channelName } + }; + + await using FileStream file = new FileStream(Transcriber.GetPath(ticket.id), FileMode.Open, FileAccess.Read); + DiscordMessageBuilder message = new DiscordMessageBuilder(); + message.WithEmbed(embed); + message.AddFiles(new Dictionary { { Transcriber.GetFilename(ticket.id), file } }); + + await logChannel.SendMessageAsync(message); + } + + if (Config.closingNotifications) + { + DiscordEmbed embed = new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Ticket " + ticket.id.ToString("00000") + " which you opened has now been closed, check the transcript for more info.\n", + Footer = new DiscordEmbedBuilder.EmbedFooter { Text = '#' + channelName } + }; + + try + { + DiscordMember staffMember = await interaction.Guild.GetMemberAsync(ticket.creatorID); + await using FileStream file = new FileStream(Transcriber.GetPath(ticket.id), FileMode.Open, FileAccess.Read); + + DiscordMessageBuilder message = new DiscordMessageBuilder(); + message.WithEmbed(embed); + message.AddFiles(new Dictionary { { Transcriber.GetFilename(ticket.id), file } }); + + await staffMember.SendMessageAsync(message); + } + catch (NotFoundException) { } + catch (UnauthorizedException) { } + } + + Database.ArchiveTicket(ticket); + + await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Channel will be deleted in 3 seconds..." + })); + + + await Task.Delay(3000); + + // Delete the channel and database entry + await interaction.Channel.DeleteAsync("Ticket closed."); + + Database.DeleteOpenTicket(ticket.id); + } +} \ No newline at end of file diff --git a/Akheemah/Commands/CreateButtonPanelCommand.cs b/Akheemah/Commands/CreateButtonPanelCommand.cs new file mode 100644 index 0000000..727bd6b --- /dev/null +++ b/Akheemah/Commands/CreateButtonPanelCommand.cs @@ -0,0 +1,81 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DSharpPlus; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class CreateButtonPanelCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("createbuttonpanel", "Creates a series of buttons which users can use to open new tickets in specific categories.")] + public async Task OnExecute(InteractionContext command) + { + DiscordMessageBuilder builder = new DiscordMessageBuilder().WithContent(" "); + List verifiedCategories = await Utilities.GetVerifiedChannels(); + + if (verifiedCategories.Count == 0) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: No registered categories found." + }, true); + return; + } + + verifiedCategories = verifiedCategories.OrderBy(x => x.name).ToList(); + + int nrOfButtons = 0; + for (int nrOfButtonRows = 0; nrOfButtonRows < 5 && nrOfButtons < verifiedCategories.Count; nrOfButtonRows++) + { + List buttonRow = new List(); + + for (; nrOfButtons < 5 * (nrOfButtonRows + 1) && nrOfButtons < verifiedCategories.Count; nrOfButtons++) + { + buttonRow.Add(new DiscordButtonComponent(ButtonStyle.Primary, "akheemah_newticketbutton " + verifiedCategories[nrOfButtons].id, verifiedCategories[nrOfButtons].name)); + } + builder.AddComponents(buttonRow); + } + + await command.Channel.SendMessageAsync(builder); + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Successfully created message, make sure to run this command again if you add new categories to the bot." + }, true); + } + + public static async Task OnButtonUsed(DiscordInteraction interaction) + { + await interaction.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral()); + + if (!ulong.TryParse(interaction.Data.CustomId.Replace("akheemah_newticketbutton ", ""), out ulong categoryID) || categoryID == 0) + { + Logger.Warn("Invalid ID: " + interaction.Data.CustomId.Replace("akheemah_newticketbutton ", "")); + return; + } + + (bool success, string message) = await NewCommand.OpenNewTicket(interaction.User.Id, interaction.ChannelId, categoryID); + + if (success) + { + await interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = message + })); + } + else + { + await interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = message + })); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/CreateSelectionBoxPanelCommand.cs b/Akheemah/Commands/CreateSelectionBoxPanelCommand.cs new file mode 100644 index 0000000..d5b2a15 --- /dev/null +++ b/Akheemah/Commands/CreateSelectionBoxPanelCommand.cs @@ -0,0 +1,79 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DSharpPlus; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class CreateSelectionBoxPanelCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("createselectionboxpanel", "Creates a selection box which users can use to open new tickets in specific categories.")] + public async Task OnExecute(InteractionContext command, [Option("Message", "(Optional) The message to show in the selection box.")] string message = null) + { + DiscordMessageBuilder builder = new DiscordMessageBuilder() + .WithContent(" ") + .AddComponents(await GetSelectComponents(command, message ?? "Open new ticket...")); + + await command.Channel.SendMessageAsync(builder); + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Successfully created message, make sure to run this command again if you add new categories to the bot." + }, true); + } + + public static async Task> GetSelectComponents(InteractionContext command, string placeholder) + { + List verifiedCategories = await Utilities.GetVerifiedChannels(); + + if (verifiedCategories.Count == 0) return new List(); + + verifiedCategories = verifiedCategories.OrderBy(x => x.name).ToList(); + List selectionComponents = new List(); + int selectionOptions = 0; + for (int selectionBoxes = 0; selectionBoxes < 5 && selectionOptions < verifiedCategories.Count; selectionBoxes++) + { + List categoryOptions = new List(); + + for (; selectionOptions < 25 * (selectionBoxes + 1) && selectionOptions < verifiedCategories.Count; selectionOptions++) + { + categoryOptions.Add(new DiscordSelectComponentOption(verifiedCategories[selectionOptions].name, verifiedCategories[selectionOptions].id.ToString())); + } + selectionComponents.Add(new DiscordSelectComponent("akheemah_newticketselector" + selectionBoxes, placeholder, categoryOptions, false, 0, 1)); + } + + return selectionComponents; + } + + public static async Task OnSelectionMenuUsed(DiscordInteraction interaction) + { + if (interaction.Data.Values == null || interaction.Data.Values.Length <= 0) return; + + if (!ulong.TryParse(interaction.Data.Values[0], out ulong categoryID) || categoryID == 0) return; + + await interaction.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new DiscordInteractionResponseBuilder().AsEphemeral()); + + (bool success, string message) = await NewCommand.OpenNewTicket(interaction.User.Id, interaction.ChannelId, categoryID); + + if (success) + { + await interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = message + }).AsEphemeral()); + } + else + { + await interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = message + }).AsEphemeral()); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/ListAssignedCommand.cs b/Akheemah/Commands/ListAssignedCommand.cs new file mode 100644 index 0000000..28ac62a --- /dev/null +++ b/Akheemah/Commands/ListAssignedCommand.cs @@ -0,0 +1,63 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.Interactivity; +using DSharpPlus.Interactivity.Extensions; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class ListAssignedCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("listassigned", "Lists tickets assigned to a user.")] + public async Task OnExecute(InteractionContext command, [Option("User", "(Optional) User to list tickets for.")] DiscordUser user = null) + { + DiscordUser listUser = user == null ? command.User : user; + + if (!Database.TryGetAssignedTickets(listUser.Id, out List assignedTickets)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "User does not have any assigned tickets." + }); + return; + } + + List listItems = new List(); + foreach (Database.Ticket ticket in assignedTickets) + { + listItems.Add("**" + ticket.DiscordRelativeTime() + ":** <#" + ticket.channelID + "> by <@" + ticket.creatorID + ">\n"); + } + + List embeds = new List(); + foreach (string message in Utilities.ParseListIntoMessages(listItems)) + { + embeds.Add(new DiscordEmbedBuilder + { + Title = "Assigned tickets: ", + Color = DiscordColor.Green, + Description = message + }); + } + + // Add the footers + for (int i = 0; i < embeds.Count; i++) + { + embeds[i].Footer = new DiscordEmbedBuilder.EmbedFooter + { + Text = $"Page {i + 1} / {embeds.Count}" + }; + } + + List listPages = new List(); + foreach (DiscordEmbedBuilder embed in embeds) + { + listPages.Add(new Page("", embed)); + } + + await command.Interaction.SendPaginatedResponseAsync(true, command.User, listPages); + } +} \ No newline at end of file diff --git a/Akheemah/Commands/ListCommand.cs b/Akheemah/Commands/ListCommand.cs new file mode 100644 index 0000000..4a51c5e --- /dev/null +++ b/Akheemah/Commands/ListCommand.cs @@ -0,0 +1,99 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.Interactivity; +using DSharpPlus.Interactivity.Extensions; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class ListCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("list", "Lists tickets opened by a user.")] + public async Task OnExecute(InteractionContext command, [Option("User", "(Optional) The user to get tickets by.")] DiscordUser user = null) + { + DiscordUser listUser = user == null ? command.User : user; + + List openEmbeds = new List(); + if (Database.TryGetOpenTickets(listUser.Id, out List openTickets)) + { + List listItems = new List(); + foreach (Database.Ticket ticket in openTickets) + { + listItems.Add("**" + ticket.DiscordRelativeTime() + ":** <#" + ticket.channelID + ">\n"); + } + + foreach (string message in Utilities.ParseListIntoMessages(listItems)) + { + openEmbeds.Add(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = message + }); + } + + // Add the titles + for (int i = 0; i < openEmbeds.Count; i++) + { + openEmbeds[i].Title = $"Open tickets ({i + 1}/{openEmbeds.Count})"; + } + } + + List closedEmbeds = new List(); + if (Database.TryGetClosedTickets(listUser.Id, out List closedTickets)) + { + List listItems = new List(); + foreach (Database.Ticket ticket in closedTickets) + { + listItems.Add("**" + ticket.DiscordRelativeTime() + ":** Ticket " + ticket.id.ToString("00000") + "\n"); + } + + foreach (string message in Utilities.ParseListIntoMessages(listItems)) + { + closedEmbeds.Add(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = message + }); + } + + // Add the titles + for (int i = 0; i < closedEmbeds.Count; i++) + { + closedEmbeds[i].Title = $"Closed tickets ({i + 1}/{closedEmbeds.Count})"; + } + } + + // Merge the embed lists and add the footers + List embeds = new List(); + embeds.AddRange(openEmbeds); + embeds.AddRange(closedEmbeds); + for (int i = 0; i < embeds.Count; i++) + { + embeds[i].Footer = new DiscordEmbedBuilder.EmbedFooter + { + Text = $"Page {i + 1} / {embeds.Count}" + }; + } + + if (embeds.Count == 0) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Cyan, + Description = "User does not have any open or closed tickets." + }); + return; + } + + List listPages = new List(); + foreach (DiscordEmbedBuilder embed in embeds) + { + listPages.Add(new Page("", embed)); + } + + await command.Interaction.SendPaginatedResponseAsync(true, command.User, listPages); + } +} \ No newline at end of file diff --git a/Akheemah/Commands/ListOpen.cs b/Akheemah/Commands/ListOpen.cs new file mode 100644 index 0000000..cbd85de --- /dev/null +++ b/Akheemah/Commands/ListOpen.cs @@ -0,0 +1,60 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.Interactivity; +using DSharpPlus.Interactivity.Extensions; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class ListOpen : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("listopen", "Lists all open tickets, oldest first.")] + public async Task OnExecute(InteractionContext command) + { + if (!Database.TryGetOpenTickets(out List openTickets)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Could not fetch any open tickets." + }); + return; + } + + List listItems = new List(); + foreach (Database.Ticket ticket in openTickets) + { + listItems.Add("**" + ticket.DiscordRelativeTime() + ":** <#" + ticket.channelID + "> by <@" + ticket.creatorID + ">\n"); + } + + List embeds = new List(); + foreach (string message in Utilities.ParseListIntoMessages(listItems)) + { + embeds.Add(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = message + }); + } + + // Add the footers + for (int i = 0; i < embeds.Count; i++) + { + embeds[i].Footer = new DiscordEmbedBuilder.EmbedFooter + { + Text = $"Page {i + 1} / {embeds.Count}" + }; + } + + List listPages = new List(); + foreach (DiscordEmbedBuilder embed in embeds) + { + listPages.Add(new Page("", embed)); + } + + await command.Interaction.SendPaginatedResponseAsync(true, command.User, listPages); + } +} \ No newline at end of file diff --git a/Akheemah/Commands/ListUnassignedCommand.cs b/Akheemah/Commands/ListUnassignedCommand.cs new file mode 100644 index 0000000..b7dd9f4 --- /dev/null +++ b/Akheemah/Commands/ListUnassignedCommand.cs @@ -0,0 +1,61 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.Interactivity; +using DSharpPlus.Interactivity.Extensions; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class ListUnassignedCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("listunassigned", "Lists unassigned tickets.")] + public async Task OnExecute(InteractionContext command) + { + if (!Database.TryGetAssignedTickets(0, out List unassignedTickets)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "There are no unassigned tickets." + }); + return; + } + + List listItems = new List(); + foreach (Database.Ticket ticket in unassignedTickets) + { + listItems.Add("**" + ticket.DiscordRelativeTime() + ":** <#" + ticket.channelID + "> by <@" + ticket.creatorID + ">\n"); + } + + List embeds = new List(); + foreach (string message in Utilities.ParseListIntoMessages(listItems)) + { + embeds.Add(new DiscordEmbedBuilder + { + Title = "Unassigned tickets: ", + Color = DiscordColor.Green, + Description = message + }); + } + + // Add the footers + for (int i = 0; i < embeds.Count; i++) + { + embeds[i].Footer = new DiscordEmbedBuilder.EmbedFooter + { + Text = $"Page {i + 1} / {embeds.Count}" + }; + } + + List listPages = new List(); + foreach (DiscordEmbedBuilder embed in embeds) + { + listPages.Add(new Page("", embed)); + } + + await command.Interaction.SendPaginatedResponseAsync(true, command.User, listPages); + } +} \ No newline at end of file diff --git a/Akheemah/Commands/MoveCommand.cs b/Akheemah/Commands/MoveCommand.cs new file mode 100644 index 0000000..7fa05be --- /dev/null +++ b/Akheemah/Commands/MoveCommand.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.Exceptions; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class MoveCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("move", "Moves a ticket to another category.")] + public async Task OnExecute(InteractionContext command, [Option("Category", "The category to move the ticket to. Only has to be the beginning of the name.")] string category) + { + // Check if ticket exists in the database + if (!Database.TryGetOpenTicket(command.Channel.Id, out Database.Ticket _)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "This channel is not a ticket." + }, true); + return; + } + + if (string.IsNullOrEmpty(category)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: No category provided." + }, true); + return; + } + + IReadOnlyList channels = await command.Guild.GetChannelsAsync(); + IEnumerable categories = channels.Where(x => x.IsCategory); + DiscordChannel categoryChannel = categories.FirstOrDefault(x => x.Name.StartsWith(category.Trim(), StringComparison.OrdinalIgnoreCase)); + + if (categoryChannel == null) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: Could not find a category by that name." + }, true); + return; + } + + if (command.Channel.Id == categoryChannel.Id) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: The ticket is already in that category." + }, true); + return; + } + + try + { + await command.Channel.ModifyAsync(modifiedAttributes => modifiedAttributes.Parent = categoryChannel); + } + catch (UnauthorizedException) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: Not authorized to move this ticket to that category." + }, true); + return; + } + + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Ticket was moved to " + categoryChannel.Mention + }); + } +} \ No newline at end of file diff --git a/Akheemah/Commands/NewCommand.cs b/Akheemah/Commands/NewCommand.cs new file mode 100644 index 0000000..d67ec4c --- /dev/null +++ b/Akheemah/Commands/NewCommand.cs @@ -0,0 +1,275 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DSharpPlus; +using DSharpPlus.Entities; +using DSharpPlus.Exceptions; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; +public class NewCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("new", "Opens a new ticket.")] + public async Task OnExecute(InteractionContext command) + { + List verifiedCategories = await Utilities.GetVerifiedChannels(); + switch (verifiedCategories.Count) + { + case 0: + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: No registered categories found." + }, true); + return; + case 1: + await command.DeferAsync(true); + (bool success, string message) = await OpenNewTicket(command.User.Id, command.Channel.Id, verifiedCategories[0].id); + + if (success) + { + await command.FollowUpAsync(new DiscordFollowupMessageBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = message + }).AsEphemeral()); + } + else + { + await command.FollowUpAsync(new DiscordFollowupMessageBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = message + }).AsEphemeral()); + } + return; + default: + if (Config.newCommandUsesSelector) + { + await CreateSelector(command, verifiedCategories); + } + else + { + await CreateButtons(command, verifiedCategories); + } + return; + } + } + + public static async Task CreateButtons(InteractionContext command, List verifiedCategories) + { + DiscordInteractionResponseBuilder builder = new DiscordInteractionResponseBuilder().WithContent(" "); + int nrOfButtons = 0; + for (int nrOfButtonRows = 0; nrOfButtonRows < 5 && nrOfButtons < verifiedCategories.Count; nrOfButtonRows++) + { + List buttonRow = new List(); + + for (; nrOfButtons < 5 * (nrOfButtonRows + 1) && nrOfButtons < verifiedCategories.Count; nrOfButtons++) + { + buttonRow.Add(new DiscordButtonComponent(ButtonStyle.Primary, "akheemah_newcommandbutton " + verifiedCategories[nrOfButtons].id, verifiedCategories[nrOfButtons].name)); + } + builder.AddComponents(buttonRow); + } + + await command.CreateResponseAsync(builder.AsEphemeral()); + } + + public static async Task CreateSelector(InteractionContext command, List verifiedCategories) + { + verifiedCategories = verifiedCategories.OrderBy(x => x.name).ToList(); + List selectionComponents = new List(); + int selectionOptions = 0; + for (int selectionBoxes = 0; selectionBoxes < 5 && selectionOptions < verifiedCategories.Count; selectionBoxes++) + { + List categoryOptions = new List(); + + for (; selectionOptions < 25 * (selectionBoxes + 1) && selectionOptions < verifiedCategories.Count; selectionOptions++) + { + categoryOptions.Add(new DiscordSelectComponentOption(verifiedCategories[selectionOptions].name, verifiedCategories[selectionOptions].id.ToString())); + } + selectionComponents.Add(new DiscordSelectComponent("akheemah_newcommandselector" + selectionBoxes, "Open new ticket...", categoryOptions, false, 0, 1)); + } + + await command.CreateResponseAsync(new DiscordInteractionResponseBuilder().AddComponents(selectionComponents).AsEphemeral()); + } + + public static async Task OnCategorySelection(DiscordInteraction interaction) + { + string stringID; + switch (interaction.Data.ComponentType) + { + case ComponentType.Button: + stringID = interaction.Data.CustomId.Replace("akheemah_newcommandbutton ", ""); + break; + case ComponentType.StringSelect: + if (interaction.Data.Values == null || interaction.Data.Values.Length <= 0) return; + stringID = interaction.Data.Values[0]; + break; + + case ComponentType.ActionRow: + case ComponentType.FormInput: + default: + return; + } + + if (!ulong.TryParse(stringID, out ulong categoryID) || categoryID == 0) return; + + await interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate, new DiscordInteractionResponseBuilder().AsEphemeral()); + + (bool success, string message) = await OpenNewTicket(interaction.User.Id, interaction.ChannelId, categoryID); + + if (success) + { + await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = message + })); + } + else + { + await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = message + })); + } + } + + public static async Task<(bool, string)> OpenNewTicket(ulong userID, ulong commandChannelID, ulong categoryID) + { + // Check if user is blacklisted + if (Database.IsBlacklisted(userID)) + { + return (false, "You are banned from opening tickets."); + } + + if (Database.IsOpenTicket(commandChannelID)) + { + return (false, "You cannot use this command in a ticket channel."); + } + + if (!Database.IsStaff(userID) && Database.TryGetOpenTickets(userID, out List ownTickets) && ownTickets.Count >= Config.ticketLimit) + { + return (false, "You have reached the limit for maximum open tickets."); + } + + DiscordChannel category = null; + try + { + category = await Akheemah.discordClient.GetChannelAsync(categoryID); + } + catch (Exception) { /*ignored*/ } + + if (category == null) + { + return (false, "Error: Could not find the category to place the ticket in."); + } + + DiscordMember member = null; + try + { + member = await category.Guild.GetMemberAsync(userID); + } + catch (Exception) { /*ignored*/ } + + if (member == null) + { + return (false, "Error: Could not find you on the Discord server."); + } + + DiscordChannel ticketChannel = null; + + try + { + ticketChannel = await category.Guild.CreateChannelAsync("ticket", ChannelType.Text, category); + } + catch (Exception) { /* ignored */ } + + if (ticketChannel == null) + { + return (false, "Error occured while creating ticket, " + member.Mention + + "!\nIs the channel limit reached in the server or ticket category?"); + } + + ulong staffID = 0; + if (Config.randomAssignment) + { + staffID = Database.GetRandomActiveStaff(0)?.userID ?? 0; + } + + long id = Database.NewTicket(member.Id, staffID, ticketChannel.Id); + string ticketID = id.ToString("00000"); + + try + { + await ticketChannel.ModifyAsync(modifiedAttributes => modifiedAttributes.Name = "ticket-" + ticketID); + } + catch (DiscordException e) + { + Logger.Error("Exception occurred trying to modify channel: " + e); + Logger.Error("JsomMessage: " + e.JsonMessage); + } + + try + { + await ticketChannel.AddOverwriteAsync(member, Permissions.AccessChannels); + } + catch (DiscordException e) + { + Logger.Error("Exception occurred trying to add channel permissions: " + e); + Logger.Error("JsomMessage: " + e.JsonMessage); + } + + await ticketChannel.SendMessageAsync("Hello, " + member.Mention + "!\n" + Config.welcomeMessage); + + // Refreshes the channel as changes were made to it above + ticketChannel = await Akheemah.discordClient.GetChannelAsync(ticketChannel.Id); + + if (staffID != 0) + { + await ticketChannel.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Ticket was randomly assigned to <@" + staffID + ">." + }); + + if (Config.assignmentNotifications) + { + try + { + DiscordMember staffMember = await category.Guild.GetMemberAsync(staffID); + await staffMember.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "You have been randomly assigned to a newly opened support ticket: " + + ticketChannel.Mention + }); + } + catch (DiscordException e) + { + Logger.Error("Exception occurred assign random staff member: " + e); + Logger.Error("JsomMessage: " + e.JsonMessage); + } + } + } + + // Log it if the log channel exists + DiscordChannel logChannel = category.Guild.GetChannel(Config.logChannel); + if (logChannel != null) + { + DiscordEmbed logMessage = new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Ticket " + ticketChannel.Mention + " opened by " + member.Mention + ".\n", + Footer = new DiscordEmbedBuilder.EmbedFooter { Text = "Ticket " + ticketID } + }; + await logChannel.SendMessageAsync(logMessage); + } + + return (true, "Ticket opened, " + member.Mention + "!\n" + ticketChannel.Mention); + } +} \ No newline at end of file diff --git a/Akheemah/Commands/RandomAssignCommand.cs b/Akheemah/Commands/RandomAssignCommand.cs new file mode 100644 index 0000000..cce6e0b --- /dev/null +++ b/Akheemah/Commands/RandomAssignCommand.cs @@ -0,0 +1,139 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.Exceptions; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; +using Microsoft.Extensions.Logging; + +namespace Akheemah.Commands; + +public class RandomAssignCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("rassign", "Randomly assigns a staff member to a ticket.")] + public async Task OnExecute(InteractionContext command, [Option("Role", "(Optional) Limit the random assignment to a specific role.")] DiscordRole role = null) + { + // Check if ticket exists in the database + if (!Database.TryGetOpenTicket(command.Channel.Id, out Database.Ticket ticket)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: This channel is not a ticket." + }, true); + return; + } + + // Get a random staff member who is verified to have the correct role if applicable + DiscordMember staffMember = await GetRandomVerifiedStaffMember(command, role, ticket); + if (staffMember == null) + { + return; + } + + // Attempt to assign the staff member to the ticket + if (!Database.AssignStaff(ticket, staffMember.Id)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: Failed to assign " + staffMember.Mention + " to ticket." + }, true); + return; + } + + // Respond that the command was successful + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Randomly assigned " + staffMember.Mention + " to ticket." + }); + + // Send a notification to the staff member if applicable + if (Config.assignmentNotifications) + { + try + { + await staffMember.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "You have been randomly assigned to a support ticket: " + command.Channel.Mention + }); + } + catch (UnauthorizedException) { } + } + + // Log it if the log channel exists + DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel); + if (logChannel != null) + { + await logChannel.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = staffMember.Mention + " was randomly assigned to " + command.Channel.Mention + " by " + command.Member.Mention + "." + }); + } + } + + private static async Task GetRandomVerifiedStaffMember(InteractionContext command, DiscordRole targetRole, Database.Ticket ticket) + { + if (targetRole != null) // A role was provided + { + // Check if role rassign should override staff's active status + List staffMembers = Config.randomAssignRoleOverride + ? Database.GetAllStaff(ticket.assignedStaffID, ticket.creatorID) + : Database.GetActiveStaff(ticket.assignedStaffID, ticket.creatorID); + + // Randomize the list before checking for roles in order to reduce number of API calls + staffMembers.Shuffle(); + + // Get the first staff member that has the role + foreach (Database.StaffMember sm in staffMembers) + { + try + { + DiscordMember verifiedMember = await command.Guild.GetMemberAsync(sm.userID); + if (verifiedMember?.Roles?.Any(role => role.Id == targetRole.Id) ?? false) + { + return verifiedMember; + } + } + catch (Exception e) + { + command.Client.Logger.Log(LogLevel.Information, e, "Error occured trying to find a staff member in the rassign command."); + } + } + } + else // No role was specified, any active staff will be picked + { + Database.StaffMember staffEntry = Database.GetRandomActiveStaff(ticket.assignedStaffID, ticket.creatorID); + if (staffEntry == null) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: There are no other staff members to choose from." + }, true); + return null; + } + + // Get the staff member from discord + try + { + return await command.Guild.GetMemberAsync(staffEntry.userID); + } + catch (NotFoundException) { } + } + + // Send a more generic error if we get to this point and still haven't found the staff member + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: Could not find an applicable staff member." + }, true); + return null; + } +} \ No newline at end of file diff --git a/Akheemah/Commands/RemoveCategoryCommand.cs b/Akheemah/Commands/RemoveCategoryCommand.cs new file mode 100644 index 0000000..7337927 --- /dev/null +++ b/Akheemah/Commands/RemoveCategoryCommand.cs @@ -0,0 +1,41 @@ +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands.Attributes; +using DSharpPlus.SlashCommands; +using System.Threading.Tasks; + +namespace Akheemah.Commands; + +public class RemoveCategoryCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("removecategory", "Removes the ability for users to open tickets in a specific category.")] + public async Task OnExecute(InteractionContext command, [Option("Category", "The category to remove.")] DiscordChannel channel) + { + if (!Database.TryGetCategory(channel.Id, out Database.Category _)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "That category is not registered." + }, true); + return; + } + + if (Database.RemoveCategory(channel.Id)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Category removed." + }, true); + } + else + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: Failed removing the category from the database." + }, true); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/RemoveMessageCommand.cs b/Akheemah/Commands/RemoveMessageCommand.cs new file mode 100644 index 0000000..b10c42f --- /dev/null +++ b/Akheemah/Commands/RemoveMessageCommand.cs @@ -0,0 +1,41 @@ +using DSharpPlus.Entities; +using System.Threading.Tasks; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class RemoveMessageCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("removemessage", "Removes a message from the 'say' command.")] + public async Task OnExecute(InteractionContext command, [Option("Identifier", "The identifier word used in the /say command.")] string identifier) + { + if (!Database.TryGetMessage(identifier.ToLower(), out Database.Message _)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "There is no message with that identifier." + }, true); + return; + } + + if (Database.RemoveMessage(identifier)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Message removed." + }, true); + } + else + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: Failed removing the message from the database." + }, true); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/RemoveStaffCommand.cs b/Akheemah/Commands/RemoveStaffCommand.cs new file mode 100644 index 0000000..6cbfaf7 --- /dev/null +++ b/Akheemah/Commands/RemoveStaffCommand.cs @@ -0,0 +1,49 @@ +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; +using MySqlConnector; + +namespace Akheemah.Commands; + +public class RemoveStaffCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("removestaff", "Removes a staff member.")] + public async Task OnExecute(InteractionContext command, [Option("User", "User to remove from staff.")] DiscordUser user) + { + if (!Database.IsStaff(user.Id)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "User is already not registered as staff." + }, true); + return; + } + + await using MySqlConnection c = Database.GetConnection(); + c.Open(); + MySqlCommand deletion = new MySqlCommand(@"DELETE FROM staff WHERE user_id=@user_id", c); + deletion.Parameters.AddWithValue("@user_id", user.Id); + deletion.Prepare(); + deletion.ExecuteNonQuery(); + + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "User was removed from staff." + }, true); + + // Log it if the log channel exists + DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel); + if (logChannel != null) + { + await logChannel.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "User was removed from staff.\n" + }); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/SayCommand.cs b/Akheemah/Commands/SayCommand.cs new file mode 100644 index 0000000..8c18724 --- /dev/null +++ b/Akheemah/Commands/SayCommand.cs @@ -0,0 +1,67 @@ +using DSharpPlus.Entities; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class SayCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("say", "Prints a message with information from staff. Use without identifier to list all identifiers.")] + public async Task OnExecute(InteractionContext command, [Option("Identifier", "(Optional) The identifier word to summon a message.")] string identifier = null) + { + // Print list of all messages if no identifier is provided + if (identifier == null) + { + List messages = Database.GetAllMessages(); + if (!messages.Any()) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "There are no messages registered." + }, true); + return; + } + + List listItems = new List(); + foreach (Database.Message message in messages) + { + listItems.Add("**" + message.identifier + "** Added by <@" + message.userID + ">\n"); + } + + LinkedList listMessages = Utilities.ParseListIntoMessages(listItems); + foreach (string listMessage in listMessages) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Title = "Available messages: ", + Color = DiscordColor.Green, + Description = listMessage + }, true); + } + } + // Print specific message + else + { + if (!Database.TryGetMessage(identifier.ToLower(), out Database.Message message)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "There is no message with that identifier." + }, true); + return; + } + + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Cyan, + Description = message.message.Replace("\\n", "\n") + }); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/SetSummaryCommand.cs b/Akheemah/Commands/SetSummaryCommand.cs new file mode 100644 index 0000000..dace5ca --- /dev/null +++ b/Akheemah/Commands/SetSummaryCommand.cs @@ -0,0 +1,42 @@ +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; +using MySqlConnector; + +namespace Akheemah.Commands; + +public class SetSummaryCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("setsummary", "Sets a ticket's summary for the summary command.")] + public async Task OnExecute(InteractionContext command, [Option("Summary", "The ticket summary text.")] string summary) + { + ulong channelID = command.Channel.Id; + // Check if ticket exists in the database + if (!Database.TryGetOpenTicket(command.Channel.Id, out Database.Ticket _)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "This channel is not a ticket." + }); + return; + } + + await using MySqlConnection c = Database.GetConnection(); + c.Open(); + MySqlCommand update = new MySqlCommand(@"UPDATE tickets SET summary = @summary WHERE channel_id = @channel_id", c); + update.Parameters.AddWithValue("@summary", summary); + update.Parameters.AddWithValue("@channel_id", channelID); + update.Prepare(); + update.ExecuteNonQuery(); + update.Dispose(); + + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Summary set." + }, true); + } +} \ No newline at end of file diff --git a/Akheemah/Commands/StatusCommand.cs b/Akheemah/Commands/StatusCommand.cs new file mode 100644 index 0000000..c61dbae --- /dev/null +++ b/Akheemah/Commands/StatusCommand.cs @@ -0,0 +1,26 @@ +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class StatusCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("status", "Shows bot status and information.")] + public async Task OnExecute(InteractionContext command) + { + long openTickets = Database.GetNumberOfTickets(); + long closedTickets = Database.GetNumberOfClosedTickets(); + + DiscordEmbed botInfo = new DiscordEmbedBuilder() + .WithAuthor("Dragon's child ctudios/Akheemah @ Taostielab", "https://toastielab.dev/Dragonschildstudios/Akheemah", "https://cdn.discordapp.com/attachments/765441543100170271/1127164720374370336/Dragons_child_studios.jpg") + .WithTitle("Bot information") + .WithColor(DiscordColor.Cyan) + .AddField("Version:", Akheemah.GetVersion()) + .AddField("Open tickets:", openTickets + "", true) + .AddField("Closed tickets:", closedTickets + " ", true); + await command.CreateResponseAsync(botInfo); + } +} \ No newline at end of file diff --git a/Akheemah/Commands/SummaryCommand.cs b/Akheemah/Commands/SummaryCommand.cs new file mode 100644 index 0000000..5a35d4d --- /dev/null +++ b/Akheemah/Commands/SummaryCommand.cs @@ -0,0 +1,35 @@ +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class SummaryCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("summary", "Lists tickets assigned to a user.")] + public async Task OnExecute(InteractionContext command) + { + if (Database.TryGetOpenTicket(command.Channel.Id, out Database.Ticket ticket)) + { + DiscordEmbed channelInfo = new DiscordEmbedBuilder() + .WithTitle("Channel information") + .WithColor(DiscordColor.Cyan) + .AddField("Ticket number:", ticket.id.ToString("00000"), true) + .AddField("Ticket creator:", $"<@{ticket.creatorID}>", true) + .AddField("Assigned staff:", ticket.assignedStaffID == 0 ? "Unassigned." : $"<@{ticket.assignedStaffID}>", true) + .AddField("Creation time:", ticket.DiscordRelativeTime(), true) + .AddField("Summary:", string.IsNullOrEmpty(ticket.summary) ? "No summary." : ticket.summary.Replace("\\n", "\n")); + await command.CreateResponseAsync(channelInfo); + } + else + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "This channel is not a ticket." + }, true); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/ToggleActiveCommand.cs b/Akheemah/Commands/ToggleActiveCommand.cs new file mode 100644 index 0000000..46a4b26 --- /dev/null +++ b/Akheemah/Commands/ToggleActiveCommand.cs @@ -0,0 +1,44 @@ +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class ToggleActiveCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("toggleactive", "Toggles active status for a staff member.")] + public async Task OnExecute(InteractionContext command, [Option("User", "(Optional) Staff member to toggle activity for.")] DiscordUser user = null) + { + DiscordUser staffUser = user == null ? command.User : user; + + // Check if ticket exists in the database + if (!Database.TryGetStaff(staffUser.Id, out Database.StaffMember staffMember)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = user == null ? "You have not been registered as staff." : "The user is not registered as staff." + }, true); + return; + } + + if (Database.SetStaffActive(staffUser.Id, !staffMember.active)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = staffMember.active ? "Staff member is now set as inactive and will no longer be randomly assigned any support tickets." : "Staff member is now set as active and will be randomly assigned support tickets again." + }, true); + } + else + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: Unable to update active status in database." + }, true); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/TranscriptActiveCommand.cs b/Akheemah/Commands/TranscriptActiveCommand.cs new file mode 100644 index 0000000..689e4ed --- /dev/null +++ b/Akheemah/Commands/TranscriptActiveCommand.cs @@ -0,0 +1,129 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.Exceptions; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class TranscriptCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("transcript", "Creates a transcript of a ticket.")] + public async Task OnExecute(InteractionContext command, [Option("Ticket", "(Optional) Ticket number to get transcript of.")] long ticketID = 0) + { + await command.DeferAsync(true); + Database.Ticket ticket; + if (ticketID == 0) // If there are no arguments use current channel + { + if (Database.TryGetOpenTicket(command.Channel.Id, out ticket)) + { + try + { + await Transcriber.ExecuteAsync(command.Channel.Id, ticket.id); + } + catch (Exception) + { + await command.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "ERROR: Could not save transcript file. Aborting..." + })); + throw; + } + } + else + { + await command.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "This channel is not a ticket." + })); + return; + } + } + else + { + // If the ticket is still open, generate a new fresh transcript + if (Database.TryGetOpenTicketByID((uint)ticketID, out ticket) && ticket?.creatorID == command.Member.Id) + { + try + { + await Transcriber.ExecuteAsync(command.Channel.Id, ticket.id); + } + catch (Exception) + { + await command.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "ERROR: Could not save transcript file. Aborting..." + })); + throw; + } + + } + // If there is no open or closed ticket, send an error. If there is a closed ticket we will simply use the old transcript from when the ticket was closed. + else if (!Database.TryGetClosedTicket((uint)ticketID, out ticket) || (ticket?.creatorID != command.Member.Id && !Database.IsStaff(command.Member.Id))) + { + await command.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Could not find a closed ticket with that number which you opened.\n(Use the /list command to see all your tickets)" + })); + return; + } + } + + // Log it if the log channel exists + DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel); + if (logChannel != null) + { + await using FileStream file = new FileStream(Transcriber.GetPath(ticket.id), FileMode.Open, FileAccess.Read); + + DiscordMessageBuilder message = new DiscordMessageBuilder(); + message.WithEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Ticket " + ticket.id.ToString("00000") + " transcript generated by " + command.Member.Mention + ".\n", + Footer = new DiscordEmbedBuilder.EmbedFooter { Text = '#' + command.Channel.Name } + }); + message.AddFiles(new Dictionary { { Transcriber.GetFilename(ticket.id), file } }); + + await logChannel.SendMessageAsync(message); + } + + try + { + // Send transcript in a direct message + await using FileStream file = new FileStream(Transcriber.GetPath(ticket.id), FileMode.Open, FileAccess.Read); + + DiscordMessageBuilder directMessage = new DiscordMessageBuilder(); + directMessage.WithEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Transcript generated!\n" + }); + directMessage.AddFiles(new Dictionary { { Transcriber.GetFilename(ticket.id), file } }); + + await command.Member.SendMessageAsync(directMessage); + } + catch (UnauthorizedException) + { + await command.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Not allowed to send direct message to you, please check your privacy settings.\n" + })); + return; + } + + await command.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Transcript sent!\n" + })); + } +} \ No newline at end of file diff --git a/Akheemah/Commands/UnassignCommand.cs b/Akheemah/Commands/UnassignCommand.cs new file mode 100644 index 0000000..2cc8deb --- /dev/null +++ b/Akheemah/Commands/UnassignCommand.cs @@ -0,0 +1,52 @@ +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class UnassignCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("unassign", "Unassigns a staff member from a ticket.")] + public async Task OnExecute(InteractionContext command) + { + // Check if ticket exists in the database + if (!Database.TryGetOpenTicket(command.Channel.Id, out Database.Ticket ticket)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "This channel is not a ticket." + }, true); + return; + } + + if (!Database.UnassignStaff(ticket)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error: Failed to unassign staff member from ticket." + }, true); + return; + } + + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Unassigned staff member from ticket." + }); + + // Log it if the log channel exists + DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel); + if (logChannel != null) + { + await logChannel.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Staff member was unassigned from " + command.Channel.Mention + " by " + command.Member.Mention + "." + }); + } + } +} \ No newline at end of file diff --git a/Akheemah/Commands/UnblacklistCommand.cs b/Akheemah/Commands/UnblacklistCommand.cs new file mode 100644 index 0000000..fb0256f --- /dev/null +++ b/Akheemah/Commands/UnblacklistCommand.cs @@ -0,0 +1,54 @@ +using System; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.SlashCommands; +using DSharpPlus.SlashCommands.Attributes; + +namespace Akheemah.Commands; + +public class UnblacklistCommand : ApplicationCommandModule +{ + [SlashRequireGuild] + [SlashCommand("unblacklist", "Unblacklists a user from opening tickets.")] + public async Task OnExecute(InteractionContext command, [Option("User", "User to remove from blacklist.")] DiscordUser user) + { + try + { + if (!Database.Unblacklist(user.Id)) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = user.Mention + " is not blacklisted." + }, true); + return; + } + + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = "Removed " + user.Mention + " from blacklist." + }, true); + + // Log it if the log channel exists + DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel); + if (logChannel != null) + { + await logChannel.SendMessageAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Green, + Description = user.Mention + " was unblacklisted from opening tickets by " + command.Member.Mention + "." + }); + } + } + catch (Exception) + { + await command.CreateResponseAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Error occured while removing " + user.Mention + " from blacklist." + }, true); + throw; + } + } +} \ No newline at end of file diff --git a/Akheemah/EventHandler.cs b/Akheemah/EventHandler.cs index 5b59004..3159fe7 100644 --- a/Akheemah/EventHandler.cs +++ b/Akheemah/EventHandler.cs @@ -211,13 +211,13 @@ internal static class EventHandler case ComponentType.Button: switch (e.Id) { - case "supportboi_closeconfirm": + case "akheemah_closeconfirm": await CloseCommand.OnConfirmed(e.Interaction); return; - case { } when e.Id.StartsWith("supportboi_newcommandbutton"): + case { } when e.Id.StartsWith("akheemah_newcommandbutton"): await NewCommand.OnCategorySelection(e.Interaction); return; - case { } when e.Id.StartsWith("supportboi_newticketbutton"): + case { } when e.Id.StartsWith("akheemah_newticketbutton"): await CreateButtonPanelCommand.OnButtonUsed(e.Interaction); return; case "right": @@ -237,10 +237,10 @@ internal static class EventHandler case ComponentType.StringSelect: switch (e.Id) { - case { } when e.Id.StartsWith("supportboi_newcommandselector"): + case { } when e.Id.StartsWith("akheemah_newcommandselector"): await NewCommand.OnCategorySelection(e.Interaction); return; - case { } when e.Id.StartsWith("supportboi_newticketselector"): + case { } when e.Id.StartsWith("akheemah_newticketselector"): await CreateSelectionBoxPanelCommand.OnSelectionMenuUsed(e.Interaction); return; default: