Updated commands
This commit is contained in:
parent
0e5a93a6a6
commit
94df384934
32 changed files with 1926 additions and 2059 deletions
SupportChild/Commands
|
@ -2,117 +2,133 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using DSharpPlus.CommandsNext;
|
||||
using DSharpPlus.CommandsNext.Attributes;
|
||||
using DSharpPlus;
|
||||
using DSharpPlus.Entities;
|
||||
using DSharpPlus.Exceptions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using DSharpPlus.SlashCommands;
|
||||
using DSharpPlus.SlashCommands.Attributes;
|
||||
|
||||
namespace SupportChild.Commands
|
||||
namespace SupportChild.Commands;
|
||||
|
||||
public class CloseCommand : ApplicationCommandModule
|
||||
{
|
||||
public class CloseCommand : BaseCommandModule
|
||||
[SlashRequireGuild]
|
||||
[SlashCommand("close", "Closes a ticket.")]
|
||||
public async Task OnExecute(InteractionContext command)
|
||||
{
|
||||
[Command("close")]
|
||||
[Cooldown(1, 5, CooldownBucketType.User)]
|
||||
public async Task OnExecute(CommandContext command, [RemainingText] string commandArgs)
|
||||
// Check if ticket exists in the database
|
||||
if (!Database.TryGetOpenTicket(command.Channel.Id, out Database.Ticket _))
|
||||
{
|
||||
// Check if the user has permission to use this command.
|
||||
if (!Config.HasPermission(command.Member, "close"))
|
||||
await command.CreateResponseAsync(new DiscordEmbedBuilder
|
||||
{
|
||||
DiscordEmbed error = new DiscordEmbedBuilder
|
||||
{
|
||||
Color = DiscordColor.Red,
|
||||
Description = "You do not have permission to use this command."
|
||||
};
|
||||
await command.RespondAsync(error);
|
||||
command.Client.Logger.Log(LogLevel.Information, "User tried to use the close command but did not have permission.");
|
||||
return;
|
||||
}
|
||||
Color = DiscordColor.Red,
|
||||
Description = "This channel is not a ticket."
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
ulong channelID = command.Channel.Id;
|
||||
string channelName = command.Channel.Name;
|
||||
|
||||
// Check if ticket exists in the database
|
||||
if (!Database.TryGetOpenTicket(channelID, out Database.Ticket ticket))
|
||||
DiscordInteractionResponseBuilder confirmation = new DiscordInteractionResponseBuilder()
|
||||
.AddEmbed(new DiscordEmbedBuilder
|
||||
{
|
||||
DiscordEmbed error = new DiscordEmbedBuilder
|
||||
{
|
||||
Color = DiscordColor.Red,
|
||||
Description = "This channel is not a ticket."
|
||||
};
|
||||
await command.RespondAsync(error);
|
||||
return;
|
||||
}
|
||||
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, "supportboi_closeconfirm", "Confirm"));
|
||||
|
||||
|
||||
await command.CreateResponseAsync(confirmation);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Log it if the log channel exists
|
||||
DiscordChannel logChannel = interaction.Guild.GetChannel(Config.logChannel);
|
||||
if (logChannel != null)
|
||||
{
|
||||
DiscordEmbed embed = new DiscordEmbedBuilder
|
||||
{
|
||||
Color = DiscordColor.Green,
|
||||
Description = "Ticket " + ticket.id.ToString("00000") + " closed by " + interaction.User.Mention + ".\n",
|
||||
Footer = new DiscordEmbedBuilder.EmbedFooter { Text = '#' + channelName }
|
||||
};
|
||||
|
||||
await using FileStream file = new FileStream(Transcriber.GetPath(ticket.id), FileMode.Open, FileAccess.Read);
|
||||
DiscordMessageBuilder message = new DiscordMessageBuilder();
|
||||
message.WithEmbed(embed);
|
||||
message.WithFiles(new Dictionary<string, Stream> { { Transcriber.GetFilename(ticket.id), file } });
|
||||
|
||||
await logChannel.SendMessageAsync(message);
|
||||
}
|
||||
|
||||
if (Config.closingNotifications)
|
||||
{
|
||||
DiscordEmbed embed = 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",
|
||||
Footer = new DiscordEmbedBuilder.EmbedFooter { Text = '#' + channelName }
|
||||
};
|
||||
|
||||
// Build transcript
|
||||
try
|
||||
{
|
||||
await Transcriber.ExecuteAsync(command.Channel.Id, ticket.id);
|
||||
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();
|
||||
message.WithEmbed(embed);
|
||||
message.WithFiles(new Dictionary<string, Stream> { { Transcriber.GetFilename(ticket.id), file } });
|
||||
|
||||
await staffMember.SendMessageAsync(message);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
DiscordEmbed error = new DiscordEmbedBuilder
|
||||
{
|
||||
Color = DiscordColor.Red,
|
||||
Description = "ERROR: Could not save transcript file. Aborting..."
|
||||
};
|
||||
await command.RespondAsync(error);
|
||||
throw;
|
||||
}
|
||||
|
||||
// Log it if the log channel exists
|
||||
DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel);
|
||||
if (logChannel != null)
|
||||
{
|
||||
DiscordEmbed embed = new DiscordEmbedBuilder
|
||||
{
|
||||
Color = DiscordColor.Green,
|
||||
Description = "Ticket " + ticket.id.ToString("00000") + " closed by " + command.Member.Mention + ".\n",
|
||||
Footer = new DiscordEmbedBuilder.EmbedFooter { Text = '#' + channelName }
|
||||
};
|
||||
|
||||
using (FileStream file = new FileStream(Transcriber.GetPath(ticket.id), FileMode.Open, FileAccess.Read))
|
||||
{
|
||||
DiscordMessageBuilder message = new DiscordMessageBuilder();
|
||||
message.WithEmbed(embed);
|
||||
message.WithFiles(new Dictionary<string, Stream>() { { Transcriber.GetFilename(ticket.id), file } });
|
||||
|
||||
await logChannel.SendMessageAsync(message);
|
||||
}
|
||||
}
|
||||
|
||||
if (Config.closingNotifications)
|
||||
{
|
||||
DiscordEmbed embed = 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",
|
||||
Footer = new DiscordEmbedBuilder.EmbedFooter { Text = '#' + channelName }
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
DiscordMember staffMember = await command.Guild.GetMemberAsync(ticket.creatorID);
|
||||
|
||||
using (FileStream file = new FileStream(Transcriber.GetPath(ticket.id), FileMode.Open, FileAccess.Read))
|
||||
{
|
||||
DiscordMessageBuilder message = new DiscordMessageBuilder();
|
||||
message.WithEmbed(embed);
|
||||
message.WithFiles(new Dictionary<string, Stream>() { { Transcriber.GetFilename(ticket.id), file } });
|
||||
|
||||
await staffMember.SendMessageAsync(message);
|
||||
}
|
||||
}
|
||||
catch (NotFoundException) { }
|
||||
catch (UnauthorizedException) { }
|
||||
}
|
||||
|
||||
Database.ArchiveTicket(ticket);
|
||||
|
||||
// Delete the channel and database entry
|
||||
await command.Channel.DeleteAsync("Ticket closed.");
|
||||
|
||||
Database.DeleteOpenTicket(ticket.id);
|
||||
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..."
|
||||
}));
|
||||
|
||||
|
||||
await Task.Delay(3000);
|
||||
|
||||
// Delete the channel and database entry
|
||||
await interaction.Channel.DeleteAsync("Ticket closed.");
|
||||
|
||||
Database.DeleteOpenTicket(ticket.id);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue