aboutsummaryrefslogtreecommitdiff
path: root/174bg/discord-bot/source/commands/close-ticket.js
blob: a4207f95ac839e68bb42d9509db4f0099888ecd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { SlashCommandBuilder, PermissionFlagsBits } from "discord.js";

export default {
  data: new SlashCommandBuilder()
    .setName("close-ticket")
    .setDescription(
      "Closes the current ticket by logging the contents and deleting the channel.",
    )
    .setDefaultMemberPermissions(PermissionFlagsBits.Administrator),
  execute: async (interaction) => {
    const channel = interaction.channel;

    if (!channel || !channel.parent || channel.parent.name.toLowerCase() !== "tickets") {
      return await interaction.reply({
        content: "This command can only be used in a ticket channel.",
        ephemeral: true,
      });
    }

    // log the contents of the ticket and send to the logs channel if it exists
    const messages = await channel.messages.fetch({ limit: 100 });
    const log = messages
      .map((message) => `${message.author.tag}: ${message.content}`)
      .reverse()
      .join("\n");

    console.log(`Ticket log for ${channel.name}:\n${log}`);

    const logsChannel = interaction.guild.channels.cache.find(
      (ch) => ch.name === "logs" && ch.isTextBased(),
    );

    if (logsChannel)
      await logsChannel.send({
        content: `# Ticket log for ${channel.name}`,
        files: [
          {
            name: `${channel.name}.txt`,
            attachment: Buffer.from(
              messages
                .map(
                  (message) =>
                    `${message.author.tag}/${message.author.id}: ${message.content}`,
                )
                .reverse()
                .join("\n"),
            ),
          },
        ],
      });

    // delete the channel
    await channel.delete();
  },
};