blob: 4de2924887cc4d5f5b01b7659913c04769883d9b [file] [log] [blame]
pineafane625d782022-05-09 18:04:32 +01001import Discord, { MessageActionRow, MessageButton } from 'discord.js';
pineafanad54d752022-04-18 19:01:43 +01002import readConfig from '../utils/readConfig.js'
pineafane625d782022-05-09 18:04:32 +01003import generateEmojiEmbed from '../utils/generateEmojiEmbed.js';
4import getEmojiByName from "../utils/getEmojiByName.js";
pineafanad54d752022-04-18 19:01:43 +01005
6export async function create(guild: Discord.Guild, member: Discord.User, client) {
7 let config = await readConfig(guild.id);
8 // @ts-ignore
9 const { log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger
10 let overwrites = [{
11 id: member,
12 allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"],
13 type: "member"
14 }] as Discord.OverwriteResolvable[];
15 if (config.tickets.supportRole != null) {
16 overwrites.push({
17 id: guild.roles.cache.get(config.tickets.supportRole),
18 allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"],
19 type: "role"
20 })
21 }
22
23 let c;
24 try {
25 c = await guild.channels.create(member.username, {
26 type: "GUILD_TEXT",
27 topic: `${member.id} Active`,
28 parent: config.tickets.category,
29 nsfw: false,
30 permissionOverwrites: (overwrites as Discord.OverwriteResolvable[]),
31 reason: "Creating ticket"
32 })
33 } catch (e) {
34 return null
35 }
36 try {
37 await c.send(
38 {
39 content: (`<@${member.id}>` + (config.tickets.supportRole != null ? ` • <@&${config.tickets.supportRole}>` : "")),
40 allowedMentions: {
41 users: [member.id],
42 roles: (config.tickets.supportRole != null ? [config.tickets.supportRole] : [])
43 }
44 }
45 )
46 await c.send({ embeds: [new generateEmojiEmbed()
47 .setTitle("New Ticket")
48 .setDescription(
49 `Ticket created by a Moderator\n` +
50 `**Support type:** Appeal submission\n` +
51 `**Ticket ID:** \`${c.id}\`\n` +
52 `Type \`/ticket close\` to archive this ticket.`,
53 )
54 .setStatus("Success")
55 .setEmoji("GUILD.TICKET.OPEN")
pineafane625d782022-05-09 18:04:32 +010056 ], components: [new MessageActionRow().addComponents([new MessageButton()
57 .setLabel("Close")
58 .setStyle("DANGER")
59 .setCustomId("closeticket")
60 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
61 ])]})
pineafanad54d752022-04-18 19:01:43 +010062 let data = {
63 meta:{
64 type: 'ticketCreate',
65 displayName: 'Ticket Created',
66 calculateType: true,
67 color: NucleusColors.green,
68 emoji: 'GUILD.TICKET.OPEN',
69 timestamp: new Date().getTime()
70 },
71 list: {
72 ticketFor: entry(member.id, renderUser(member)),
73 created: entry(new Date().getTime(), renderDelta(new Date().getTime())),
74 ticketChannel: entry(c.id, renderChannel(c)),
75 },
76 hidden: {
77 guild: guild.id
78 }
79 }
80 log(data, client);
81 } catch (e) { console.log(e); return null }
82 return c.id
83}
84
85export async function areTicketsEnabled(guild: string) {
86 let config = await readConfig(guild);
87 return config.tickets.enabled;
88}