SupportChild/Commands/CloseCommand.cs

146 lines
5.6 KiB
C#
Raw Normal View History

2022-04-18 10:52:03 +00:00
using System;
2022-02-21 08:40:09 +00:00
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
2022-08-21 07:34:11 +00:00
using DSharpPlus;
2022-02-21 08:40:09 +00:00
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
2022-08-21 07:34:11 +00:00
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
2022-02-21 08:40:09 +00:00
2022-08-21 07:34:11 +00:00
namespace SupportChild.Commands;
public class CloseCommand : ApplicationCommandModule
2022-02-21 08:40:09 +00:00
{
2024-10-29 10:14:47 +00:00
private static Dictionary<ulong, string> closeReasons = new Dictionary<ulong, string>();
[SlashRequireGuild]
[SlashCommand("close", "Closes a ticket.")]
2024-10-29 10:17:44 +00:00
public async Task OnExecute(InteractionContext command, [Option("Reason", "(Optional) Reason for closing the ticket.")] string reason = "")
{
// 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, "supportchild_closeconfirm", "Confirm"));
await command.CreateResponseAsync(confirmation);
2024-10-29 10:14:47 +00:00
closeReasons.Add(command.Channel.Id, reason);
}
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;
}
2024-10-29 10:14:47 +00:00
string closeReason = "";
if (closeReasons.TryGetValue(channelID, out string cachedReason))
{
closeReason = "\nReason: " + cachedReason + "\n";
}
// Log it if the log channel exists
DiscordChannel logChannel = interaction.Guild.GetChannel(Config.logChannel);
if (logChannel != null)
{
DiscordEmbed embed = new DiscordEmbedBuilder
{
Color = DiscordColor.Green,
2024-10-29 10:14:47 +00:00
Description = "Ticket " + ticket.id.ToString("00000") + " closed by " +
interaction.User.Mention + ".\n" + closeReason,
Footer = new DiscordEmbedBuilder.EmbedFooter { Text = '#' + channelName }
};
await using FileStream file = new FileStream(Transcriber.GetPath(ticket.id), FileMode.Open, FileAccess.Read);
DiscordMessageBuilder message = new DiscordMessageBuilder();
2024-10-29 09:52:02 +00:00
message.AddEmbed(embed);
message.AddFiles(new Dictionary<string, Stream> { { Transcriber.GetFilename(ticket.id), file } });
await logChannel.SendMessageAsync(message);
}
if (Config.closingNotifications)
{
DiscordEmbed embed = new DiscordEmbedBuilder
{
Color = DiscordColor.Green,
2024-10-29 10:14:47 +00:00
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 = '#' + 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();
2024-10-29 09:52:02 +00:00
message.AddEmbed(embed);
message.AddFiles(new Dictionary<string, Stream> { { 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..."
}));
2024-10-29 10:14:47 +00:00
await Task.Delay(3000);
// Delete the channel and database entry
await interaction.Channel.DeleteAsync("Ticket closed.");
Database.DeleteOpenTicket(ticket.id);
2024-10-29 10:14:47 +00:00
closeReasons.Remove(channelID);
}
2022-05-19 11:38:59 +00:00
}