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 ;
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 TranscriptCommand : ApplicationCommandModule
2022-02-21 08:40:09 +00:00
{
2022-08-21 07:34:11 +00:00
[SlashRequireGuild]
[SlashCommand("transcript", "Creates a transcript of a ticket.")]
public async Task OnExecute ( InteractionContext command , [ Option ( "Ticket" , "(Optional) Ticket number to get transcript of." ) ] long ticketID = 0 )
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
await command . DeferAsync ( true ) ;
Database . Ticket ticket ;
if ( ticketID = = 0 ) // If there are no arguments use current channel
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
if ( Database . TryGetOpenTicket ( command . Channel . Id , out ticket ) )
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
try
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
await Transcriber . ExecuteAsync ( command . Channel . Id , ticket . id ) ;
2022-05-19 11:38:59 +00:00
}
2022-08-21 07:34:11 +00:00
catch ( Exception )
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
await command . EditResponseAsync ( new DiscordWebhookBuilder ( ) . AddEmbed ( new DiscordEmbedBuilder
2022-05-19 11:38:59 +00:00
{
Color = DiscordColor . Red ,
2022-08-21 07:34:11 +00:00
Description = "ERROR: Could not save transcript file. Aborting..."
} ) ) ;
throw ;
2022-05-19 11:38:59 +00:00
}
}
2022-08-21 07:34:11 +00:00
else
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
await command . EditResponseAsync ( new DiscordWebhookBuilder ( ) . AddEmbed ( new DiscordEmbedBuilder
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
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
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
await Transcriber . ExecuteAsync ( command . Channel . Id , ticket . id ) ;
2022-05-19 11:38:59 +00:00
}
2022-08-21 07:34:11 +00:00
catch ( Exception )
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
await command . EditResponseAsync ( new DiscordWebhookBuilder ( ) . AddEmbed ( new DiscordEmbedBuilder
2022-05-19 11:38:59 +00:00
{
Color = DiscordColor . Red ,
2022-08-21 07:34:11 +00:00
Description = "ERROR: Could not save transcript file. Aborting..."
} ) ) ;
throw ;
2022-05-19 11:38:59 +00:00
}
2022-02-21 08:40:09 +00:00
2022-08-21 07:34:11 +00:00
}
// 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 ) ) )
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
await command . EditResponseAsync ( new DiscordWebhookBuilder ( ) . AddEmbed ( new DiscordEmbedBuilder
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
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 ;
2022-05-19 11:38:59 +00:00
}
2022-08-21 07:34:11 +00:00
}
2022-02-21 08:40:09 +00:00
2022-08-21 07:34:11 +00:00
// Log it if the log channel exists
DiscordChannel logChannel = command . Guild . GetChannel ( Config . logChannel ) ;
if ( logChannel ! = null )
{
await using FileStream file = new FileStream ( Transcriber . GetPath ( ticket . id ) , FileMode . Open , FileAccess . Read ) ;
DiscordMessageBuilder message = new DiscordMessageBuilder ( ) ;
message . WithEmbed ( new DiscordEmbedBuilder
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
Color = DiscordColor . Green ,
Description = "Ticket " + ticket . id . ToString ( "00000" ) + " transcript generated by " + command . Member . Mention + ".\n" ,
Footer = new DiscordEmbedBuilder . EmbedFooter { Text = '#' + command . Channel . Name }
} ) ;
2023-03-23 12:21:20 +00:00
message . AddFiles ( new Dictionary < string , Stream > { { Transcriber . GetFilename ( ticket . id ) , file } } ) ;
2022-02-21 08:40:09 +00:00
2022-08-21 07:34:11 +00:00
await logChannel . SendMessageAsync ( message ) ;
}
2022-02-21 08:40:09 +00:00
2022-08-21 07:34:11 +00:00
try
{
// Send transcript in a direct message
await using FileStream file = new FileStream ( Transcriber . GetPath ( ticket . id ) , FileMode . Open , FileAccess . Read ) ;
2022-02-21 08:40:09 +00:00
2022-08-21 07:34:11 +00:00
DiscordMessageBuilder directMessage = new DiscordMessageBuilder ( ) ;
directMessage . WithEmbed ( new DiscordEmbedBuilder
2022-05-19 11:38:59 +00:00
{
2022-08-21 07:34:11 +00:00
Color = DiscordColor . Green ,
Description = "Transcript generated!\n"
} ) ;
2023-03-23 12:21:20 +00:00
directMessage . AddFiles ( new Dictionary < string , Stream > { { Transcriber . GetFilename ( ticket . id ) , file } } ) ;
2022-08-21 07:34:11 +00:00
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 ;
2022-05-19 11:38:59 +00:00
}
2022-08-21 07:34:11 +00:00
await command . EditResponseAsync ( new DiscordWebhookBuilder ( ) . AddEmbed ( new DiscordEmbedBuilder
{
Color = DiscordColor . Green ,
Description = "Transcript sent!\n"
} ) ) ;
2022-05-19 11:38:59 +00:00
}
}