示例#1
0
        public async Task OnExecute(CommandContext command)
        {
            // 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("", false, error);

                command.Client.DebugLogger.LogMessage(LogLevel.Info, "SupportBoi", "User tried to use the close command but did not have permission.", DateTime.UtcNow);
                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))
            {
                DiscordEmbed error = new DiscordEmbedBuilder
                {
                    Color       = DiscordColor.Red,
                    Description = "This channel is not a ticket."
                };
                await command.RespondAsync("", false, error);

                return;
            }

            // Build transcript
            try
            {
                await Transcriber.ExecuteAsync(command.Channel.Id.ToString(), ticket.id);
            }
            catch (Exception)
            {
                DiscordEmbed error = new DiscordEmbedBuilder
                {
                    Color       = DiscordColor.Red,
                    Description = "ERROR: Could not save transcript file. Aborting..."
                };
                await command.RespondAsync("", false, error);

                throw;
            }
            string filePath = Transcriber.GetPath(ticket.id);

            // Log it if the log channel exists
            DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel);

            if (logChannel != null)
            {
                DiscordEmbed logMessage = new DiscordEmbedBuilder
                {
                    Color       = DiscordColor.Green,
                    Description = "Ticket " + ticket.id.ToString("00000") + " closed by " + command.Member.Mention + ".\n",
                    Footer      = new DiscordEmbedBuilder.EmbedFooter {
                        Text = '#' + channelName
                    }
                };
                await logChannel.SendFileAsync(filePath, "", false, logMessage);
            }

            if (Config.closingNotifications)
            {
                DiscordEmbed message = 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);

                    DiscordMessage dm = await staffMember.SendFileAsync(filePath, "", false, message);

                    await dm.CreateReactionAsync(DiscordEmoji.FromName(command.Client, ":one:"));

                    await dm.CreateReactionAsync(DiscordEmoji.FromName(command.Client, ":two:"));

                    await dm.CreateReactionAsync(DiscordEmoji.FromName(command.Client, ":three:"));

                    await dm.CreateReactionAsync(DiscordEmoji.FromName(command.Client, ":four:"));

                    await dm.CreateReactionAsync(DiscordEmoji.FromName(command.Client, ":five:"));
                }
                catch (NotFoundException) { }
                catch (UnauthorizedException) { }
            }

            using (MySqlConnection c = Database.GetConnection())
            {
                // Create an entry in the ticket history database
                MySqlCommand archiveTicket = new MySqlCommand(@"INSERT INTO ticket_history (id, created_time, closed_time, creator_id, assigned_staff_id, summary, channel_id, rating) VALUES (@id, @created_time, now(), @creator_id, @assigned_staff_id, @summary, @channel_id, @rating);", c);
                archiveTicket.Parameters.AddWithValue("@id", ticket.id);
                archiveTicket.Parameters.AddWithValue("@created_time", ticket.createdTime);
                archiveTicket.Parameters.AddWithValue("@creator_id", ticket.creatorID);
                archiveTicket.Parameters.AddWithValue("@assigned_staff_id", ticket.assignedStaffID);
                archiveTicket.Parameters.AddWithValue("@summary", ticket.summary);
                archiveTicket.Parameters.AddWithValue("@channel_id", channelID);
                archiveTicket.Parameters.AddWithValue("@rating", 0);

                c.Open();
                archiveTicket.ExecuteNonQuery();

                // Delete the channel and database entry
                await command.Channel.DeleteAsync("Ticket closed.");

                MySqlCommand deletion = new MySqlCommand(@"DELETE FROM tickets WHERE channel_id=@channel_id", c);
                deletion.Parameters.AddWithValue("@channel_id", channelID);
                deletion.Prepare();
                deletion.ExecuteNonQuery();

                Sheets.DeleteTicketQueued(ticket.id);
            }
        }
        public async Task OnExecute(CommandContext command, [RemainingText] string commandArgs)
        {
            // Check if the user has permission to use this command.
            if (!Config.HasPermission(command.Member, "transcript"))
            {
                DiscordEmbed error = new DiscordEmbedBuilder
                {
                    Color       = DiscordColor.Red,
                    Description = "You do not have permission to use this command."
                };
                await command.RespondAsync("", false, error);

                command.Client.Logger.Log(LogLevel.Information, "User tried to use the transcript command but did not have permission.");
                return;
            }

            Database.Ticket ticket;
            string          strippedMessage = command.Message.Content.Replace(Config.prefix, "");

            string[] parsedMessage = strippedMessage.Replace("<@!", "").Replace("<@", "").Replace(">", "").Split();

            // If there are no arguments use current channel
            if (parsedMessage.Length < 2)
            {
                if (Database.TryGetOpenTicket(command.Channel.Id, out ticket))
                {
                    try
                    {
                        await Transcriber.ExecuteAsync(ticket.channelID.ToString(), ticket.id);
                    }
                    catch (Exception)
                    {
                        DiscordEmbed error = new DiscordEmbedBuilder
                        {
                            Color       = DiscordColor.Red,
                            Description = "ERROR: Could not save transcript file. Aborting..."
                        };
                        await command.RespondAsync("", false, error);

                        throw;
                    }
                }
                else
                {
                    DiscordEmbed error = new DiscordEmbedBuilder
                    {
                        Color       = DiscordColor.Red,
                        Description = "This channel is not a ticket."
                    };
                    await command.RespondAsync("", false, error);

                    return;
                }
            }
            else
            {
                // Check if argument is numerical, if not abort
                if (!uint.TryParse(parsedMessage[1], out uint ticketID))
                {
                    DiscordEmbed error = new DiscordEmbedBuilder
                    {
                        Color       = DiscordColor.Red,
                        Description = "Argument must be a number."
                    };
                    await command.RespondAsync("", false, error);

                    return;
                }

                // If the ticket is still open, generate a new fresh transcript
                if (Database.TryGetOpenTicketByID(ticketID, out ticket) && ticket?.creatorID == command.Member.Id)
                {
                    try
                    {
                        await Transcriber.ExecuteAsync(ticket.channelID.ToString(), ticket.id);
                    }
                    catch (Exception)
                    {
                        DiscordEmbed error = new DiscordEmbedBuilder
                        {
                            Color       = DiscordColor.Red,
                            Description = "ERROR: Could not save transcript file. Aborting..."
                        };
                        await command.RespondAsync("", false, error);

                        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(ticketID, out ticket) || (ticket?.creatorID != command.Member.Id && !Database.IsStaff(command.Member.Id)))
                {
                    DiscordEmbed error = new DiscordEmbedBuilder
                    {
                        Color       = DiscordColor.Red,
                        Description = "Could not find a closed ticket with that number which you opened." + (Config.HasPermission(command.Member, "list") ? "\n(Use the " + Config.prefix + "list command to see all your tickets)" : "")
                    };
                    await command.RespondAsync("", false, error);

                    return;
                }
            }

            string filePath = Transcriber.GetPath(ticket.id);

            // Log it if the log channel exists
            DiscordChannel logChannel = command.Guild.GetChannel(Config.logChannel);

            if (logChannel != null)
            {
                DiscordEmbed logMessage = new DiscordEmbedBuilder
                {
                    Color       = DiscordColor.Green,
                    Description = "Ticket " + ticket.id.ToString("00000") + " transcript generated by " + command.Member.Mention + ".\n",
                    Footer      = new DiscordEmbedBuilder.EmbedFooter {
                        Text = '#' + command.Channel.Name
                    }
                };
                await logChannel.SendFileAsync(filePath, "", false, logMessage);
            }

            try
            {
                // Send transcript privately
                DiscordEmbed directMessage = new DiscordEmbedBuilder
                {
                    Color       = DiscordColor.Green,
                    Description = "Transcript generated, " + command.Member.Mention + "!\n"
                };
                await command.Member.SendFileAsync(filePath, "", false, directMessage);

                // Respond to message directly
                DiscordEmbed response = new DiscordEmbedBuilder
                {
                    Color       = DiscordColor.Green,
                    Description = "Transcript sent, " + command.Member.Mention + "!\n"
                };
                await command.RespondAsync("", false, response);
            }
            catch (UnauthorizedException)
            {
                // Send transcript privately
                DiscordEmbed error = new DiscordEmbedBuilder
                {
                    Color       = DiscordColor.Red,
                    Description = "Not allowed to send direct message to you, " + command.Member.Mention + ", please check your privacy settings.\n"
                };
                await command.RespondAsync("", false, error);
            }
        }
示例#3
0
        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;
            }

            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))
            {
                DiscordEmbed error = new DiscordEmbedBuilder
                {
                    Color       = DiscordColor.Red,
                    Description = "This channel is not a ticket."
                };
                await command.RespondAsync(error);

                return;
            }

            // 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;
            }

            // 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);
        }