using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Threading.Tasks; using DSharpPlus.Commands; using DSharpPlus.Commands.ContextChecks; using DSharpPlus.Commands.Processors.SlashCommands; using DSharpPlus.Entities; using DSharpPlus.Exceptions; namespace SupportChild.Commands; public class TranscriptCommand { [RequireGuild] [Command("transcript")] [Description("Creates a transcript of a ticket.")] public async Task OnExecute(SlashCommandContext command, [Parameter("ticket-id")][Description("(Optional) Ticket number to get transcript of.")] long ticketID = 0) { await command.DeferResponseAsync(true); Database.Ticket ticket; if (ticketID == 0) // If there are no arguments use current channel { if (Database.TryGetOpenTicket(command.Channel.Id, out ticket)) { try { await Transcriber.ExecuteAsync(command.Channel.Id, ticket.id); } catch (Exception) { await command.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder { Color = DiscordColor.Red, Description = "ERROR: Could not save transcript file. Aborting..." })); throw; } } else { await command.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder { Color = DiscordColor.Red, Description = "This channel is not a ticket." })); return; } } else { // If the ticket is still open, generate a new fresh transcript if (Database.TryGetOpenTicketByID((uint)ticketID, out ticket) && ticket?.creatorID == command.Member.Id) { try { await Transcriber.ExecuteAsync(command.Channel.Id, ticket.id); } catch (Exception) { await command.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder { Color = DiscordColor.Red, Description = "ERROR: Could not save transcript file. Aborting..." })); throw; } } // If there is no open or closed ticket, send an error. If there is a closed ticket we will simply use the old transcript from when the ticket was closed. else if (!Database.TryGetClosedTicket((uint)ticketID, out ticket) || (ticket?.creatorID != command.Member.Id && !Database.IsStaff(command.Member.Id))) { await command.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder { Color = DiscordColor.Red, Description = "Could not find a closed ticket with that number which you opened.\n(Use the /list command to see all your tickets)" })); return; } } try { // Log it if the log channel exists DiscordChannel logChannel = await SupportChild.client.GetChannelAsync(Config.logChannel); await using FileStream file = new FileStream(Transcriber.GetPath(ticket.id), 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 { { Transcriber.GetFilename(ticket.id), file } }); await logChannel.SendMessageAsync(message); } catch (NotFoundException) { Logger.Error("Could not find the log channel."); } try { // Send transcript in a direct message await using FileStream file = new FileStream(Transcriber.GetPath(ticket.id), FileMode.Open, FileAccess.Read); DiscordMessageBuilder directMessage = new DiscordMessageBuilder(); directMessage.AddEmbed(new DiscordEmbedBuilder { Color = DiscordColor.Green, Description = "Transcript generated!\n" }); directMessage.AddFiles(new Dictionary { { Transcriber.GetFilename(ticket.id), file } }); await command.Member.SendMessageAsync(directMessage); } catch (UnauthorizedException) { await command.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder { Color = DiscordColor.Red, Description = "Not allowed to send direct message to you, please check your privacy settings.\n" })); return; } await command.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(new DiscordEmbedBuilder { Color = DiscordColor.Green, Description = "Transcript sent!\n" })); } }