xfinity
Version: 
Easy to use discord.js multipurpose package for your bot
253 lines (242 loc) • 8.62 kB
JavaScript
const {
  Client,
  MessageEmbed,
  MessageButton,
  MessageActionRow,
  MessageAttachment,
} = require("discord.js");
const chalk = require("chalk");
const fetchTranscript = require("./trans");
/**
 *
 * @param {Client} client
 */
async function handleinteraction(client, options = []) {
  client.on("interactionCreate", async (button) => {
    if (button.isButton()) {
      const close = new MessageButton()
        .setStyle(options.closeButtonColor || "DANGER")
        .setEmoji("🔒")
        .setLabel("Close")
        .setCustomId("close");
      const openbt = new MessageButton()
        .setStyle(options.reopenButtonColor || "SUCCESS")
        .setEmoji("🔓")
        .setLabel("Reopen")
        .setCustomId("openbt");
      const deletebt = new MessageButton()
        .setStyle(options.deleteButtonColor || "SECONDARY")
        .setEmoji("❌")
        .setLabel("Delete")
        .setCustomId("deletebt");
      const tr = new MessageButton()
        .setStyle(options.trasncriptButtonColor || "SECONDARY")
        .setEmoji("📰")
        .setLabel("Transcript")
        .setCustomId("tr");
      const row = new MessageActionRow().addComponents([openbt, deletebt, tr]);
      const surebtn = new MessageButton()
        .setStyle(options.yesButtonColor || "DANGER")
        .setLabel("Yes")
        .setCustomId("yes");
      const nobtn = new MessageButton()
        .setStyle(options.noButtonColor || "SUCCESS")
        .setLabel("No")
        .setCustomId("no");
      const row1 = new MessageActionRow().addComponents([surebtn, nobtn]);
      const closerow = new MessageActionRow().addComponents([close]);
      await button.deferUpdate();
      if (button.customId === "start") {
        let ticketname = `ticket-${button.user.id}`;
        if (options.ticketName) {
          ticketname = options.ticketName
            .replace("{username}", button.user.username)
            .replace("{tag}", button.user.tag);
        }
        let topic = `ticket-${button.user.id}`;
        const spam = button.guild.channels.cache.find(
          (ch) => ch.topic === topic.toLowerCase()
        );
        if (spam) {
          button.channel.send({
            content: `${
              options.spamMessage ||
              `A ticket named \`${ticketname}\` has already been opened. Close it first to reopen new one.`
            }`,
          });
          return;
        }
        try {
          button.guild.channels
            .create(ticketname, {
              type: "GUILD_TEXT",
              topic: topic,
              parent: options.parentId || null,
              permissionOverwrites: [
                {
                  id: button.message.guild.roles.everyone,
                  deny: [
                    "VIEW_CHANNEL",
                    "SEND_MESSAGES",
                    "READ_MESSAGE_HISTORY",
                  ],
                },
                {
                  id: button.user.id,
                  allow: [
                    "VIEW_CHANNEL",
                    "SEND_MESSAGES",
                    "READ_MESSAGE_HISTORY",
                  ],
                },
              ],
            })
            .then((pussy) => {
              const reembed = new MessageEmbed()
                .setTitle("Ticket Created")
                .setDescription(
                  `${
                    options.ticketOpenMessage ||
                    `A new Ticket has been created by ${button.user}.`
                  }`
                )
                .setThumbnail(button.message.guild.iconURL())
                .setTimestamp()
                .setColor(options.ticketOpenEmbedColor || "#075FFF")
                .setFooter(
                  `Requested by - ${button.user.tag}`,
                  button.user.displayAvatarURL({ dynamic: true })
                );
              pussy.send({
                content: `${button.user}`,
                embeds: [reembed],
                components: [closerow],
              });
            });
        } catch (e) {
          console.log(chalk.redBright(`Error Ocurred. | Error: ${e.stack}`));
        }
      } else if (button.customId === "close") {
        button.channel.permissionOverwrites
          .edit(button.user.id, {
            SEND_MESSAGES: false,
            VIEW_CHANNEL: true,
          })
          .catch((err) => {});
        const reembed = new MessageEmbed()
          .setTitle("Ticket Created")
          .setDescription(
            `${
              options.ticketOpenMessage ||
              `A new Ticket has been created by ${button.user}.`
            }`
          )
          .setThumbnail(button.message.guild.iconURL())
          .setTimestamp()
          .setColor(options.ticketOpenEmbedColor || "#075FFF")
          .setFooter(
            `Requested by - ${button.user.tag}`,
            button.user.displayAvatarURL({ dynamic: true })
          );
        button.message.edit({
          content: `${button.user}`,
          embeds: [reembed],
          components: [row],
        });
      } else if (button.customId === "openbt") {
        button.channel.permissionOverwrites
          .edit(button.user.id, {
            SEND_MESSAGES: true,
            VIEW_CHANNEL: true,
          })
          .catch((err) => {});
        const emb = new MessageEmbed()
          .setTitle("Ticket Created")
          .setDescription(
            `${
              options.ticketOpenMessage ||
              `A new Ticket has been created by ${button.user}.`
            }`
          )
          .setThumbnail(button.message.guild.iconURL())
          .setTimestamp()
          .setColor(options.ticketOpenEmbedColor || "#075FFF")
          .setFooter(
            `Requested by - ${button.user.tag}`,
            button.user.displayAvatarURL({ dynamic: true })
          );
        button.message.edit({
          content: `${button.user}`,
          embedDesc: [emb],
          components: [closerow],
        });
        button.channel.send({
          content: "Reopened!",
        });
      } else if (button.customId === "deletebt") {
        const emb2 = new MessageEmbed()
          .setTitle("Are you sure ?")
          .setDescription(
            `${
              options.confirmationMessage ||
              `Clicking yes will result in channel delete and ticket delete. You cant undo this action`
            }`
          )
          .setTimestamp()
          .setColor(options.confirmationEmbedColor || "#c90000")
          .setTimestamp();
        button.channel.send({ embeds: [emb2], components: [row1] });
      } else if (button.customId === "yes") {
        button.channel.send({
          content:
            options.ticketDeleteMessage ||
            "Deleting the ticket and channel.. Please wait.",
        });
        setTimeout(() => {
          let fuck = button.guild.channels.cache.get(button.message.channel.id);
          fuck.delete().catch((err) => {
            button.message.channel.send({
              content: "An Error Occured. " + err,
            });
          });
        }, 5000);
      } else if (button.customId === "no") {
        button.message.delete();
        button.channel.send({
          content: options.ticketDeleteCancelMessage || "Aborted!",
        });
      }
      const transEmbed = new MessageEmbed()
        .setColor("RED")
        .setTitle(`Hey` + ` ` + button.user.username)
        .setDescription(
          options.closeDescription ||
            `Are you sure you want to delete **this** ticket ?`
        );
      if (button.customId === "del") {
        const ok = new MessageActionRow().addComponents(
          new MessageButton()
            .setCustomId("sure")
            .setLabel(`Delete`)
            .setStyle(options.deleteChannelButtonColor || "DANGER")
        );
        await button.channel.send({
          embeds: [transEmbed],
          components: [ok],
        });
      }
      if (button.customId === "sure") {
        button.channel.delete();
      }
    }
    if (button.customId === "del2") {
      await button.channel.delete();
    } else if (button.customId === "tr") {
      fetchTranscript(button.channel, button, 100).then((data) => {
        const file = new MessageAttachment(data, "index.html");
        button.channel.send({ files: [file] });
      });
    }
  });
}
module.exports = handleinteraction;