blob: ef317b7e1e360700750f3128564bf7940ca201f0 [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
pineafan6fb3e072022-05-20 19:27:23 +01006export async function create(guild: Discord.Guild, member: Discord.User, createdBy: Discord.User, client) {
pineafanad54d752022-04-18 19:01:43 +01007 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)),
pineafan6fb3e072022-05-20 19:27:23 +010073 createdBy: entry(createdBy.id, renderUser(createdBy)),
pineafanad54d752022-04-18 19:01:43 +010074 created: entry(new Date().getTime(), renderDelta(new Date().getTime())),
75 ticketChannel: entry(c.id, renderChannel(c)),
76 },
77 hidden: {
78 guild: guild.id
79 }
80 }
81 log(data, client);
82 } catch (e) { console.log(e); return null }
83 return c.id
84}
85
86export async function areTicketsEnabled(guild: string) {
87 let config = await readConfig(guild);
88 return config.tickets.enabled;
89}