Advertisement
daysling

Untitled

May 15th, 2022
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {
  2.   GuildChannel,
  3.   Message,
  4.   MessageEmbed,
  5.   TextChannel,
  6.   MessageActionRow,
  7.   MessageButton,
  8. } from "discord.js";
  9. import { TicketPanel } from "../entity/Panel";
  10. import { Tickets } from "../entity/Tickets";
  11. import { BotClient } from "../interfaces/BotClient";
  12.  
  13. // setup basic command module
  14. module.exports = {
  15.   name: "accept",
  16.   description: "Accept a ticket",
  17.   usage: "accept [@roles]",
  18.   aliases: ["ticketaccept"],
  19.   async execute(
  20.     message: Message,
  21.     client: BotClient,
  22.     args: string[]
  23.   ): Promise<void> {
  24.     // check if the ticket is a valid application or ban appeal
  25.     if (
  26.       !(
  27.         (message.channel as TextChannel).name.startsWith("application") ||
  28.         (message.channel as TextChannel).name.startsWith("ban")
  29.       )
  30.     ) {
  31.       // sends a embed with description "This is not a valid ticket channel" in red color embed
  32.       let embed = new MessageEmbed()
  33.         .setColor("#ff0000")
  34.         .setDescription("This is not a valid ticket channel");
  35.       message.channel.send({ embeds: [embed] });
  36.       return;
  37.     }
  38.     // get the ticket through typeorm repoistory to make sure it exists
  39.     const ticket = await client.orm.getRepository(Tickets).findOne({
  40.       where: {
  41.         ticketChannelId: message.channel.id,
  42.       },
  43.     });
  44.     // if the ticket is not found, send a embed with description "This is not a ticket channel" in red color embed
  45.     if (!ticket) {
  46.       let embed = new MessageEmbed()
  47.         .setColor("#ff0000")
  48.         .setDescription("This is not a ticket channel");
  49.       message.channel.send({ embeds: [embed] });
  50.       return;
  51.     }
  52.     // get the type of ticket
  53.     const ticketType = ticket.ticketType;
  54.     let ticketHandler = null;
  55.     // check if the ticketType is application, if it is set ticketHandler to applicationReviewerRoles from config else set it to banAppealRoles
  56.     if (ticketType === "application") {
  57.       ticketHandler = client.config.applicationReviewerRoles;
  58.     } else {
  59.       ticketHandler = client.config.banAppealRoles;
  60.     }
  61.     // check if the author has any of the role from array ticketHandler
  62.     if (!ticketHandler.some((role) => message.member.roles.cache.has(role))) {
  63.       // sends a embed with description "You don't have permission to run this command" in red color embed
  64.       let embed = new MessageEmbed()
  65.         .setColor("#ff0000")
  66.         .setDescription("You don't have permission to run this command");
  67.       message.channel.send({ embeds: [embed] });
  68.       return;
  69.     }
  70.     // get the Panel through orm using guild id
  71.     const panel = await client.orm.getRepository(TicketPanel).findOne({
  72.       guild: message.guild.id,
  73.     });
  74.     if (!panel) {
  75.       // send a embed with description "Unable to locate panel for this guild" in red color embed
  76.       let embed = new MessageEmbed()
  77.         .setColor("#ff0000")
  78.         .setDescription("Unable to locate panel for this guild");
  79.       message.channel.send({ embeds: [embed] });
  80.       return;
  81.     }
  82.     let acceptedCategory = panel.approvedCategory;
  83.     // move the ticket to the denied category
  84.     (message.channel as GuildChannel).setParent(acceptedCategory);
  85.     // remove the permission to send message from the owner
  86.     (message.channel as TextChannel).permissionOverwrites.edit(ticket.userId, {
  87.       SEND_MESSAGES: false,
  88.       ADD_REACTIONS: false,
  89.       CREATE_PUBLIC_THREADS: false,
  90.     });
  91.     (message.channel as TextChannel).permissionOverwrites.edit(
  92.       message.guild.id,
  93.       {
  94.         SEND_MESSAGES: false,
  95.         VIEW_CHANNEL: false,
  96.         ADD_REACTIONS: false,
  97.         CREATE_PUBLIC_THREADS: false,
  98.       }
  99.     );
  100.  
  101.     // Create a message with title "Ticket denied", in description "Your ticket has been denied by ${staffMember} on ${Date} for reason ${reason}\n\nYour Ticket UUID is ${TicketUUID}\nPlease react to the following button to close the ticket" and in embed color red
  102.     let deniedEmbed = new MessageEmbed()
  103.       .setColor("#00ff55")
  104.       .setTitle("Ticket accepted")
  105.       .setDescription(`Your ticket has been accepted by our staff team.`)
  106.       .addFields(
  107.         {
  108.           name: "Staff Member",
  109.           value: message.author.username,
  110.         },
  111.         {
  112.           name: "Date",
  113.           value: new Date().toLocaleString(),
  114.         },
  115.         {
  116.           name: "Ticket UUID",
  117.           value: ticket.id,
  118.         }
  119.       );
  120.     if (message.mentions.roles.size > 0)
  121.       deniedEmbed.addField(
  122.         "Roles",
  123.         message.mentions.roles.map((role) => role.toString()).join(", ")
  124.       );
  125.     // Create a button row
  126.     let deniedButtonRow = new MessageActionRow();
  127.     // Create a button to close, with emoji  and name "Close Ticket" in Danger style
  128.     let deniedCloseButton = new MessageButton()
  129.       .setCustomId("close")
  130.       .setEmoji("❌")
  131.       .setLabel("Close Ticket")
  132.       .setStyle("DANGER");
  133.     // Add the button to the row
  134.     deniedButtonRow.addComponents(deniedCloseButton);
  135.     // send the row and embed to the channel
  136.     (message.channel as TextChannel).send({
  137.       content: `<@!${ticket.userId}>`,
  138.       embeds: [deniedEmbed],
  139.       components: [deniedButtonRow],
  140.     });
  141.     // find the member ticket.userId
  142.     let member = message.guild.members.cache.get(ticket.userId);
  143.     if (!member) return;
  144.     // loop through all the roles that is mentioned in the command and add it to the ticket owner
  145.     message.mentions.roles.forEach((role) => {
  146.       // add the role to member
  147.       member.roles.add(role);
  148.     });
  149.   },
  150. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement