Add some janky handling of users clicking the close button several times
This commit is contained in:
parent
84ad8cbca4
commit
7d70cde3b4
1 changed files with 169 additions and 139 deletions
|
@ -15,6 +15,7 @@ public class CloseCommand
|
||||||
{
|
{
|
||||||
// TODO: Refactor this class a whole lot
|
// TODO: Refactor this class a whole lot
|
||||||
private static Dictionary<ulong, string> closeReasons = new Dictionary<ulong, string>();
|
private static Dictionary<ulong, string> closeReasons = new Dictionary<ulong, string>();
|
||||||
|
private static List<ulong> currentlyClosingTickets = new List<ulong>();
|
||||||
|
|
||||||
[RequireGuild]
|
[RequireGuild]
|
||||||
[Command("close")]
|
[Command("close")]
|
||||||
|
@ -47,166 +48,195 @@ public class CloseCommand
|
||||||
|
|
||||||
public static async Task OnConfirmed(DiscordInteraction interaction)
|
public static async Task OnConfirmed(DiscordInteraction interaction)
|
||||||
{
|
{
|
||||||
await interaction.CreateResponseAsync(DiscordInteractionResponseType.DeferredMessageUpdate);
|
if (currentlyClosingTickets.Contains(interaction.Channel.Id))
|
||||||
|
|
||||||
// Check if ticket exists in the database
|
|
||||||
if (!Database.TryGetOpenTicket(interaction.Channel.Id, out Database.Ticket ticket))
|
|
||||||
{
|
{
|
||||||
await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder
|
await interaction.CreateResponseAsync(DiscordInteractionResponseType.ChannelMessageWithSource,
|
||||||
{
|
new DiscordInteractionResponseBuilder().AddEmbed(new DiscordEmbedBuilder
|
||||||
Color = DiscordColor.Red,
|
{
|
||||||
Description = "This channel is not a ticket."
|
Color = DiscordColor.Red,
|
||||||
}));
|
Description = "This ticket is already closing."
|
||||||
return;
|
}).AsEphemeral());
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
string closeReason = "";
|
|
||||||
if (closeReasons.TryGetValue(interaction.Channel.Id, out string cachedReason))
|
|
||||||
{
|
|
||||||
closeReason = "\nReason: " + cachedReason + "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
string fileName = Transcriber.GetZipFilename(ticket.id);
|
|
||||||
string filePath = Transcriber.GetZipPath(ticket.id);
|
|
||||||
long zipSize = 0;
|
|
||||||
|
|
||||||
// If the zip transcript doesn't exist, use the html file.
|
|
||||||
try
|
|
||||||
{
|
|
||||||
FileInfo fi = new FileInfo(filePath);
|
|
||||||
if (!fi.Exists || fi.Length >= 26214400)
|
|
||||||
{
|
|
||||||
fileName = Transcriber.GetHTMLFilename(ticket.id);
|
|
||||||
filePath = Transcriber.GetHtmlPath(ticket.id);
|
|
||||||
}
|
|
||||||
zipSize = fi.Length;
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder
|
|
||||||
{
|
|
||||||
Color = DiscordColor.Red,
|
|
||||||
Description = "ERROR: Could not find transcript file. Aborting..."
|
|
||||||
}));
|
|
||||||
Logger.Error("Failed to access transcript file:", e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the chosen file path works.
|
|
||||||
if (!File.Exists(filePath))
|
|
||||||
{
|
|
||||||
await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder
|
|
||||||
{
|
|
||||||
Color = DiscordColor.Red,
|
|
||||||
Description = "ERROR: Could not find transcript file. Aborting..."
|
|
||||||
}));
|
|
||||||
Logger.Error("Transcript file does not exist: \"" + filePath + "\"");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Log it if the log channel exists
|
currentlyClosingTickets.Add(interaction.Channel.Id);
|
||||||
DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel);
|
await interaction.CreateResponseAsync(DiscordInteractionResponseType.DeferredMessageUpdate);
|
||||||
|
|
||||||
await using FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
// Check if ticket exists in the database
|
||||||
DiscordMessageBuilder message = new DiscordMessageBuilder();
|
if (!Database.TryGetOpenTicket(interaction.Channel.Id, out Database.Ticket ticket))
|
||||||
message.AddEmbed(new DiscordEmbedBuilder
|
|
||||||
{
|
{
|
||||||
Color = DiscordColor.Green,
|
currentlyClosingTickets.Remove(interaction.Channel.Id);
|
||||||
Description = "Ticket " + ticket.id.ToString("00000") + " closed by " +
|
await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder
|
||||||
interaction.User.Mention + ".\n" + closeReason,
|
|
||||||
Footer = new DiscordEmbedBuilder.EmbedFooter
|
|
||||||
{
|
{
|
||||||
Text = "Ticket: " + ticket.id.ToString("00000")
|
Color = DiscordColor.Red,
|
||||||
}
|
Description = "This channel is not a ticket."
|
||||||
});
|
}));
|
||||||
message.AddFiles(new Dictionary<string, Stream> { { fileName, file } });
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
await logChannel.SendMessageAsync(message);
|
// Build transcript
|
||||||
}
|
|
||||||
catch (NotFoundException)
|
|
||||||
{
|
|
||||||
Logger.Error("Could not send message in log channel.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Config.closingNotifications)
|
|
||||||
{
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
DiscordUser staffMember = await SupportChild.client.GetUserAsync(ticket.creatorID);
|
await Transcriber.ExecuteAsync(interaction.Channel.Id, ticket.id);
|
||||||
await using FileStream file = new(filePath, FileMode.Open, FileAccess.Read);
|
}
|
||||||
|
catch (Exception e)
|
||||||
DiscordMessageBuilder message = new();
|
{
|
||||||
|
currentlyClosingTickets.Remove(interaction.Channel.Id);
|
||||||
if (zipSize >= 26214400)
|
Logger.Error("Exception occured when trying to save transcript while closing ticket: " + e);
|
||||||
|
await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder
|
||||||
{
|
{
|
||||||
message.AddEmbed(new DiscordEmbedBuilder
|
Color = DiscordColor.Red,
|
||||||
{
|
Description = "ERROR: Could not save transcript file. Aborting..."
|
||||||
Color = DiscordColor.Orange,
|
}));
|
||||||
Description = "Ticket " + ticket.id.ToString("00000") + " which you opened has now been closed, check the transcript for more info.\n\n" +
|
return;
|
||||||
"The zip file is too large, sending only the HTML file. Ask an administrator for the zip if you need it.\"\n" + closeReason,
|
}
|
||||||
Footer = new DiscordEmbedBuilder.EmbedFooter
|
|
||||||
{
|
string closeReason = "";
|
||||||
Text = "Ticket: " + ticket.id.ToString("00000")
|
if (closeReasons.TryGetValue(interaction.Channel.Id, out string cachedReason))
|
||||||
}
|
{
|
||||||
});
|
closeReason = "\nReason: " + cachedReason + "\n";
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
string fileName = Transcriber.GetZipFilename(ticket.id);
|
||||||
|
string filePath = Transcriber.GetZipPath(ticket.id);
|
||||||
|
long zipSize = 0;
|
||||||
|
|
||||||
|
// If the zip transcript doesn't exist, use the html file.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
FileInfo fi = new FileInfo(filePath);
|
||||||
|
if (!fi.Exists || fi.Length >= 26214400)
|
||||||
{
|
{
|
||||||
message.AddEmbed(new DiscordEmbedBuilder
|
fileName = Transcriber.GetHTMLFilename(ticket.id);
|
||||||
{
|
filePath = Transcriber.GetHtmlPath(ticket.id);
|
||||||
Color = DiscordColor.Green,
|
|
||||||
Description = "Ticket " + ticket.id.ToString("00000") + " which you opened has now been closed, " + "check the transcript for more info.\n" + closeReason,
|
|
||||||
Footer = new DiscordEmbedBuilder.EmbedFooter
|
|
||||||
{
|
|
||||||
Text = "Ticket: " + ticket.id.ToString("00000")
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
zipSize = fi.Length;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
currentlyClosingTickets.Remove(interaction.Channel.Id);
|
||||||
|
await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder
|
||||||
|
{
|
||||||
|
Color = DiscordColor.Red,
|
||||||
|
Description = "ERROR: Could not find transcript file. Aborting..."
|
||||||
|
}));
|
||||||
|
Logger.Error("Failed to access transcript file:", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the chosen file path works.
|
||||||
|
if (!File.Exists(filePath))
|
||||||
|
{
|
||||||
|
currentlyClosingTickets.Remove(interaction.Channel.Id);
|
||||||
|
await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder
|
||||||
|
{
|
||||||
|
Color = DiscordColor.Red,
|
||||||
|
Description = "ERROR: Could not find transcript file. Aborting..."
|
||||||
|
}));
|
||||||
|
Logger.Error("Transcript file does not exist: \"" + filePath + "\"");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 } });
|
message.AddFiles(new Dictionary<string, Stream> { { fileName, file } });
|
||||||
|
|
||||||
await staffMember.SendMessageAsync(message);
|
await logChannel.SendMessageAsync(message);
|
||||||
}
|
}
|
||||||
catch (NotFoundException) { }
|
catch (NotFoundException)
|
||||||
catch (UnauthorizedException) { }
|
{
|
||||||
|
Logger.Error("Could not send message in log channel.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Config.closingNotifications)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
DiscordUser staffMember = await SupportChild.client.GetUserAsync(ticket.creatorID);
|
||||||
|
await using FileStream file = new(filePath, FileMode.Open, FileAccess.Read);
|
||||||
|
|
||||||
|
DiscordMessageBuilder message = new();
|
||||||
|
|
||||||
|
if (zipSize >= 26214400)
|
||||||
|
{
|
||||||
|
message.AddEmbed(new DiscordEmbedBuilder
|
||||||
|
{
|
||||||
|
Color = DiscordColor.Orange,
|
||||||
|
Description = "Ticket " + ticket.id.ToString("00000") + " which you opened has now been closed, check the transcript for more info.\n\n" +
|
||||||
|
"The zip file is too large, sending only the HTML file. Ask an administrator for the zip if you need it.\"\n" + closeReason,
|
||||||
|
Footer = new DiscordEmbedBuilder.EmbedFooter
|
||||||
|
{
|
||||||
|
Text = "Ticket: " + ticket.id.ToString("00000")
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
message.AddEmbed(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" + closeReason,
|
||||||
|
Footer = new DiscordEmbedBuilder.EmbedFooter
|
||||||
|
{
|
||||||
|
Text = "Ticket: " + ticket.id.ToString("00000")
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
message.AddFiles(new Dictionary<string, Stream> { { fileName, file } });
|
||||||
|
|
||||||
|
await staffMember.SendMessageAsync(message);
|
||||||
|
}
|
||||||
|
catch (NotFoundException) { }
|
||||||
|
catch (UnauthorizedException) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
Database.ArchiveTicket(ticket);
|
||||||
|
Database.TryDeleteInterview(interaction.Channel.Id);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
closeReasons.Remove(interaction.Channel.Id);
|
||||||
|
currentlyClosingTickets.Remove(interaction.Channel.Id);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
Database.ArchiveTicket(ticket);
|
|
||||||
Database.TryDeleteInterview(interaction.Channel.Id);
|
|
||||||
|
|
||||||
await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder
|
|
||||||
{
|
{
|
||||||
Color = DiscordColor.Green,
|
currentlyClosingTickets.Remove(interaction.Channel.Id);
|
||||||
Description = "Channel will be deleted in 3 seconds..."
|
await interaction.EditOriginalResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder
|
||||||
}));
|
{
|
||||||
|
Color = DiscordColor.Red,
|
||||||
|
Description = "An unexpected error occurred when trying to close ticket. Aborting..."
|
||||||
await Task.Delay(3000);
|
}));
|
||||||
|
Logger.Error("An unexpected error occurred when trying to close ticket:", e);
|
||||||
// Delete the channel and database entry
|
}
|
||||||
await interaction.Channel.DeleteAsync("Ticket closed.");
|
|
||||||
|
|
||||||
Database.DeleteOpenTicket(ticket.id);
|
|
||||||
|
|
||||||
closeReasons.Remove(interaction.Channel.Id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue