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

/**
 * Creates a new ticket channel under the "tickets" category with the specified options.
 * 
 * @param {Interaction} interaction The interaction object from Discord.js.
 * @param {Object} options The options for creating the ticket.
 * @param {Array<string>} options.roleNamesWithAccess The names of the roles that should have access to the ticket.
 * @param {string} options.ticketNamePrefix The prefix for the ticket channel name.
 * 
 * @returns {Promise<Channel>} The newly created ticket channel.
 */
export default async function createTicket(
  interaction,
  options = { roleNamesWithAccess: [], ticketNamePrefix: "ticket-" },
) {
  const ticketsCategory = interaction.guild.channels.cache.find(
    (channel) =>
      channel.name.toLowerCase() === "tickets" &&
      channel.type === ChannelType.GuildCategory,
  );

  if (!ticketsCategory) throw new Error("Tickets category not found in the guild.");

  const rolesWithAccess = options.roleNamesWithAccess.map((roleName) => {
    return interaction.guild.roles.cache.find(
      (role) => role.name.toLowerCase() === roleName.toLowerCase(),
    );
  });

  const timestamp = Date.now().toString(36); // base36

  const newChannel = await interaction.guild.channels.create({
    name: `${options.ticketNamePrefix}-${timestamp}`,
    type: ChannelType.GuildText,
    parent: ticketsCategory.id,
    permissionOverwrites: [
      {
        id: interaction.user.id,
        allow: [
          PermissionFlagsBits.ViewChannel,
          PermissionFlagsBits.SendMessages,
        ],
      },
      ...rolesWithAccess.map((role) => ({
        id: role.id,
        allow: [
          PermissionFlagsBits.ViewChannel,
          PermissionFlagsBits.SendMessages,
        ],
      })),
    ],
  });

  return newChannel;
};