aboutsummaryrefslogtreecommitdiff
path: root/174bg/discord-bot/utilities/createTicket.js
blob: 9b573d6118b15719323d01d7b753eb8da26188d9 (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
import { ChannelType, PermissionFlagsBits } from "discord.js";

export default async (interaction, roleNamesWithAccess) => {
  const ticketsCategory = interaction.guild.channels.cache.find(
    (channel) =>
      channel.name.toLowerCase() === "tickets" &&
      channel.type === ChannelType.GuildCategory,
  );

  if (!ticketsCategory)
    return await interaction.reply("Tickets category not found.");

  // get the roles that should have access to this ticket
  const rolesWithAccess = roleNamesWithAccess.map((roleName) => {
    return interaction.guild.roles.cache.find(
      (role) => role.name.toLowerCase() === roleName.toLowerCase(),
    );
  });

  // create a new text channel under the "tickets" category with the following format: ticket-<timestamp_short>
  const timestamp = new Date()
    .toISOString()
    .replace(/[-:.T]/g, "")
    .slice(2, 16);

  const channelName = `ticket-${timestamp}`;

  const newChannel = await interaction.guild.channels.create({
    name: channelName,
    type: ChannelType.GuildText,
    parent: ticketsCategory.id,
    // set permissions so that only the user who created the ticket and the roles with access can view it
    permissionOverwrites: [
      {
        id: interaction.user.id,
        allow: [
          PermissionFlagsBits.ViewChannel,
          PermissionFlagsBits.SendMessages,
        ],
      },
      ...rolesWithAccess.map((role) => ({
        id: role.id,
        allow: [
          PermissionFlagsBits.ViewChannel,
          PermissionFlagsBits.SendMessages,
        ],
      })),
    ],
  });

  return newChannel;
};