blob: 2ab9a465ea371748e1da5039171fdb8c9b1b1b73 [file] [log] [blame]
pineafane625d782022-05-09 18:04:32 +01001import Discord, { MessageActionRow, MessageButton } from 'discord.js';
pineafan4edb7762022-06-26 19:21:04 +01002import EmojiEmbed from '../utils/generateEmojiEmbed.js';
pineafane625d782022-05-09 18:04:32 +01003import getEmojiByName from "../utils/getEmojiByName.js";
pineafan6702cef2022-06-13 17:52:37 +01004import client from "../utils/client.js";
pineafanad54d752022-04-18 19:01:43 +01005
pineafan6702cef2022-06-13 17:52:37 +01006export async function create(guild: Discord.Guild, member: Discord.User, createdBy: Discord.User, reason: string) {
pineafan4edb7762022-06-26 19:21:04 +01007 let config = await client.database.guilds.read(guild.id);
pineafanad54d752022-04-18 19:01:43 +01008 // @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[];
pineafan6702cef2022-06-13 17:52:37 +010015 overwrites.push({
16 id: guild.roles.everyone,
17 deny: ["VIEW_CHANNEL"],
18 type: "role"
19 })
pineafanad54d752022-04-18 19:01:43 +010020 if (config.tickets.supportRole != null) {
21 overwrites.push({
22 id: guild.roles.cache.get(config.tickets.supportRole),
23 allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "ATTACH_FILES", "ADD_REACTIONS", "READ_MESSAGE_HISTORY"],
24 type: "role"
25 })
26 }
27
28 let c;
29 try {
30 c = await guild.channels.create(member.username, {
31 type: "GUILD_TEXT",
32 topic: `${member.id} Active`,
33 parent: config.tickets.category,
34 nsfw: false,
35 permissionOverwrites: (overwrites as Discord.OverwriteResolvable[]),
36 reason: "Creating ticket"
37 })
38 } catch (e) {
39 return null
40 }
41 try {
42 await c.send(
43 {
44 content: (`<@${member.id}>` + (config.tickets.supportRole != null ? ` • <@&${config.tickets.supportRole}>` : "")),
45 allowedMentions: {
46 users: [member.id],
47 roles: (config.tickets.supportRole != null ? [config.tickets.supportRole] : [])
48 }
49 }
50 )
pineafan4edb7762022-06-26 19:21:04 +010051 await c.send({ embeds: [new EmojiEmbed()
pineafanad54d752022-04-18 19:01:43 +010052 .setTitle("New Ticket")
53 .setDescription(
54 `Ticket created by a Moderator\n` +
pineafan6702cef2022-06-13 17:52:37 +010055 `**Support type:** Appeal submission\n` + (reason != null ? `**Reason:**\n> ${reason}\n` : "") +
pineafanad54d752022-04-18 19:01:43 +010056 `**Ticket ID:** \`${c.id}\`\n` +
pineafanda6e5342022-07-03 10:03:16 +010057 `Type \`/ticket close\` to close this ticket.`,
pineafanad54d752022-04-18 19:01:43 +010058 )
59 .setStatus("Success")
60 .setEmoji("GUILD.TICKET.OPEN")
pineafane625d782022-05-09 18:04:32 +010061 ], components: [new MessageActionRow().addComponents([new MessageButton()
62 .setLabel("Close")
63 .setStyle("DANGER")
64 .setCustomId("closeticket")
65 .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
66 ])]})
pineafanad54d752022-04-18 19:01:43 +010067 let data = {
68 meta:{
69 type: 'ticketCreate',
70 displayName: 'Ticket Created',
71 calculateType: true,
72 color: NucleusColors.green,
73 emoji: 'GUILD.TICKET.OPEN',
74 timestamp: new Date().getTime()
75 },
76 list: {
77 ticketFor: entry(member.id, renderUser(member)),
pineafan6fb3e072022-05-20 19:27:23 +010078 createdBy: entry(createdBy.id, renderUser(createdBy)),
pineafanad54d752022-04-18 19:01:43 +010079 created: entry(new Date().getTime(), renderDelta(new Date().getTime())),
80 ticketChannel: entry(c.id, renderChannel(c)),
81 },
82 hidden: {
83 guild: guild.id
84 }
85 }
pineafan4edb7762022-06-26 19:21:04 +010086 log(data);
pineafanad54d752022-04-18 19:01:43 +010087 } catch (e) { console.log(e); return null }
88 return c.id
89}
90
91export async function areTicketsEnabled(guild: string) {
pineafan4edb7762022-06-26 19:21:04 +010092 let config = await client.database.guilds.read(guild);
pineafanad54d752022-04-18 19:01:43 +010093 return config.tickets.enabled;
94}