Refactored log channel handling

This commit is contained in:
Toastie 2024-12-27 20:17:14 +13:00
parent dac221246c
commit c6579beba2
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
27 changed files with 171 additions and 522 deletions

View file

@ -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.");
}
}

View file

@ -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)
{

View file

@ -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);
}
}

View file

@ -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 + ".");
}
}

View file

@ -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();

View file

@ -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);
}
}

View file

@ -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)
{

View file

@ -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<string, Stream> { { 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<string, Stream> { { fileName, file } });
await staffMember.SendMessageAsync(message);
}
catch (NotFoundException) { }
catch (UnauthorizedException) { }
catch (NotFoundException) { /* ignore */ }
catch (UnauthorizedException) { /* ignore */ }
}
Database.ArchiveTicket(ticket);

View file

@ -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 + ".");
}
}

View file

@ -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<List<DiscordSelectComponent>> GetSelectComponents(SlashCommandContext command, string placeholder)

View file

@ -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);
}
}

View file

@ -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));
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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<DiscordMember> GetRandomVerifiedStaffMember(DiscordChannel channel, ulong creatorID, ulong currentStaffID, DiscordRole targetRole)

View file

@ -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 + ".");
}
}

View file

@ -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 + ".");
}
}

View file

@ -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 + ".");
}
}

View file

@ -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<Database.Message> messages = Database.GetAllMessages();
if (messages.Count == 0)

View file

@ -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);
}
}

View file

@ -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"));
}
}
}

View file

@ -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<string, Stream> { { 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))

View file

@ -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);
}
}

View file

@ -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)
{

View file

@ -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<Database.Ticket> assignedTickets) && Config.logChannel != 0)
if (LogChannel.IsEnabled && Database.TryGetAssignedTickets(e.Member.Id, out List<Database.Ticket> 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;

75
LogChannel.cs Normal file
View file

@ -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);
}
}
}

View file

@ -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<T>(this IList<T> 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;