Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const botSettings = require("./botSettings.json");
  2.  
  3. const Discord = require("discord.js");
  4.  
  5. const { Client } = require('pg')
  6.  
  7. const postgres = new Client({
  8.  
  9.     user: 'flightline',
  10.  
  11.     host: 'localhost',
  12.  
  13.     database: 'flightlinebotdb',
  14.  
  15.     password: 'bebsi152',
  16.  
  17.     port: 5432
  18.  
  19. })
  20.  
  21.  
  22.  
  23. postgres.connect(err => {
  24.  
  25.     if(err) throw err;  
  26.  
  27.     console.log(`Connecting to database: ${postgres.database}`)
  28.  
  29.     console.log(`Connecting as: ${postgres.user}`)
  30.  
  31.     console.log(`Connecting to ${postgres.host}:${postgres.port}`)
  32.  
  33.     console.log("                        ")
  34.  
  35.     console.log("Connected to Database...")
  36.  
  37. });
  38.  
  39.  
  40.  
  41. const prefix = botSettings.prefix
  42.  
  43.  
  44.  
  45. const bot = new Discord.Client({ disableEveryone: true });
  46.  
  47.  
  48.  
  49. function embedMsgICAO(){
  50.  
  51.     return new Discord.RichEmbed()
  52.  
  53.         .setTimestamp(Date.now())
  54.  
  55.         .setDescription("All Airports in ICAOs in Flightline!")
  56.  
  57.         .addField("Airports:", "-------------------------------------------------")
  58.  
  59.         .addField("JTPH", "Tophon Bridge Intl.")
  60.  
  61.         .addField("JSLL", "Wellingsaul Square Intl.")
  62.  
  63.         .addField("JCO4", "Connerview Airfield")
  64.  
  65.         .setColor("#00FF00");
  66.  
  67. }
  68.  
  69.  
  70.  
  71. function embedMsgChart() {
  72.  
  73.     return new Discord.RichEmbed()
  74.  
  75.         .setTimestamp(Date.now())
  76.  
  77.         .setDescription("This is the map for all airports!")
  78.  
  79.         .addField("Below find the link for the Aerodrome map of all airports!","https://flightlinecharts.webnode.com/charts/")
  80.  
  81.         .setColor("#00FF00");
  82.  
  83. }
  84.  
  85.  
  86.  
  87. function embedMsgWebsite(){
  88.  
  89.     return new Discord.RichEmbed()
  90.  
  91.     .setTimestamp(Date.now())
  92.  
  93.     .setTitle("Website of the Unofficial Flightline Community!")
  94.  
  95.     .addField("Link to website:", "https://flightlinecharts.webnode.com/")
  96.  
  97.     .setColor("#00FF00");
  98.  
  99. }
  100.  
  101.  
  102.  
  103. function embedMsgHelp(){
  104.  
  105.     return new Discord.RichEmbed()
  106.  
  107.     .setTimestamp(Date.now())
  108.  
  109.     .setTitle("These are the available commands for this bot.")
  110.  
  111.     .addField("Commands:", "-------------------------------------------------------------------------------------------")
  112.  
  113.     .addField(`${prefix}ICAOS`, "Displays all the ICAOs for all the airports in Flightline.")
  114.  
  115.     .addField(`${prefix}charts`, "Displays a link of the maps for all the airports. (Only in Flightline!)")
  116.  
  117.     .addField(`${prefix}setatis ,{AirportICAO,Information,Time,Active Runways,Wind,Cloud,Temp,Dew,Altimeter/QNH,Remarks(Optional: If None, say "None".)}`, "Set an ATIS for a chosen Airports. ONLY FOR ATC USE!")
  118.  
  119.     .addField(`${prefix}atis {AirportICAO}`, "Displays the ATIS for the chosen airport.")
  120.  
  121.     .addField(`${prefix}website`, "Shows you the link to our website :D")
  122.  
  123.     .addField(`${prefix}holdqueue`, "Displays the aircraft that are waiting in a holding pattern. (ONLY AVAILABLE DURING ATC USE!")
  124.  
  125.     .addField(`${prefix}holdqueueadd ,{Aircraft Callsign}`, "Adds the Aircraft to the back of the queue. (ATC USE ONLY)")
  126.  
  127.     .addField(`${prefix}holdqueueremove`, "Removes the first aircraft from the queue.")
  128.  
  129.     .setFooter("These commands are subject to change should any occur.")
  130.  
  131.     .setColor("#FFFF66");
  132.  
  133. }
  134.  
  135.  
  136.  
  137. function embedMsgATIS(AirportReq, Information, Time, ActRun, Wind, Cloud, Visibility, Temp, Altimeter, Remarks, DeliveryFreq, GroundFreq, TowerFreq, DirectorFreq, CenterFreq){
  138.  
  139.     return new Discord.RichEmbed()
  140.  
  141.     .setTitle(`ATIS for ${AirportReq}`)
  142.  
  143.     .addField("Information for Airport:", `${AirportReq}`)
  144.  
  145.     .addField("Information:", `${Information}`)
  146.  
  147.     .addField("Time:", `${Time}`)
  148.  
  149.     .addField("Active Runways:", `${ActRun}`)
  150.  
  151.     .addField("Wind:", `${Wind}`)
  152.  
  153.     .addField("Cloud:", `${Cloud}`)
  154.  
  155.     .addField("Visibility:", `${Visibility}`)
  156.  
  157.     .addField("Temperature:", `${Temp}`)
  158.  
  159.     .addField("Altimeter:", `${Altimeter}`)
  160.  
  161.     .addField("Remarks", `${Remarks}`)
  162.  
  163.     .addField("Delivery Frequency:", `${DeliveryFreq}`)
  164.  
  165.     .addField("Ground Frequency:", `${GroundFreq}`)
  166.  
  167.     .addField("Tower Frequency:", `${TowerFreq}`)
  168.  
  169.     .addField("Director Frequency:", `${DirectorFreq}`)
  170.  
  171.     .addField("Center Frequency:", `${CenterFreq}`)
  172.  
  173.     .setFooter("ATIS is constantly updated, beware of sudden weather changes!")
  174.  
  175.     .setColor("#00BFFF");
  176.  
  177. }
  178.  
  179.    
  180.  
  181. let finishedATISJTPH;
  182.  
  183. let finishedATISJSLL;
  184.  
  185. let finishedATISJCO4;
  186.  
  187.  
  188.  
  189. function embedMsgCommandHintSetATIS(){
  190.  
  191.     return new Discord.RichEmbed()
  192.  
  193.     .setDescription("Usage for /setatis:")
  194.  
  195.     .addField("/setatis ,Airport,Information,Time,ActiveRunways,Wind,Cloud,Visibility,Temp,Dew,Altimeter,Remarks", "Make sure that there is NO SPACES BETWEEN LETTER/NUMBERS AND COMMAS(,). The only Spaces that can be used ARE IN BETWEEN WORDS AND SEPERATING /setatis FROM THE AIRPORT. Also note that there is to be a comma right after /setatis: /setatis ,JTPH,Information B...")
  196.  
  197.     .addField("If there is any errors, please contact JusSupra#0962.","--------------------------------------------")
  198.  
  199.     .addField("Reason of error:", "Insufficent information for the ATIS!")
  200.  
  201.     .setTimestamp(Date.now())
  202.  
  203.     .setColor("#FF0000");
  204.  
  205. }
  206.  
  207.  
  208.  
  209. let queueJTPH = [];
  210.  
  211. let queueJSLL = [];
  212.  
  213. let queueJCO4 = [];
  214.  
  215.  
  216.  
  217. function embedMsgQueue(airportQueue){
  218.  
  219.    
  220.  
  221.     if(airportQueue.length == 0)return "There is no planes in a holding pattern!"
  222.  
  223.    
  224.  
  225.     let embed = new Discord.RichEmbed()
  226.  
  227.    
  228.  
  229.     airportQueue.forEach(function(item, index) {
  230.  
  231.         embed.addField(`Number ${index + 1} for landing:`, item);
  232.  
  233.    
  234.  
  235.     embed.setDescription("All the planes that are in a holding pattern.")
  236.  
  237.     embed.setColor("#FFF700")
  238.  
  239.     embed.setTimestamp(Date.now());
  240.  
  241.    
  242.  
  243.    
  244.  
  245.     });
  246.  
  247.     return embed
  248.  
  249. }
  250.  
  251.  
  252.  
  253.  
  254.  
  255. bot.on("ready", async () => {
  256.  
  257.     try {
  258.  
  259.         var link = await bot.generateInvite(["ADMINISTRATOR"]);
  260.  
  261.     } catch (e) {
  262.  
  263.         console.log(e.stack)
  264.  
  265.     }
  266.  
  267.  
  268.  
  269.     console.log("--------------------------------------------------------------------------------------------");
  270.  
  271.     console.log(`Bot is ready! Logged in as: ${bot.user.username}`);
  272.  
  273.     console.log(`Logged in on: ${botSettings.token}`);
  274.  
  275.     console.log(`Time Logged on: ${new(Date)}`);
  276.  
  277.     console.log(`The prefix is: ${prefix}`);
  278.  
  279.     console.log("Below find the invite link should it be needed for the bot to be invited to another server:");
  280.  
  281.     console.log(link);
  282.  
  283.     console.log("--------------------------------------------------------------------------------------------");
  284.  
  285.  
  286.  
  287. });
  288.  
  289.    
  290.  
  291.  
  292.  
  293.  
  294.  
  295. bot.on("message", async message => {
  296.  
  297.     if (message.author.bot) return;
  298.  
  299.  
  300.  
  301.     let messageArray = message.content.split(" ");
  302.  
  303.     let command = messageArray[0];
  304.  
  305.     let args = messageArray.slice(1);
  306.  
  307.  
  308.  
  309.     if(!command.startsWith(prefix)) return;
  310.  
  311.  
  312.  
  313.     if(command === `${prefix}help`){
  314.  
  315.         if(message.channel.type === "dm") return message.channel.send("This command only works in the server chats!")
  316.  
  317.         message.channel.send(embedMsgHelp());
  318.  
  319.     }
  320.  
  321.  
  322.  
  323.     if(command === `${prefix}website`){
  324.  
  325.         if(message.channel.type === "dm") return message.channel.send("This command only works in the server chats!")
  326.  
  327.         message.channel.send(embedMsgWebsite());
  328.  
  329.     }
  330.  
  331.  
  332.  
  333.     if(command === `${prefix}ICAOS`){
  334.  
  335.         if(message.channel.type === "dm") return message.channel.send("This command only works in the server chats!")
  336.  
  337.         if(args[0] === "JTPH" || "JSLL" || "JCO4") return message.channel.send(embedMsgICAO());
  338.  
  339.         message.channel.send(embedMsgICAO());
  340.  
  341.     }
  342.  
  343.  
  344.  
  345.     if(command === `${prefix}charts`){
  346.  
  347.         if(message.channel.type === "dm") return message.channel.send("This command only works in the server chats!")
  348.  
  349.         message.channel.send(embedMsgChart());
  350.  
  351.     }
  352.  
  353.    
  354.  
  355.     if(command === `${prefix}setatis`){
  356.  
  357.        
  358.  
  359.         if(message.channel.type === "dm") return message.channel.send("This command only works in the server chats!")
  360.  
  361.  
  362.  
  363.         let guildID = "593830690777333770";
  364.  
  365.         let guild = bot.guilds.get(guildID);
  366.  
  367.         let member = message.guild.member(message.author);
  368.  
  369.         let role = member.guild.roles.find(r => r.name === "----------------- ATC Staff -----------------");
  370.  
  371.        
  372.  
  373.         if(!member.roles.has(role.id)) return message.channel.send("Only ATC's are allowed to set ATIS'es");
  374.  
  375.  
  376.  
  377.             let messageArray = message.content.split(",");
  378.  
  379.             let args = messageArray.slice(0);
  380.  
  381.             let firArg = args[0].split(" ");
  382.  
  383.             let firstArg = firArg[1]
  384.  
  385.  
  386.  
  387.        
  388.  
  389.             let AirportReqIn = firstArg;
  390.  
  391.             let InformationIn = args[1];
  392.  
  393.             let TimeIn = args[2];
  394.  
  395.             let ActRunIn = args[3];
  396.  
  397.             let WindIn = args[4];
  398.  
  399.             let CloudIn = args[5];
  400.  
  401.             let VisibilityIn = args[6];
  402.  
  403.             let TempIn = args[7];
  404.  
  405.             let AltimeterIn = args[8];
  406.  
  407.             let RemarksIn = args[9];
  408.  
  409.  
  410.  
  411.             definedATISvarJTPH = {
  412.  
  413.                 AirportReq: AirportReqIn,
  414.  
  415.                 Information: InformationIn,
  416.  
  417.                 Time: TimeIn,
  418.  
  419.                 ActRun: ActRunIn,
  420.  
  421.                 Wind: WindIn,
  422.  
  423.                 Cloud: CloudIn,
  424.  
  425.                 Visibility: VisibilityIn,
  426.  
  427.                 Temp: TempIn,
  428.  
  429.                 Altimeter: AltimeterIn,
  430.  
  431.                 Remarks: RemarksIn,
  432.  
  433.             }
  434.  
  435.            
  436.  
  437.             console.log(definedATISvarJTPH)
  438.  
  439.            
  440.  
  441.             if(args.length < 10) return message.channel.send(embedMsgCommandHintSetATIS());
  442.  
  443.             message.channel.send("Arguments passed!")
  444.  
  445.  
  446.  
  447.             if(AirportReqIn === "JTPH"){
  448.  
  449.  
  450.  
  451.                 let DeliveryFreq = "118.400MHz";
  452.  
  453.                 let GroundFreq = "120.750MHz";
  454.  
  455.                 let TowerFreq = "124.225MHz";
  456.  
  457.                 let DirectorFreq = "126.825MHz";
  458.  
  459.                 let CenterFreq = "132.600MHz";
  460.  
  461.  
  462.  
  463.                 finishedATISJTPH =  embedMsgATIS(definedATISvarJTPH.AirportReq, definedATISvarJTPH.Information, definedATISvarJTPH.Time, definedATISvarJTPH.ActRun, definedATISvarJTPH.Wind, definedATISvarJTPH.Cloud, definedATISvarJTPH.Visibility, definedATISvarJTPH.Temp, definedATISvarJTPH.Altimeter, definedATISvarJTPH.Remarks, DeliveryFreq, GroundFreq, TowerFreq, DirectorFreq, CenterFreq);  
  464.  
  465.                 message.channel.sendEmbed(finishedATISJTPH);
  466.  
  467.                
  468.  
  469.             }
  470.  
  471.            
  472.  
  473.             else if(AirportReqIn === "JSLL"){
  474.  
  475.  
  476.  
  477.                 let DeliveryFreq = "122.225MHz";
  478.  
  479.                 let GroundFreq = "119.600MHz";
  480.  
  481.                 let TowerFreq = "123.775MHz";
  482.  
  483.                 let DirectorFreq = "130.500MHz";
  484.  
  485.                 let CenterFreq = "131.775MHz";
  486.  
  487.  
  488.  
  489.                 finishedATISJSLL =  embedMsgATIS(definedATISvarJTPH.AirportReq, definedATISvarJTPH.Information, definedATISvarJTPH.Time, definedATISvarJTPH.ActRun, definedATISvarJTPH.Wind, definedATISvarJTPH.Cloud, definedATISvarJTPH.Visibility, definedATISvarJTPH.Temp, definedATISvarJTPH.Altimeter, definedATISvarJTPH.Remarks, DeliveryFreq, GroundFreq, TowerFreq, DirectorFreq, CenterFreq);
  490.  
  491.                 message.channel.send(finishedATISJSLL);
  492.  
  493.             }
  494.  
  495.  
  496.  
  497.             else if(AirportReqIn === "JCO4"){
  498.  
  499.  
  500.  
  501.                 let DeliveryFreq = "None";
  502.  
  503.                 let GroundFreq = "None";
  504.  
  505.                 let TowerFreq = "124.225MHz (Controlled by JTPH Tower!)";
  506.  
  507.                 let DirectorFreq = "None";
  508.  
  509.                 let CenterFreq = "None";
  510.  
  511.  
  512.  
  513.                 finishedATISJCO4 =  embedMsgATIS(definedATISvarJTPH.AirportReq, definedATISvarJTPH.Information, definedATISvarJTPH.Time, definedATISvarJTPH.ActRun, definedATISvarJTPH.Wind, definedATISvarJTPH.Cloud, definedATISvarJTPH.Visibility, definedATISvarJTPH.Temp, definedATISvarJTPH.Altimeter, definedATISvarJTPH.Remarks, DeliveryFreq, GroundFreq, TowerFreq, DirectorFreq, CenterFreq);
  514.  
  515.                 message.channel.send(finishedATISJCO4);
  516.  
  517.             }
  518.  
  519.        
  520.  
  521.     }
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.     if(command === `${prefix}atis` || `${prefix}ATIS`){
  530.  
  531.         if(message.channel.type === "dm") return message.channel.send("This command only works in the server chats!")
  532.  
  533.  
  534.  
  535.         let AirportChosen = args[0];
  536.  
  537.        
  538.  
  539.         if (AirportChosen === "JTPH") {
  540.  
  541.             message.channel.sendEmbed(finishedATISJTPH);
  542.  
  543.         }
  544.  
  545.  
  546.  
  547.         if (AirportChosen === "JSLL") {
  548.  
  549.             message.channel.send(finishedATISJSLL);
  550.  
  551.         }
  552.  
  553.  
  554.  
  555.         if (AirportChosen === "JCO4") {
  556.  
  557.             message.channel.send(finishedATISJCO4);
  558.  
  559.         }
  560.  
  561.     }
  562.  
  563.  
  564.  
  565.     if(command === `${prefix}holdqueueadd`){
  566.  
  567.  
  568.  
  569.         if(message.channel.type === "dm") return message.channel.send("This command only works in the server chats!")
  570.  
  571.  
  572.  
  573.         let guildID = "593830690777333770";
  574.  
  575.         let guild = bot.guilds.get(guildID);
  576.  
  577.         let member = message.guild.member(message.author);
  578.  
  579.         let role = member.guild.roles.find(r => r.name === "----------------- ATC Staff -----------------");
  580.  
  581.        
  582.  
  583.         if(!member.roles.has(role.id)) return message.channel.send("Only ATC's are allowed to add to Holding Pattern Queue!");
  584.  
  585.         if(!args[0]) return message.channel.send("No airport specified!")
  586.  
  587.         if(!args[1]) return message.channel.send("No aircraft specified!")
  588.  
  589.  
  590.  
  591.         let messageArray = message.content.split(",");
  592.  
  593.         let args = messageArray.slice(0);
  594.  
  595.        
  596.  
  597.         if(args == '') return message.channel.send("No Aircraft provided to hold or no comma after the space of the command and it's arguments!")
  598.  
  599.         console.log(args)
  600.  
  601.  
  602.  
  603.         let Aircraft = args[1];
  604.  
  605.        
  606.  
  607.         if(args[0] === "JTPH"){
  608.  
  609.             queueJTPH.push(Aircraft);
  610.  
  611.             console.log(queueJTPH);
  612.  
  613.             message.channel.send(embedMsgQueue(queueJTPH));
  614.  
  615.         }
  616.  
  617.  
  618.  
  619.         if(args[0] === "JSLL"){
  620.  
  621.             queueJSLL.push(Aircraft);
  622.  
  623.             console.log(queueJSLL);
  624.  
  625.             message.channel.send(embedMsgQueue(queueJSLL));
  626.  
  627.         }
  628.  
  629.  
  630.  
  631.         if(args[0] === "JCO4"){
  632.  
  633.             queueJCO4.push(Aircraft);
  634.  
  635.             console.log(queueJCO4);
  636.  
  637.             message.channel.send(embedMsgQueue(queueJCO4));
  638.  
  639.         }
  640.  
  641.  
  642.  
  643.     }
  644.  
  645.  
  646.  
  647.     if(command === `${prefix}holdqueueremove`){
  648.  
  649.  
  650.  
  651.         if(message.channel.type === "dm") return message.channel.send("This command only works in the server chats!")
  652.  
  653.         if(!args[0]) return message.channel.send("No airport specified!")
  654.  
  655.  
  656.  
  657.         let guildID = "593830690777333770";
  658.  
  659.         let guild = bot.guilds.get(guildID);
  660.  
  661.         let member = message.guild.member(message.author);
  662.  
  663.         let role = member.guild.roles.find(r => r.name === "----------------- ATC Staff -----------------");
  664.  
  665.        
  666.  
  667.         if(!member.roles.has(role.id)) return message.channel.send("Only ATC's are allowed to remove from the Holding Pattern Queue.");
  668.  
  669.        
  670.  
  671.         if(args[0] === "JTPH"){
  672.  
  673.             queueJTPH.shift();
  674.  
  675.             console.log(queueJTPH);
  676.  
  677.             if(queueJTPH.length === 0) return message.channel.send("No planes are in a holding pattern at JTPH!");
  678.  
  679.             message.channel.send(embedMsgQueue(queueJTPH));          
  680.  
  681.         }
  682.  
  683.  
  684.  
  685.         if(args[0] === "JSLL"){
  686.  
  687.             queueJSLL.shift();
  688.  
  689.             console.log(queueJSLL);
  690.  
  691.             if(queueJSLL.length === 0) return message.channel.send("No planes are in a holding pattern at JSLL!");
  692.  
  693.             message.channel.send(embedMsgQueue(queueJSLL));          
  694.  
  695.         }
  696.  
  697.  
  698.  
  699.         if(args[0] === "JCO4"){
  700.  
  701.             queueJTPH.shift();
  702.  
  703.             console.log(queueJCO4);
  704.  
  705.             if(queueJCO4.length === 0) return message.channel.send("No planes are in a holding pattern at JCO4!");
  706.  
  707.             message.channel.send(embedMsgQueue(queueJCO4));          
  708.  
  709.         }
  710.  
  711.     }
  712.  
  713.  
  714.  
  715.     if(command === `${prefix}holdqueue`){
  716.  
  717.  
  718.  
  719.         if(message.channel.type === "dm") return message.channel.send("This command only works in the server chats!")
  720.  
  721.         if(!args[0]) return message.channel.send("No airport specified!")
  722.  
  723.  
  724.  
  725.         console.log(args)
  726.  
  727.         console.log(embedMsgQueue(queueJSLL))
  728.  
  729.  
  730.  
  731.         if(args[0] === "JTPH"){
  732.  
  733.             if(queueJTPH.length === 0) return message.channel.send("No planes are in a holding pattern at JTPH!");
  734.  
  735.             message.channel.send(embedMsgQueue(queueJTPH));
  736.  
  737.         }
  738.  
  739.  
  740.  
  741.         if(args[0] === "JSLL"){
  742.  
  743.             if(queueJSLL.length === 0) return message.channel.send("No planes are in a holding pattern at JSLL!");
  744.  
  745.             console.log(embedMsgQueue(queueJSLL))
  746.  
  747.             message.channel.send(embedMsgQueue(queueJSLL))
  748.  
  749.         }
  750.  
  751.  
  752.  
  753.         if(args[0] === "JCO4"){
  754.  
  755.             if(queueJCO4.length === 0) return message.channel.send("No planes are in a holding pattern at JCO4!");
  756.  
  757.             message.channel.send(embedMsgQueue(queueJCO4));
  758.  
  759.         }
  760.  
  761.     }
  762.  
  763.  
  764.  
  765.     if(command === `${prefix}login`){
  766.  
  767.  
  768.  
  769.         let guildID = "593830690777333770";
  770.  
  771.         let guild = bot.guilds.get(guildID);
  772.  
  773.         let member = message.guild.member(message.author);
  774.  
  775.         let role = member.guild.roles.find(r => r.name === "----------------- ATC Staff -----------------");
  776.  
  777.  
  778.  
  779.         if(!member.roles.has(role.id)) return message.channel.send("Only ATC's are allowed to login as ATC!");
  780.  
  781.  
  782.  
  783.         let mAuthor = message.author
  784.  
  785.         let mUsername = args[0]
  786.  
  787.         let mAirportServed = args[1]
  788.  
  789.         let mPositionServed = args[2]
  790.  
  791.         let mTimeStart = new Date().toISOString();
  792.  
  793.  
  794.  
  795.         message.author.loginDetails = {
  796.  
  797.             author: mAuthor,
  798.  
  799.             username: mUsername,
  800.  
  801.             AirportServed:  mAirportServed,
  802.  
  803.             PositionServed: mPositionServed,          
  804.  
  805.             TimeStart: mTimeStart,
  806.  
  807.         }
  808.  
  809.        
  810.  
  811.         if(!args[0]) return message.channel.send("No username provided!");
  812.  
  813.         if(!args[1]) return message.channel.send("No airport provided!");
  814.  
  815.         if(!args[2]) return message.channel.send("No position provided!");
  816.  
  817.        
  818.  
  819.         postgres.query(`SELECT * FROM login_details WHERE username = '${mUsername}'`, async (err, res) => {
  820.  
  821.             if(err) return message.channel.send("Error connecting to the Database. Contact JusSupra#0962 for help if this error continues to arise.")
  822.  
  823.             if(res.rows.length === 0) return message.channel.send(`There is no entry in the database with the username: ${mUsername}`)
  824.  
  825.  
  826.  
  827.             message.channel.send("Go to DM's to enter your password!")
  828.  
  829.            
  830.  
  831.             const filter = m => m.content === `${res.rows[0].password}`
  832.  
  833.             const channel = await message.author.send(`If there is no response from the bot after you have entered the password, it means the password is incorrect! \nEnter password for ${mUsername}:`)
  834.  
  835.             const collector = channel.channel.createMessageCollector(filter, { max: 1 });
  836.  
  837.  
  838.  
  839.             collector.on('collect', m => {
  840.  
  841.             });
  842.  
  843.  
  844.  
  845.             collector.on('end', collected => {
  846.  
  847.                 if(collected.get(channel.channel.lastMessageID).content === `${res.rows[0].password}`){
  848.  
  849.                     message.author.send(`Sucessfully logged in as ${mUsername}!`)
  850.  
  851.                    
  852.  
  853.                     message.member.orgNick = {
  854.  
  855.                         originNick: message.member.nickname,
  856.  
  857.                     }
  858.  
  859.                    
  860.  
  861.                     member.setNickname(`${mAirportServed} ${mPositionServed} - ${message.author.username}`)
  862.  
  863.                     postgres.query(`INSERT INTO login_logs(staff_user_id, username, airport_served, position_served, time_start, status) VALUES (${message.author.id}, '${mUsername}', '${mAirportServed}', '${mPositionServed}', '${mTimeStart}', 'ON_DUTY');`, (e, r) => {
  864.  
  865.                         console.log(e)
  866.  
  867.                             if(e) return message.author.send("ERROR: Error while entering data into Database. If this continues, contact JusSupra#0962.")      
  868.  
  869.                     })
  870.  
  871.                 }  
  872.  
  873.             });
  874.  
  875.         })
  876.  
  877.     }
  878.  
  879.  
  880.  
  881.     if(command === `${prefix}logout`){
  882.  
  883.        
  884.  
  885.         let role = message.member.guild.roles.find(r => r.name === "----------------- ATC Staff -----------------");
  886.  
  887.        
  888.  
  889.         if(!message.member.roles.has(role.id)) return message.channel.send("Only ATC's are allowed to login and out as ATC!");
  890.  
  891.  
  892.  
  893.         let TimeEnd = new Date().toISOString()        
  894.  
  895.         postgres.query(`UPDATE login_logs SET status = 'FIN_DUTY', time_end = '${TimeEnd}' WHERE username = '${message.author.loginDetails.username}' AND time_start = '${message.author.loginDetails.TimeStart}'`, (err, res) => {
  896.  
  897.             message.channel.send("Logged out sucessfully!")
  898.  
  899.             let member = message.guild.fetchMember(message.author).then(message.member.setNickname(`${message.member.orgNick.originNick}`)).catch(e => console.log(e))
  900.  
  901.         });
  902.  
  903.     }
  904.  
  905.  
  906.  
  907.     if(command === `${prefix}register`){
  908.  
  909.  
  910.  
  911.         let username = args[0]
  912.  
  913.         let password = args[1]
  914.  
  915.  
  916.  
  917.         if(!args[0]) return message.channel.send("You did not provide a username!")
  918.  
  919.         if(!args[1]) return message.channel.send("You did not set a password!")
  920.  
  921.        
  922.  
  923.         const filter = m => m.content === "thirst152"
  924.  
  925.         const channel = await message.author.send(`If there is no response from the bot after you have entered the password, it means the password is incorrect! \nEnter main password:`)
  926.  
  927.         const collector = channel.channel.createMessageCollector(filter, { max: 1 });
  928.  
  929.  
  930.  
  931.         collector.on('collect', m => {
  932.  
  933.         });
  934.  
  935.  
  936.  
  937.         collector.on('end', collected => {
  938.  
  939.             if(collected.get(channel.channel.lastMessageID).content === "thirst152"){
  940.  
  941.                 message.author.send(`Sucessfully authorised!`)        
  942.  
  943.                 postgres.query(`INSERT INTO login_details (username, password) VALUES ('${username}','${password}');`, (err, res) => {
  944.  
  945.                     if(err) return message.channel.send("ERROR: An error occured while entering your details into the database. If this continues, contact JusSupra#0962.")
  946.  
  947.                     message.channel.send("Sucessfully added your details into the database. Make sure you save them somewhere safe and don't give these details to anyone!")
  948.  
  949.                 });
  950.  
  951.             }
  952.  
  953.         });
  954.  
  955.        
  956.  
  957.  
  958.  
  959.     }
  960.  
  961.  
  962.  
  963.             // SELECT * FROM login_details EXCEPT SELECT * FROM login_details WHERE username = 'supraaxdd';
  964.  
  965.  
  966.  
  967.     if(command === `${prefix}psql`){
  968.  
  969.  
  970.  
  971.         let role = message.member.guild.roles.find(r => r.name === "Postgres Access");        
  972.  
  973.         if(!message.member.roles.has(role.id)) return message.channel.send("Only people with the 'Postgres Access' role are allowed to query PostgreSQL!");
  974.  
  975.  
  976.  
  977.         let query = args.slice(0).join(" ");
  978.  
  979.  
  980.  
  981.         postgres.query(query, (err, res) => {
  982.  
  983.             if(err){
  984.  
  985.                 return console.log(err)
  986.  
  987.             }
  988.  
  989.  
  990.  
  991.  
  992.  
  993.        
  994.  
  995.             for (i = 0; i < res.rows.length; i++){
  996.  
  997.  
  998.  
  999.                     message.channel.send("Results of query:")
  1000.  
  1001.                     let columns = Object.entries(res.rows[i])
  1002.  
  1003.                  
  1004.  
  1005.                     for(j = 0; j < columns.length; j++){
  1006.  
  1007.                         let cRow = columns[j].toString().split(",")
  1008.  
  1009.                         message.channel.send(`***${cRow[0]}***\n${cRow[1]}\n\n`)
  1010.  
  1011.                     }
  1012.  
  1013.                    
  1014.  
  1015.                     message.channel.send("---------------------------------------")
  1016.  
  1017.             }
  1018.  
  1019.    
  1020.  
  1021.         });
  1022.  
  1023.  
  1024.  
  1025.     }
  1026.  
  1027. });
  1028.  
  1029.  
  1030.  
  1031. bot.login(botSettings.token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement