From c6579beba23693a01e2f9983497e57cfb0b13c38 Mon Sep 17 00:00:00 2001 From: Toastie Date: Fri, 27 Dec 2024 20:17:14 +1300 Subject: [PATCH] Refactored log channel handling --- Commands/AddCategoryCommand.cs | 18 +---- Commands/AddCommand.cs | 19 +----- Commands/AddMessageCommand.cs | 21 +----- Commands/AddStaffCommand.cs | 15 +---- Commands/AdminCommands.cs | 56 ++-------------- Commands/AssignCommand.cs | 21 +----- Commands/BlacklistCommand.cs | 15 +---- Commands/CloseCommand.cs | 27 ++------ Commands/CreateButtonPanelCommand.cs | 14 +--- Commands/CreateSelectionBoxPanelCommand.cs | 14 +--- Commands/InterviewCommands.cs | 36 +--------- Commands/InterviewTemplateCommands.cs | 30 ++------- Commands/MoveCommand.cs | 18 +---- Commands/NewCommand.cs | 20 +----- Commands/RandomAssignCommand.cs | 19 +----- Commands/RemoveCategoryCommand.cs | 15 +---- Commands/RemoveMessageCommand.cs | 17 +---- Commands/RemoveStaffCommand.cs | 35 +--------- Commands/SayCommand.cs | 22 ++---- Commands/SetSummaryCommand.cs | 18 +---- Commands/ToggleActiveCommand.cs | 25 ++----- Commands/TranscriptCommand.cs | 21 +----- Commands/UnassignCommand.cs | 19 +----- Commands/UnblacklistCommand.cs | 15 +---- EventHandler.cs | 78 +++++++++------------- LogChannel.cs | 75 +++++++++++++++++++++ Utilities.cs | 10 ++- 27 files changed, 171 insertions(+), 522 deletions(-) create mode 100644 LogChannel.cs diff --git a/Commands/AddCategoryCommand.cs b/Commands/AddCategoryCommand.cs index 9d92f8b..a5ad447 100644 --- a/Commands/AddCategoryCommand.cs +++ b/Commands/AddCategoryCommand.cs @@ -74,22 +74,6 @@ public class AddCategoryCommand }, true); } - try - { - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.User.Mention + " added `" + category.Name + "` to the category list.", - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Category: " + title - } - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(command.User.Mention + " added `" + category.Name + "` as `" + title + "` to the category list."); } } \ No newline at end of file diff --git a/Commands/AddCommand.cs b/Commands/AddCommand.cs index aaf5ecc..7b3b217 100644 --- a/Commands/AddCommand.cs +++ b/Commands/AddCommand.cs @@ -62,24 +62,7 @@ public class AddCommand Description = "Added " + member.Mention + " to ticket." }); - try - { - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = member.Mention + " was added to " + command.Channel.Mention + - " by " + command.User.Mention + ".", - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + ticket.id.ToString("00000") - } - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(member.Mention + " was added to " + command.Channel.Mention + " by " + command.User.Mention + ".", ticket.id); } catch (Exception) { diff --git a/Commands/AddMessageCommand.cs b/Commands/AddMessageCommand.cs index 3eb0bde..ff9057c 100644 --- a/Commands/AddMessageCommand.cs +++ b/Commands/AddMessageCommand.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using System.Globalization; using DSharpPlus.Entities; using System.Threading.Tasks; using DSharpPlus.Commands; @@ -27,7 +28,7 @@ public class AddMessageCommand return; } - if (Database.TryGetMessage(identifier.ToLower(), out Database.Message _)) + if (Database.TryGetMessage(identifier.ToLower(CultureInfo.InvariantCulture), out Database.Message _)) { await command.RespondAsync(new DiscordEmbedBuilder { @@ -54,22 +55,6 @@ public class AddMessageCommand }, true); } - try - { - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.User.Mention + " added or updated `" + identifier + "` in the /say command.\n\nContent:\n\n" + message, - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Identifier: " + identifier - } - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(command.User.Mention + " added or updated `" + identifier + "` in the /say command.\n\nContent:\n\n" + message); } } \ No newline at end of file diff --git a/Commands/AddStaffCommand.cs b/Commands/AddStaffCommand.cs index adf789a..016ea24 100644 --- a/Commands/AddStaffCommand.cs +++ b/Commands/AddStaffCommand.cs @@ -60,19 +60,6 @@ public class AddStaffCommand Description = alreadyStaff ? staffMember.Mention + " is already a staff member, refreshed username in database." : staffMember.Mention + " was added to staff." }, true); - try - { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = staffMember.Mention + " was added to staff by " + command.User.Mention + "." - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(staffMember.Mention + " was added to staff by " + command.User.Mention + "."); } } \ No newline at end of file diff --git a/Commands/AdminCommands.cs b/Commands/AdminCommands.cs index de35ca2..8ed2512 100644 --- a/Commands/AdminCommands.cs +++ b/Commands/AdminCommands.cs @@ -18,7 +18,7 @@ public class AdminCommands [Command("setticket")] [Description("Turns a channel into a ticket. WARNING: Anyone will be able to delete the channel using /close.")] public async Task SetTicket(SlashCommandContext command, - [Parameter("user")] [Description("(Optional) The owner of the ticket.")] DiscordUser user = null) + [Parameter("user")][Description("(Optional) The owner of the ticket.")] DiscordUser user = null) { // Check if ticket exists in the database if (Database.IsOpenTicket(command.Channel.Id)) @@ -40,31 +40,14 @@ public class AdminCommands Description = "Channel has been designated ticket " + id.ToString("00000") + "." }); - try - { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.Channel.Mention + " has been designated ticket " + id.ToString("00000") + " by " + command.Member?.Mention + ".", - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + id.ToString("00000") - } - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(command.Channel.Mention + " has been designated ticket " + id.ToString("00000") + " by " + command.Member?.Mention + ".", (uint)id); } [RequireGuild] [Command("unsetticket")] [Description("Deletes a ticket from the ticket system without deleting the channel.")] public async Task UnsetTicket(SlashCommandContext command, - [Parameter("ticket-id")] [Description("(Optional) Ticket to unset. Uses the channel you are in by default. Use ticket ID, not channel ID!")] long ticketID = 0) + [Parameter("ticket-id")][Description("(Optional) Ticket to unset. Uses the channel you are in by default. Use ticket ID, not channel ID!")] long ticketID = 0) { Database.Ticket ticket; DiscordChannel channel = null; @@ -121,24 +104,7 @@ public class AdminCommands Description = "Channel has been undesignated as a ticket." }); - try - { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.Channel.Mention + " has been undesignated as a ticket by " + command.User.Mention + ".", - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + ticket.id.ToString("00000") - } - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(command.Channel.Mention + " has been undesignated as a ticket by " + command.User.Mention + ".", ticket.id); } else { @@ -161,19 +127,7 @@ public class AdminCommands Description = "Reloading bot application..." }, true); - try - { - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.Channel.Mention + " reloaded the bot.", - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(command.Channel.Mention + " reloaded the bot."); Logger.Log("Reloading bot..."); await SupportChild.Reload(); diff --git a/Commands/AssignCommand.cs b/Commands/AssignCommand.cs index 6a18220..de6e6a7 100644 --- a/Commands/AssignCommand.cs +++ b/Commands/AssignCommand.cs @@ -89,26 +89,9 @@ public class AssignCommand Description = "You have been assigned to a support ticket: " + command.Channel.Mention }); } - catch (UnauthorizedException) { } + catch (UnauthorizedException) { /* ignore */ } } - try - { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = member.Mention + " was assigned to " + command.Channel.Mention + " by " + command.User.Mention + ".", - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + ticket.id.ToString("00000") - } - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(member.Mention + " was assigned to " + command.Channel.Mention + " by " + command.User.Mention + ".", ticket.id); } } \ No newline at end of file diff --git a/Commands/BlacklistCommand.cs b/Commands/BlacklistCommand.cs index 93c2646..27cf520 100644 --- a/Commands/BlacklistCommand.cs +++ b/Commands/BlacklistCommand.cs @@ -35,20 +35,7 @@ public class BlacklistCommand Description = "Blocked " + user.Mention + " from opening new tickets." }, true); - try - { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = user.Mention + " was blocked from opening tickets by " + command.User.Mention + "." - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(user.Mention + " was blocked from opening tickets by " + command.User.Mention + "."); } catch (Exception) { diff --git a/Commands/CloseCommand.cs b/Commands/CloseCommand.cs index 48bbf2d..957d537 100644 --- a/Commands/CloseCommand.cs +++ b/Commands/CloseCommand.cs @@ -141,28 +141,12 @@ public class CloseCommand try { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await using FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read); - DiscordMessageBuilder message = new DiscordMessageBuilder(); - message.AddEmbed(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = "Ticket " + ticket.id.ToString("00000") + " closed by " + - interaction.User.Mention + ".\n" + closeReason, - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + ticket.id.ToString("00000") - } - }); - message.AddFiles(new Dictionary { { fileName, file } }); - - await logChannel.SendMessageAsync(message); + await LogChannel.Success("Ticket " + ticket.id.ToString("00000") + " closed by " + interaction.User.Mention + ".\n" + closeReason, ticket.id, new Utilities.File(fileName, file)); } - catch (NotFoundException) + catch (Exception e) { - Logger.Error("Could not send message in log channel."); + Logger.Error("Error occurred sending transcript log message. ", e); } if (Config.closingNotifications) @@ -200,13 +184,12 @@ public class CloseCommand }); } - message.AddFiles(new Dictionary { { fileName, file } }); await staffMember.SendMessageAsync(message); } - catch (NotFoundException) { } - catch (UnauthorizedException) { } + catch (NotFoundException) { /* ignore */ } + catch (UnauthorizedException) { /* ignore */ } } Database.ArchiveTicket(ticket); diff --git a/Commands/CreateButtonPanelCommand.cs b/Commands/CreateButtonPanelCommand.cs index a24b78f..bc00a33 100644 --- a/Commands/CreateButtonPanelCommand.cs +++ b/Commands/CreateButtonPanelCommand.cs @@ -51,18 +51,6 @@ public class CreateButtonPanelCommand Description = "Successfully created message, make sure to run this command again if you add new categories to the bot." }, true); - try - { - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.User.Mention + " created a new button panel in " + command.Channel.Mention + "." - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(command.User.Mention + " created a new button panel in " + command.Channel.Mention + "."); } } \ No newline at end of file diff --git a/Commands/CreateSelectionBoxPanelCommand.cs b/Commands/CreateSelectionBoxPanelCommand.cs index 9ab2c0d..d7103a7 100644 --- a/Commands/CreateSelectionBoxPanelCommand.cs +++ b/Commands/CreateSelectionBoxPanelCommand.cs @@ -29,19 +29,7 @@ public class CreateSelectionBoxPanelCommand Description = "Successfully created message, make sure to run this command again if you add new categories to the bot." }, true); - try - { - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.User.Mention + " created a new selector panel in " + command.Channel.Mention + "." - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(command.User.Mention + " created a new selector panel in " + command.Channel.Mention + "."); } public static async Task> GetSelectComponents(SlashCommandContext command, string placeholder) diff --git a/Commands/InterviewCommands.cs b/Commands/InterviewCommands.cs index c4d0688..1629f77 100644 --- a/Commands/InterviewCommands.cs +++ b/Commands/InterviewCommands.cs @@ -45,23 +45,7 @@ public class InterviewCommands }, true); } - try - { - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.User.Mention + " restarted interview in " + command.Channel.Mention + ".", - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + ticket.id.ToString("00000") - } - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(command.User.Mention + " restarted interview in " + command.Channel.Mention + ".", ticket.id); } [Command("stop")] @@ -107,22 +91,6 @@ public class InterviewCommands }, true); } - try - { - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.User.Mention + " stopped the interview in " + command.Channel.Mention + ".", - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + ticket.id.ToString("00000") - } - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(command.User.Mention + " stopped the interview in " + command.Channel.Mention + ".", ticket.id); } } \ No newline at end of file diff --git a/Commands/InterviewTemplateCommands.cs b/Commands/InterviewTemplateCommands.cs index 3ad55c2..c3af250 100644 --- a/Commands/InterviewTemplateCommands.cs +++ b/Commands/InterviewTemplateCommands.cs @@ -168,18 +168,12 @@ public class InterviewTemplateCommands try { MemoryStream memStream = new(Encoding.UTF8.GetBytes(Database.GetInterviewTemplateJSON(template.categoryID))); - - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordMessageBuilder().AddEmbed(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.User.Mention + " uploaded a new interview template for the `" + category.Name + "` category." - }).AddFile("interview-template-" + template.categoryID + ".json", memStream)); + await LogChannel.Success(command.User.Mention + " uploaded a new interview template for the `" + category.Name + "` category.", 0, + new Utilities.File("interview-template-" + template.categoryID + ".json", memStream)); } - catch (NotFoundException) + catch (Exception e) { - Logger.Error("Could not send message in log channel."); + Logger.Error("Unable to log interview template upload.", e); } } catch (Exception e) @@ -241,19 +235,7 @@ public class InterviewTemplateCommands Description = "Deleted interview template." }, true); - try - { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordMessageBuilder().AddEmbed(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.User.Mention + " deleted the interview template for the `" + category.Name + "` category." - }).AddFile("interview-template-" + category.Id + ".json", memStream)); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(command.User.Mention + " deleted the interview template for the `" + category.Name + "` category.", 0, + new Utilities.File("interview-template-" + category.Id + ".json", memStream)); } } \ No newline at end of file diff --git a/Commands/MoveCommand.cs b/Commands/MoveCommand.cs index b8880de..4ef23a4 100644 --- a/Commands/MoveCommand.cs +++ b/Commands/MoveCommand.cs @@ -105,22 +105,6 @@ public class MoveCommand Description = "Ticket was moved to `" + categoryChannel.Name + "`." }); - try - { - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.User.Mention + " moved " + command.Channel.Mention + " to `" + categoryChannel.Name + "`.", - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + ticket.id.ToString("00000") - } - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(command.User.Mention + " moved " + command.Channel.Mention + " to `" + categoryChannel.Name + "`.", ticket.id); } } \ No newline at end of file diff --git a/Commands/NewCommand.cs b/Commands/NewCommand.cs index 4881b86..010eb63 100644 --- a/Commands/NewCommand.cs +++ b/Commands/NewCommand.cs @@ -236,25 +236,7 @@ public class NewCommand } } - try - { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = "Ticket " + ticketChannel.Mention + " opened by " + member.Mention + ".", - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + id.ToString("00000") - } - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } - + await LogChannel.Success("Ticket " + ticketChannel.Mention + " opened by " + member.Mention + ".", (uint)id); return (true, "Ticket opened, " + member.Mention + "!\n" + ticketChannel.Mention); } } \ No newline at end of file diff --git a/Commands/RandomAssignCommand.cs b/Commands/RandomAssignCommand.cs index fda7c34..5cbfe35 100644 --- a/Commands/RandomAssignCommand.cs +++ b/Commands/RandomAssignCommand.cs @@ -74,24 +74,7 @@ public class RandomAssignCommand catch (UnauthorizedException) { /* ignore */ } } - try - { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = staffMember.Mention + " was randomly assigned to " + command.Channel.Mention + " by " + command.User.Mention + ".", - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + ticket.id.ToString("00000") - } - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(staffMember.Mention + " was randomly assigned to " + command.Channel.Mention + " by " + command.User.Mention + ".", ticket.id); } internal static async Task GetRandomVerifiedStaffMember(DiscordChannel channel, ulong creatorID, ulong currentStaffID, DiscordRole targetRole) diff --git a/Commands/RemoveCategoryCommand.cs b/Commands/RemoveCategoryCommand.cs index 9ce98bb..1a20b19 100644 --- a/Commands/RemoveCategoryCommand.cs +++ b/Commands/RemoveCategoryCommand.cs @@ -43,19 +43,6 @@ public class RemoveCategoryCommand }, true); } - try - { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = "`" + category.Name + "` was removed by " + command.User.Mention + "." - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success("`" + category.Name + "` was removed by " + command.User.Mention + "."); } } \ No newline at end of file diff --git a/Commands/RemoveMessageCommand.cs b/Commands/RemoveMessageCommand.cs index de602b9..1506c6b 100644 --- a/Commands/RemoveMessageCommand.cs +++ b/Commands/RemoveMessageCommand.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using System.Globalization; using DSharpPlus.Entities; using System.Threading.Tasks; using DSharpPlus.Commands; @@ -16,7 +17,7 @@ public class RemoveMessageCommand public async Task OnExecute(SlashCommandContext command, [Parameter("identifier")][Description("The identifier word used in the /say command.")] string identifier) { - if (!Database.TryGetMessage(identifier.ToLower(), out Database.Message _)) + if (!Database.TryGetMessage(identifier.ToLower(CultureInfo.InvariantCulture), out Database.Message _)) { await command.RespondAsync(new DiscordEmbedBuilder { @@ -43,18 +44,6 @@ public class RemoveMessageCommand }, true); } - try - { - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = "`" + identifier + "` was removed from the /say command by " + command.User.Mention + "." - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success("`" + identifier + "` was removed from the /say command by " + command.User.Mention + "."); } } \ No newline at end of file diff --git a/Commands/RemoveStaffCommand.cs b/Commands/RemoveStaffCommand.cs index 4863dd4..96b504b 100644 --- a/Commands/RemoveStaffCommand.cs +++ b/Commands/RemoveStaffCommand.cs @@ -6,7 +6,6 @@ using DSharpPlus.Commands; using DSharpPlus.Commands.ContextChecks; using DSharpPlus.Commands.Processors.SlashCommands; using DSharpPlus.Entities; -using DSharpPlus.Exceptions; using MySqlConnector; namespace SupportChild.Commands; @@ -50,24 +49,7 @@ public class RemoveStaffCommand Logger.Error("Error when trying to send message about unassigning staff member from ticket-" + assignedTicket.id.ToString("00000"), e); } - try - { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = "<@" + assignedTicket.assignedStaffID + "> was unassigned from <#" + assignedTicket.channelID + "> by " + command.User.Mention + ".", - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + assignedTicket.id.ToString("00000") - } - }); - } - catch (Exception e) - { - Logger.Error("Could not send message in log channel.", e); - } + await LogChannel.Success("<@" + assignedTicket.assignedStaffID + "> was unassigned from <#" + assignedTicket.channelID + "> by " + command.User.Mention + ".", assignedTicket.id); } } @@ -84,19 +66,6 @@ public class RemoveStaffCommand Description = "User was removed from staff." }, true); - try - { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = user.Mention + " was removed from staff by " + command.User.Mention + "." - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(user.Mention + " was removed from staff by " + command.User.Mention + "."); } } \ No newline at end of file diff --git a/Commands/SayCommand.cs b/Commands/SayCommand.cs index 2e0d823..a6188e7 100644 --- a/Commands/SayCommand.cs +++ b/Commands/SayCommand.cs @@ -1,6 +1,7 @@ using DSharpPlus.Entities; using System.Collections.Generic; using System.ComponentModel; +using System.Globalization; using System.Threading.Tasks; using DSharpPlus.Commands; using DSharpPlus.Commands.ContextChecks; @@ -22,11 +23,12 @@ public class SayCommand // Print list of all messages if no identifier is provided if (identifier == null) { - SendMessageList(command); + await SendMessageList(command); return; } - if (!Database.TryGetMessage(identifier.ToLower(), out Database.Message message)) + if (!Database.TryGetMessage(identifier.ToLower(CultureInfo.InvariantCulture), + out Database.Message message)) { await command.RespondAsync(new DiscordEmbedBuilder { @@ -42,22 +44,10 @@ public class SayCommand Description = message.message.Replace("\\n", "\n") }); - try - { - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.User.Mention + " posted the " + message.identifier + " message in " + command.Channel.Mention + "." - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(command.User.Mention + " posted the " + message.identifier + " message in " + command.Channel.Mention + "."); } - private static async void SendMessageList(SlashCommandContext command) + private static async Task SendMessageList(SlashCommandContext command) { List messages = Database.GetAllMessages(); if (messages.Count == 0) diff --git a/Commands/SetSummaryCommand.cs b/Commands/SetSummaryCommand.cs index 953b770..31b4c9c 100644 --- a/Commands/SetSummaryCommand.cs +++ b/Commands/SetSummaryCommand.cs @@ -43,22 +43,6 @@ public class SetSummaryCommand Description = "Summary set." }, true); - try - { - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = command.User.Mention + " set the summary for " + command.Channel.Mention + " to:\n\n" + summary, - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + ticket.id.ToString("00000") - } - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(command.User.Mention + " set the summary for " + command.Channel.Mention + " to:\n\n" + summary, ticket.id); } } \ No newline at end of file diff --git a/Commands/ToggleActiveCommand.cs b/Commands/ToggleActiveCommand.cs index adec888..0519196 100644 --- a/Commands/ToggleActiveCommand.cs +++ b/Commands/ToggleActiveCommand.cs @@ -58,30 +58,13 @@ public class ToggleActiveCommand }, true); } - try + if (user != null && user.Id != command.User.Id) { - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - - if (user != null && user.Id != command.User.Id) - { - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = staffUser.Mention + " set " + command.Channel.Mention + "'s status to " + (staffMember.active ? "active" : "inactive") - }); - } - else - { - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = staffUser.Mention + " set their own status to " + (staffMember.active ? "active" : "inactive") - }); - } + await LogChannel.Success(staffUser.Mention + " set " + command.Channel.Mention + "'s status to " + (staffMember.active ? "active" : "inactive")); } - catch (NotFoundException) + else { - Logger.Error("Could not send message in log channel."); + await LogChannel.Success(staffUser.Mention + " set their own status to " + (staffMember.active ? "active" : "inactive")); } } } \ No newline at end of file diff --git a/Commands/TranscriptCommand.cs b/Commands/TranscriptCommand.cs index adcd011..2163595 100644 --- a/Commands/TranscriptCommand.cs +++ b/Commands/TranscriptCommand.cs @@ -121,27 +121,12 @@ public class TranscriptCommand try { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); await using FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read); - - DiscordMessageBuilder message = new DiscordMessageBuilder(); - message.AddEmbed(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = "Transcript generated by " + command.User.Mention + ".", - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + ticket.id.ToString("00000") - } - }); - message.AddFiles(new Dictionary { { fileName, file } }); - - await logChannel.SendMessageAsync(message); + await LogChannel.Success("Transcript generated by " + command.User.Mention + ".", ticket.id, new Utilities.File(fileName, file)); } - catch (NotFoundException) + catch (Exception e) { - Logger.Error("Could not send message in log channel."); + Logger.Error("Failed to log transcript generation.", e); } if (await SendDirectMessage(command, fileName, filePath, zipSize, ticket.id)) diff --git a/Commands/UnassignCommand.cs b/Commands/UnassignCommand.cs index b1544ec..4e08d57 100644 --- a/Commands/UnassignCommand.cs +++ b/Commands/UnassignCommand.cs @@ -42,23 +42,6 @@ public class UnassignCommand Description = "Unassigned <@" + ticket.assignedStaffID + "> from ticket." }); - try - { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = "<@" + ticket.assignedStaffID + "> was unassigned from <#" + ticket.channelID + "> by " + command.User.Mention + ".", - Footer = new DiscordEmbedBuilder.EmbedFooter - { - Text = "Ticket: " + ticket.id.ToString("00000") - } - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success("<@" + ticket.assignedStaffID + "> was unassigned from <#" + ticket.channelID + "> by " + command.User.Mention + ".", ticket.id); } } \ No newline at end of file diff --git a/Commands/UnblacklistCommand.cs b/Commands/UnblacklistCommand.cs index c3fb0ce..5da5769 100644 --- a/Commands/UnblacklistCommand.cs +++ b/Commands/UnblacklistCommand.cs @@ -34,20 +34,7 @@ public class UnblacklistCommand Description = "Unblocked " + user.Mention + " from opening new tickets." }, true); - try - { - // Log it if the log channel exists - DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = user.Mention + " was unblocked from opening tickets by " + command.User.Mention + "." - }); - } - catch (NotFoundException) - { - Logger.Error("Could not send message in log channel."); - } + await LogChannel.Success(user.Mention + " was unblocked from opening tickets by " + command.User.Mention + "."); } catch (Exception) { diff --git a/EventHandler.cs b/EventHandler.cs index e3e3fd6..69abca6 100644 --- a/EventHandler.cs +++ b/EventHandler.cs @@ -112,8 +112,8 @@ public static class EventHandler Description = "A ticket you are assigned to has been updated: " + e.Channel.Mention }); } - catch (NotFoundException) { } - catch (UnauthorizedException) { } + catch (NotFoundException) { /* ignored */ } + catch (UnauthorizedException) { /* ignored */ } } } @@ -174,27 +174,19 @@ public static class EventHandler } } - if (Database.TryGetAssignedTickets(e.Member.Id, out List assignedTickets) && Config.logChannel != 0) + if (LogChannel.IsEnabled && Database.TryGetAssignedTickets(e.Member.Id, out List assignedTickets)) { - DiscordChannel logChannel = await client.GetChannelAsync(Config.logChannel); - if (logChannel != null) + foreach (Database.Ticket ticket in assignedTickets) { - foreach (Database.Ticket ticket in assignedTickets) + try { - try + DiscordChannel channel = await client.GetChannelAsync(ticket.channelID); + if (channel?.GuildId == e.Guild.Id) { - DiscordChannel channel = await client.GetChannelAsync(ticket.channelID); - if (channel?.GuildId == e.Guild.Id) - { - await logChannel.SendMessageAsync(new DiscordEmbedBuilder - { - Color = DiscordColor.Red, - Description = "Assigned staff member '" + e.Member.Username + "#" + e.Member.Discriminator + "' has left the server: <#" + channel.Id + ">" - }); - } + await LogChannel.Warn("Assigned staff member '" + e.Member.Username + "#" + e.Member.Discriminator + "' has left the server: <#" + channel.Id + ">"); } - catch (Exception) { /* ignored */ } } + catch (Exception) { /* ignored */ } } } } @@ -253,45 +245,41 @@ public static class EventHandler Logger.Warn("Unknown form input received! '" + e.Id + "'"); return; case DiscordComponentType.UserSelect: - switch (e.Id) + if (e.Id.StartsWith("supportchild_interviewuserselector")) { - case not null when e.Id.StartsWith("supportchild_interviewuserselector"): - await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction); - return; - default: - Logger.Warn("Unknown selection box option received! '" + e.Id + "'"); - return; + await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction); + return; } + + Logger.Warn("Unknown selection box option received! '" + e.Id + "'"); + return; case DiscordComponentType.RoleSelect: - switch (e.Id) + if (e.Id.StartsWith("supportchild_interviewroleselector")) { - case not null when e.Id.StartsWith("supportchild_interviewroleselector"): - await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction); - return; - default: - Logger.Warn("Unknown selection box option received! '" + e.Id + "'"); - return; + await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction); + return; } + + Logger.Warn("Unknown selection box option received! '" + e.Id + "'"); + return; case DiscordComponentType.MentionableSelect: - switch (e.Id) + if (e.Id.StartsWith("supportchild_interviewmentionableselector")) { - case not null when e.Id.StartsWith("supportchild_interviewmentionableselector"): - await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction); - return; - default: - Logger.Warn("Unknown selection box option received! '" + e.Id + "'"); - return; + await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction); + return; } + + Logger.Warn("Unknown selection box option received! '" + e.Id + "'"); + return; case DiscordComponentType.ChannelSelect: - switch (e.Id) + if (e.Id.StartsWith("supportchild_interviewchannelselector")) { - case not null when e.Id.StartsWith("supportchild_interviewchannelselector"): - await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction); - return; - default: - Logger.Warn("Unknown selection box option received! '" + e.Id + "'"); - return; + await Interviewer.ProcessButtonOrSelectorResponse(e.Interaction); + return; } + + Logger.Warn("Unknown selection box option received! '" + e.Id + "'"); + return; default: Logger.Warn("Unknown interaction type received! '" + e.Interaction.Data.ComponentType + "'"); break; diff --git a/LogChannel.cs b/LogChannel.cs new file mode 100644 index 0000000..391ad2e --- /dev/null +++ b/LogChannel.cs @@ -0,0 +1,75 @@ +using System; +using System.Threading.Tasks; +using DSharpPlus.Entities; +using DSharpPlus.Exceptions; + +namespace SupportChild; + +public static class LogChannel +{ + public static bool IsEnabled => Config.logChannel != 0; + + public static async Task Log(string message, uint ticketID = 0, Utilities.File file = null) + { + await Log(DiscordColor.Cyan, message, ticketID, file); + } + + public static async Task Success(string message, uint ticketID = 0, Utilities.File file = null) + { + await Log(DiscordColor.Green, message, ticketID, file); + } + + public static async Task Warn(string message, uint ticketID = 0, Utilities.File file = null) + { + await Log(DiscordColor.Orange, message, ticketID, file); + } + + public static async Task Error(string message, uint ticketID = 0, Utilities.File file = null) + { + await Log(DiscordColor.Red, message, ticketID, file); + } + + private static async Task Log(DiscordColor color, string message, uint ticketID = 0, Utilities.File file = null) + { + if (!IsEnabled) + { + return; + } + + try + { + DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); + + DiscordMessageBuilder messageBuilder = new(); + DiscordEmbedBuilder embedBuilder = new() + { + Color = color, + Description = message + }; + + if (ticketID != 0) + { + embedBuilder.WithFooter("Ticket: " + ticketID.ToString("00000")); + } + + if (file != null) + { + messageBuilder.AddFile(file.fileName, file.contents); + } + + await logChannel.SendMessageAsync(messageBuilder); + } + catch (NotFoundException) + { + Logger.Error("Log channel does not exist. Channel ID: " + Config.logChannel); + } + catch (UnauthorizedException) + { + Logger.Error("No permissions to send message in log channel. Channel ID: " + Config.logChannel); + } + catch (Exception e) + { + Logger.Error("Error occured trying to send message in log channel. Channel ID: " + Config.logChannel, e); + } + } +} \ No newline at end of file diff --git a/Utilities.cs b/Utilities.cs index e528514..792d8a9 100644 --- a/Utilities.cs +++ b/Utilities.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Linq; using System.Reflection; @@ -14,6 +15,7 @@ public static class Extensions { return needles.Any(haystack.Contains); } + public static bool ContainsAny(this string haystack, params char[] needles) { return needles.Any(haystack.Contains); @@ -24,6 +26,12 @@ public static class Utilities { private static readonly Random rng = new Random(); + public class File(string fileName, Stream contents) + { + public string fileName = fileName; + public Stream contents = contents; + } + public static void Shuffle(this IList list) { int n = list.Count; @@ -95,7 +103,7 @@ public static class Utilities public static DiscordColor StringToColor(string color) { - switch (color.ToLower()) + switch (color.ToLower(CultureInfo.InvariantCulture)) { case "black": return DiscordColor.Black;