SupportChild/SupportChild/Commands/CloseCommand.cs

118 lines
3.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;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.Exceptions;
using Microsoft.Extensions.Logging;
namespace SupportChild.Commands
{
2022-05-19 11:38:59 +00:00
public class CloseCommand : BaseCommandModule
{
[Command("close")]
[Cooldown(1, 5, CooldownBucketType.User)]
public async Task OnExecute(CommandContext command, [RemainingText] string commandArgs)
{
// Check if the user has permission to use this command.
if (!Config.HasPermission(command.Member, "close"))
{
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;
}
2022-02-21 08:40:09 +00:00
2022-05-19 11:38:59 +00:00
ulong channelID = command.Channel.Id;
string channelName = command.Channel.Name;
2022-02-21 08:40:09 +00:00
2022-05-19 11:38:59 +00:00
// Check if ticket exists in the database
if (!Database.TryGetOpenTicket(channelID, out Database.Ticket ticket))
{
DiscordEmbed error = new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "This channel is not a ticket."
};
await command.RespondAsync(error);
return;
}
2022-02-21 08:40:09 +00:00
2022-05-19 11:38:59 +00:00
// Build transcript
try
{
await Transcriber.ExecuteAsync(command.Channel.Id, ticket.id);
}
catch (Exception)
{
DiscordEmbed error = new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Description = "ERROR: Could not save transcript file. Aborting..."
};
await command.RespondAsync(error);
throw;
}
2022-02-21 08:40:09 +00:00
2022-05-19 11:38:59 +00:00
// 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 }
};
2022-02-21 08:40:09 +00:00
2022-05-19 11:38:59 +00:00
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 } });
2022-02-21 08:40:09 +00:00
2022-05-19 11:38:59 +00:00
await logChannel.SendMessageAsync(message);
}
}
2022-02-21 08:40:09 +00:00
2022-05-19 11:38:59 +00:00
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 }
};
2022-02-21 08:40:09 +00:00
2022-05-19 11:38:59 +00:00
try
{
DiscordMember staffMember = await command.Guild.GetMemberAsync(ticket.creatorID);
2022-02-21 08:40:09 +00:00
2022-05-19 11:38:59 +00:00
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 } });
2022-02-21 08:40:09 +00:00
2022-05-19 11:38:59 +00:00
await staffMember.SendMessageAsync(message);
}
}
catch (NotFoundException) { }
catch (UnauthorizedException) { }
}
2022-02-21 08:40:09 +00:00
2022-05-19 11:38:59 +00:00
Database.ArchiveTicket(ticket);
2022-02-21 08:40:09 +00:00
2022-05-19 11:38:59 +00:00
// Delete the channel and database entry
await command.Channel.DeleteAsync("Ticket closed.");
2022-02-21 08:40:09 +00:00
2022-05-19 11:38:59 +00:00
Database.DeleteOpenTicket(ticket.id);
}
}
}