Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require("dotenv").config();
  2. const discord = require("discord.js");
  3. const client = new discord.Client();
  4. const db = require("./database/database");
  5. const Ticket = require("./models/Ticket");
  6.  
  7. client.login(process.env.BOT_TOKEN);
  8.  
  9. client.on("ready", () => {
  10.   console.log("bot is logged in");
  11.   db.authenticate()
  12.     .then(() => {
  13.       console.log("logged in. db ");
  14.       Ticket.init(db);
  15.       Ticket.sync();
  16.     })
  17.     .catch(err => console.log(err));
  18. });
  19.  
  20. client.on("raw", async payload => {
  21.   let eventName = payload.t;
  22.   if (eventName === "MESSAGE_REACTION_ADD") {
  23.     let msgId = payload.d.message_id;
  24.     if (msgId === "668923818059890720") { //change message ID during live
  25.       /* change message id from discord */
  26.       let channelId = payload.d.channel_id;
  27.       let channel = client.channels.get(channelId);
  28.       if (channel) {
  29.         if (channel.messages.has(msgId)) return;
  30.         else {
  31.           try {
  32.             let msg = await channel.fetchMessage(msgId);
  33.             let reaction = msg.reactions.get("📥");
  34.             let user = client.users.get(payload.d.user_id);
  35.             client.emit("messageReactionAdd", reaction, user);
  36.           } catch (ex) {
  37.             console.log(ex);
  38.             return;
  39.           }
  40.         }
  41.       }
  42.     } else if (payload.d.emoji.name === "✅") {
  43.       try {
  44.         let findTicket = await Ticket.findOne({
  45.           where: { closeMessageId: msgId }
  46.         });
  47.         if (findTicket) {
  48.           let channelId = payload.d.channel_id;
  49.           let channel = client.channels.get(channelId);
  50.           if (channel) {
  51.             if (channel.messages.has(msgId)) return;
  52.             else {
  53.               try {
  54.                 let msg = await channel.fetchMessage(msgId);
  55.                 let reaction = msg.reactions.get("✅");
  56.                 let user = client.users.get(payload.d.user_id);
  57.                 reaction.ticket = findTicket;
  58.                 client.emit("messageReactionAdd", reaction, user);
  59.               } catch (ex) {
  60.                 console.log(ex);
  61.                 return;
  62.               }
  63.             }
  64.           }
  65.         } else {
  66.           console.log("Request not found");
  67.         }
  68.       } catch (ex) {
  69.         console.log(ex);
  70.       }
  71.     } else if (payload.d.emoji.name === "▶️") {
  72.       try {
  73.         let findTicket = await Ticket.findOne({
  74.           where: { closeMessageId: msgId }
  75.         });
  76.         if (findTicket) {
  77.           let channelId = payload.d.channel_id;
  78.           let channel = client.channels.get(channelId);
  79.           if (channel) {
  80.             if (channel.messages.has(msgId)) return;
  81.             else {
  82.               try {
  83.                 let msg = await channel.fetchMessage(msgId);
  84.                 let reaction = msg.reactions.get("▶️");
  85.                 let user = client.users.get(payload.d.user_id);
  86.                 client.emit("messageReactionAdd", reaction, user);
  87.               } catch (ex) {
  88.                 console.log(ex);
  89.               }
  90.             }
  91.           }
  92.         } else {
  93.           console.log("Request not found");
  94.         }
  95.       } catch (ex) {
  96.         console.log(ex);
  97.       }
  98.     }
  99.   }
  100. });
  101.  
  102. client.on("message", message => {
  103.   if (message.author.bot) return;
  104.   if (
  105.     message.content.toLowerCase().startsWith("?closet") &&
  106.     message.channel.name.startsWith("ticket-")
  107.   ) {
  108.     if (message.member.hasPermission("ADMINISTRATOR")) {
  109.       message.channel
  110.         .delete()
  111.         .then(c => console.log(c.name + " was deleted."))
  112.         .catch(err => console.log(err));
  113.       let channelId = message.channel.id;
  114.       Ticket.update({ resolved: true }, { where: { channelId: channelId } });
  115.     }
  116.   }
  117. });
  118. let embedsMsg = [];
  119. client.on("messageReactionAdd", async (reaction, user) => {
  120.   if (user.bot) return;
  121.   if (reaction.emoji.name === "📥") {
  122.     let findTicket = await Ticket.findOne({
  123.       where: { authorId: user.id, resolved: false }
  124.     }).catch(err => console.log(err));
  125.     let limitTicket = await Ticket.findOne({
  126.       where: { authorId: user.id, dailyLimit: { $lt: 3 } }
  127.     });
  128.     if (findTicket) {
  129.       user.send("You already have a Request opened!");
  130.     } else if (!limitTicket) {
  131.       console.log("Create new ticket.");
  132.       try {
  133.         console.log("Creating ticket...");
  134.         let channel = await reaction.message.guild.createChannel("ticket", {
  135.           type: "text",
  136.           permissionOverwrites: [
  137.             {
  138.               allow: "VIEW_CHANNEL",
  139.               id: user.id
  140.             } /*
  141.             {
  142.               allow: "VIEW_CHANNEL",
  143.               id: "625907626303160354"
  144.             },*/,
  145.             {
  146.               deny: "VIEW_CHANNEL",
  147.               id: reaction.message.guild.id
  148.             }
  149.           ]
  150.         });
  151.  
  152.         // Create Embed Message and send it to channel.
  153.         let embed = new discord.RichEmbed();
  154.         embed.setTitle(`Request Ticket`);
  155.         embed.setDescription(
  156.           "React with ✅ to close the ticket.\nReact with ▶️ to add a user to this ticket."
  157.         );
  158.         embed.setColor("#00FF4D");
  159.         embed.setTimestamp();
  160.         embed.setFooter("Requestbot, read the guidelines for the rules.");
  161.         let msg = await channel.send(embed);
  162.         await msg.react("✅");
  163.         await msg.react("▶️");
  164.  
  165.         let newTicket = await Ticket.create({
  166.           authorId: user.id,
  167.           channelId: channel.id,
  168.           guildId: reaction.message.guild.id,
  169.           resolved: false,
  170.           closeMessageId: msg.id
  171.         });
  172.         console.log("Ticket Saved...");
  173.         let ticketId = String(newTicket.dataValues.ticketId).padStart(4, "0");
  174.         await channel.edit({
  175.           name: `${channel.name}-${user.username}-${ticketId}`
  176.         });
  177.         //Log entire channel till ticket is closed
  178.  
  179.         let filter = m => true;
  180.         let collector = new discord.MessageCollector(channel, filter);
  181.         let destination = client.channels.get("671669844449558528"); // Input destination channel id
  182.         channel.send("This channel will be recorded and added to the log");
  183.         collector.on("collect", (m, col) => {
  184.           console.log("collected: " + m.content);
  185.           if (destination) {
  186.             if (m.content.toLowerCase() === "?stopcol") {
  187.               console.log("stopped collecting messages");
  188.               collector.stop();
  189.             }
  190.             let embed = new discord.RichEmbed()
  191.               .setTitle(`${channel.name}`)
  192.               .setDescription(m.content)
  193.               .setTimestamp()
  194.               .setAuthor(m.author.tag, m.author.displayAvatarURL)
  195.               .setColor("#FFBBAA");
  196.             embedsMsg.push(embed);
  197.           }
  198.         });
  199.       } catch (ex) {
  200.         console.log(ex);
  201.       }
  202.     } else {
  203.       user.send("you have reach the daily request limit");
  204.     }
  205.   } else if (reaction.emoji.name === "✅") {
  206.     let embeds = reaction.message.embeds;
  207.     if (embeds.length !== 1) {
  208.       console.log("Incorrect message.");
  209.       return;
  210.     }
  211.     if (embeds[0].title === "Request Ticket") {
  212.       try {
  213.         let tickets = await Ticket.update(
  214.           { resolved: true },
  215.           { where: { closeMessageId: reaction.message.id } }
  216.         );
  217.         let incrementTicket = await Ticket.increment(
  218.           { dailyLimit: +1 },
  219.           { where: { authorId: user.id } }
  220.         );
  221.         console.log(embedsMsg);
  222.         let findTicket = await Ticket.findOne({
  223.           where: { closeMessageId: reaction.message.id }
  224.         });
  225.         console.log("Updated...");
  226.         let channel = reaction.message.channel;
  227.         let updatedChannel = await channel.overwritePermissions(
  228.           findTicket.dataValues.authorId,
  229.           {
  230.             VIEW_CHANNEL: false
  231.           }
  232.         );
  233.         console.log("Updated channel permissions.");
  234.         if (findTicket.dataValues.additionalUsers) {
  235.           let otherUsers = JSON.parse(findTicket.dataValues.additionalUsers);
  236.           otherUsers.forEach(userId => {
  237.             channel
  238.               .overwritePermissions(userId, {
  239.                 VIEW_CHANNEL: false
  240.               })
  241.               .then(v => console.log("Success"))
  242.               .catch(err => console.log(err));
  243.           });
  244.         }
  245.       } catch (ex) {
  246.         console.log(ex);
  247.       }
  248.     }
  249.   } else if (reaction.emoji.name === "▶️") {
  250.     let embeds = reaction.message.embeds;
  251.     if (embeds.length !== 1) {
  252.       console.log("Incorrect message.");
  253.       return;
  254.     }
  255.     if (embeds[0].title === "Request Ticket") {
  256.       console.log("Trying to add user to ticket");
  257.       try {
  258.         let findTicket = await Ticket.findOne({
  259.           where: { closeMessageId: reaction.message.id }
  260.         });
  261.         if (findTicket) {
  262.           let otherUsers;
  263.           if (findTicket.dataValues.additionalUsers) {
  264.             otherUsers = JSON.parse(findTicket.dataValues.additionalUsers);
  265.           } else otherUsers = [];
  266.  
  267.           let channel = reaction.message.channel;
  268.           if (otherUsers.length >= 2) {
  269.             let m = await channel.send(
  270.               "You cannot add any more users to this ticket!"
  271.             );
  272.             await m.delete(5000);
  273.           } else if (otherUsers.length === 0) {
  274.             let filter = m => m.author.id === user.id;
  275.             channel.send(
  276.               "Please enter the ID of the user you wish to add to the ticket."
  277.             );
  278.             let userId = (
  279.               await channel.awaitMessages(filter, { max: 1 })
  280.             ).first().content;
  281.             let findUser = channel.guild.members.find(m => m.id === userId);
  282.             if (!findUser.user.bot) {
  283.               await Ticket.update(
  284.                 { additionalUsers: JSON.stringify([findUser.id]) },
  285.                 { where: { closeMessageId: reaction.message.id } }
  286.               );
  287.               console.log("Added user to ticket.");
  288.               await channel.overwritePermissions(findUser.id, {
  289.                 VIEW_CHANNEL: true
  290.               });
  291.               console.log("Updated permissions for user.");
  292.             }
  293.           } else {
  294.             let filter = m => m.author.id === user.id;
  295.             channel.send(
  296.               "Please enter the ID of the user you wish to add to the ticket."
  297.             );
  298.             let userId = (
  299.               await channel.awaitMessages(filter, { max: 1 })
  300.             ).first().content;
  301.             let findUser = channel.guild.members.find(m => m.id === userId);
  302.             if (findUser) {
  303.               if (otherUsers.find(v => v === findUser.id)) {
  304.                 channel
  305.                   .send("User is already in ticket.")
  306.                   .then(m => m.delete(5000))
  307.                   .catch(err => console.log(err));
  308.               } else {
  309.                 otherUsers.push(findUser.id);
  310.                 console.log(otherUsers);
  311.                 await Ticket.update(
  312.                   { additionalUsers: JSON.stringify(otherUsers) },
  313.                   { where: { closeMessageId: reaction.message.id } }
  314.                 );
  315.                 console.log("Updated.");
  316.                 await channel.overwritePermissions(findUser.id, {
  317.                   VIEW_CHANNEL: true
  318.                 });
  319.                 console.log("Updated permissions for user.");
  320.               }
  321.             } else {
  322.               console.log("User is not in guild or does not exist.");
  323.             }
  324.           }
  325.         }
  326.       } catch (ex) {
  327.         console.log(ex);
  328.       }
  329.     }
  330.   }
  331. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement