Advertisement
Guest User

Untitled

a guest
Jan 12th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const commando = require('discord.js-commando');
  2. const config = require('../../config.json');
  3. const mysql = require('mysql');
  4. const jimp = require('jimp');
  5. const { Client, MessageAttachment } = require('discord.js');
  6. const fs = require('fs');
  7.  
  8. // Connection to Database
  9. let dbpass = config.dbpassword;
  10.  
  11. var db = mysql.createPool({
  12.   host: 'localhost',
  13.   user:'serien',
  14.   password: dbpass,
  15.   database: 'ami'
  16. });
  17.  
  18. /*db.connect((err) => {
  19.   if(err) throw err;
  20. });
  21. */
  22.  
  23. db.getConnection(function(err, connection) {
  24.     if (err) throw err;
  25.     connection.release();
  26. })
  27.  
  28.  
  29. class SetupCommand extends commando.Command
  30. {
  31.    
  32.  
  33.     constructor(client)
  34.     {
  35.        
  36.  
  37.         super(client, {
  38.             name: 'setup',
  39.             group: 'social',
  40.             memberName: 'setup',
  41.             description: 'Changes the appearance of your profile card.',
  42.             examples: [ 'setup' ],
  43.         });
  44.     }
  45.  
  46.    
  47.  
  48.     async run(message, {member})
  49.     {
  50.       const filter = m => m.author.id === message.author.id;
  51.  
  52.       // Profile Setup Menu
  53.       message.channel.startTyping();
  54.       message.channel.send('🛠 | Profile Setup Menu\n```Select one of the following options:\n\n[1]    Background Image\n[2]    XP Bar Color\n[3]    Bio\n\nType a number to continue, or type \'exit\' to leave this menu.\n```').then(q => q.delete(3e4))
  55.       message.channel.stopTyping();
  56.       message.channel.awaitMessages(filter, {
  57.         max: 1,
  58.         time: 3e4
  59.       }).then(collected => {
  60.         collected.delete(3e4);
  61.  
  62.         // Code for main menu collection
  63.         if (collected.first().content === 'exit')
  64.         {
  65.           return message.reply('cancelled profile setup.').then(q => q.delete(1e4));
  66.         }
  67.         else if (collected.first().content === '1')
  68.         {
  69.           let names = ['Default', 'Snow Miku', 'Blue Low-Poly', 'Etsuji', 'Howl\'s Moving Castle', 'Blue Jellyfish', 'Distant Tower', 'Dawn City', 'Eyes', 'Ribbit Boys', 'Owl Forest', 'Domino\'s Miku', 'Polar Bear & Seal', 'Sky Falling', 'Twilight Lake'];
  70.           let images = ['default.png', 'snowmiku.png', 'bluepoly.png', 'etsuji.png', 'castle.png', 'jelly.png', 'clifftower.png', 'dawncity.png', 'eyes.png', 'frogs.png', 'owl.png', 'pizzamiku.png', 'polarbears.png', 'skygirl.png', 'skylake.png'];
  71.  
  72.           var setup = '🖼 | Background Image Gallery\n```Select a background image!\n\n';
  73.           for (var i = 0; i < names.length; i++)
  74.           {
  75.             var number = i;
  76.             number += 1;
  77.             setup += '[' + number + ']' + '    ' + names[i] + '\n';
  78.           }
  79.           setup += '```';
  80.  
  81.           message.channel.startTyping();
  82.           message.channel.send(setup).then(q => q.delete(3e4))
  83.           message.channel.stopTyping();
  84.           message.channel.awaitMessages(filter, {
  85.             max: 1,
  86.             time: 3e4
  87.           }).then(collected => {
  88.             collected.delete(3e4);
  89.  
  90.             // Code for Background selection collection
  91.            
  92.  
  93.             if (collected.first().content === 'exit')
  94.             {
  95.               return message.reply('cancelled background setup.').then(q => q.delete(1e4));
  96.             }
  97.             else if (parseInt(collected.first().content) > 0 && parseInt(collected.first().content) <= images.length)
  98.             {
  99.               var choice = parseInt(collected.first().content) - 1;
  100.               var name = names[choice];
  101.               var selection = images[choice];
  102.               var bgpath = './bg_images/' + selection;
  103.  
  104.               // Connecting to database
  105.               let dbpass = config.dbpassword;
  106.  
  107.               var db = mysql.createPool({
  108.                 host: 'localhost',
  109.                 user:'serien',
  110.                 password: dbpass,
  111.                 database: 'ami'
  112.               });
  113.  
  114.               db.getConnection(function(err, connection) {
  115.                   if (err) throw err;
  116.  
  117.                   connection.query(`SELECT color FROM xp WHERE id = '${message.author.id}'`, (err, rows) => {
  118.                     if (err) {
  119.                         connection.end();
  120.                         console.error(err);
  121.                         return;
  122.                     }
  123.                    
  124.                     if (rows.length < 1)
  125.                     {
  126.                       return message.reply('you do not have a profile set up yet! Please try again.').then(q => q.delete(1e4));
  127.                     }
  128.                     message.channel.startTyping();
  129.                     let xppath = './bg_images/' + rows[0].color;  
  130.                     let avatar = message.author.avatarURL;
  131.                     let overlay = './bg_images/overlay.png'
  132.                     let xpbarcrop_x = 166 + 268;   
  133.                     let mime = jimp.MIME_PNG;
  134.  
  135.                     let modelica_reg12 = './fonts/modelica_reg12/modelica_reg12.fnt';
  136.                     let modelica_reg10 = './fonts/opensans_12/opensans_12.fnt';
  137.                     let modelica_reg18 = './fonts/modelica_extrabold18/opensans_bold20.fnt';
  138.                     let modelica_bold30 = './fonts/modelica_bold30/modelica_bold30.fnt';
  139.                     let modelica_extrabold18 = './fonts/modelica_extrabold18/opensans_bold20.fnt';
  140.                  
  141.                     let getProfile = function() {
  142.                       var promise = new Promise(function(resolve, reject) {
  143.                         jimp.read(bgpath).then(function(background) {
  144.                           jimp.read(overlay).then(function(overlay1) {
  145.                             jimp.read(avatar).then(function(avatar) {
  146.                               jimp.read(xppath).then(function(xpbar1) {
  147.                                 jimp.loadFont(modelica_reg12).then(function(fontreg12) {
  148.                                   jimp.loadFont(modelica_bold30).then(function(fontbold30) {
  149.                                     jimp.loadFont(modelica_extrabold18).then(function(fontextrabold18) {
  150.                                       jimp.loadFont(modelica_reg10).then(function(fontreg10) {
  151.                                         jimp.loadFont(modelica_reg18).then(function(fontreg18) {
  152.                                           background.composite(overlay1, 0, 0);
  153.                                           background.composite(avatar.resize(94, 94), 23, 67);
  154.                                           background.composite(xpbar1.crop(0, 0, xpbarcrop_x, 250), 0, 0);
  155.                                           background.print(
  156.                                             fontextrabold18,
  157.                                             168,
  158.                                             115,
  159.                                             {
  160.                                               text: 'USERNAME',
  161.                                               alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  162.                                             },
  163.                                             254,
  164.                                             30
  165.                                             );
  166.                                             background.print(
  167.                                                 fontreg10,
  168.                                                 168,
  169.                                                 157,
  170.                                                 {
  171.                                                     text: 'XP: ' + '9999999' + ' / ' + '9999999',
  172.                                                     alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  173.                                                 },
  174.                                                 254,
  175.                                                 30
  176.                                             );
  177.                                             background.print(
  178.                                                 fontreg10,
  179.                                                 168,
  180.                                                 135,
  181.                                                 {
  182.                                                     text: '"..."',
  183.                                                     alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  184.                                                 },
  185.                                                 254,
  186.                                                 30
  187.                                             );
  188.                                             background.print(fontreg12, 43, 169, 'LEVEL');
  189.                                             background.print(
  190.                                                 fontbold30,
  191.                                                 21,
  192.                                                 193,
  193.                                                 {
  194.                                                     text: '99',
  195.                                                     alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  196.                                                 },
  197.                                                 94,
  198.                                                 90
  199.                                             );
  200.                                             background.print(
  201.                                                 fontreg10,
  202.                                                 230,
  203.                                                 215,
  204.                                                 {
  205.                                                     text: 'Ranking:\t#99',
  206.                                                     alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  207.                                                 },
  208.                                                 120,
  209.                                                   30
  210.                                                 );
  211.                                             background.print(
  212.                                                 fontreg10,
  213.                                                 230,
  214.                                                 198,
  215.                                                 {
  216.                                                   text: 'Credits:\t9999999',
  217.                                                   alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  218.                                                 },
  219.                                                 120,
  220.                                                 30
  221.                                                 );
  222.                                             background.print(
  223.                                                 fontreg10,
  224.                                                 230,
  225.                                                 181,
  226.                                                 {
  227.                                                     text: 'Total XP:\t9999999',
  228.                                                     alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  229.                                                 },
  230.                                                 120,
  231.                                                   30
  232.                                                 );
  233.                                             let result = background.getBufferAsync(mime);
  234.                                             resolve(result);    
  235.                                         });
  236.                                     });
  237.                                 });
  238.                             });
  239.                         });
  240.                     });
  241.                 });
  242.               });
  243.               });
  244.               });
  245.               return promise;
  246.               };
  247.               message.channel.send('🖼 | ' + name + ' Preview:')
  248.               getProfile().then(function(result) {
  249.                 /*const buff = fs.readFileSync('./memes.txt');
  250.                 console.log(buff);
  251.                 const attach = new MessageAttachment(buff, 'memes.txt');
  252.                 message.channel.send(`${message.author}`, attach);*/
  253.                
  254.                 var buffer = result;
  255.                 let msg = message.channel.send({
  256.                 files: [{
  257.                 attachment: buffer,
  258.                 name: 'profile.png'
  259.                 }]
  260.                 }).then(q => q.delete(3e4))
  261.                 .catch(console.error);
  262.                 await msg.channel.send('Would you like to update your background image to: ' + name + '?\nPlease enter \'yes\' to confirm or \'no\' to cancel.').then(q => q.delete(3e4))
  263.                 });
  264.              
  265.               message.channel.awaitMessages(filter, {
  266.                 max: 1,
  267.                 time: 3e4
  268.               }).then(collected => {
  269.                 collected.delete(3e4);
  270.    
  271.                 if (collected.first().content.toLowerCase() === 'no')
  272.                 {
  273.                   return message.reply('cancelled background setup.').then(q => q.delete(1e4));
  274.                 }
  275.                 else if (collected.first().content.toLowerCase() === 'yes')
  276.                 {
  277.                     console.log(selection);
  278.                   connection.query(`UPDATE xp SET bg = '${selection}' WHERE id = '${message.author.id}'`);
  279.                   return message.reply('background image updated!').then(q => q.delete(1e4));
  280.                 }
  281.                 else
  282.                 {
  283.                   return message.reply('that is an invalid option. Please try again.').then(q => q.delete(1e4));
  284.                 }
  285.               }).catch(err => {
  286.                 message.reply('profile setup cancelled due to inactivity').then(q => q.delete(1e4));
  287.               })  
  288.               message.channel.stopTyping();
  289.               });
  290.               connection.release();
  291.               });
  292.             }
  293.             else
  294.             {
  295.               return message.reply('that image does not exist! Please try again.').then(q => q.delete())
  296.             }
  297.           }).catch(err => {
  298.             message.reply('profile setup cancelled due to inactivity').then(q => q.delete(1e4));
  299.           })
  300.          
  301.         }
  302.         else if (collected.first().content === '2')
  303.         {
  304.           // Code for changing XP bar color
  305.           let names = ['Rose Pink', 'Periwinkle Blue', 'Valiant Lavender'];
  306.           let images = ['xpbar.png', 'xpblue.png', 'xppurple.png'];
  307.  
  308.           var setup = '🎨 | XP Bar Color Gallery\n```Select a color!\n\n';
  309.           for (var i = 0; i < names.length; i++)
  310.           {
  311.             var number = i;
  312.             number += 1;
  313.             setup += '[' + number + ']' + '    ' + names[i] + '\n';
  314.           }
  315.           setup += '```';
  316.  
  317.           message.channel.startTyping();
  318.           message.channel.send(setup).then(q => q.delete(3e4))
  319.           message.channel.stopTyping();
  320.           message.channel.awaitMessages(filter, {
  321.             max: 1,
  322.             time: 3e4
  323.           }).then(collected => {
  324.             collected.delete(3e4);
  325.  
  326.             // Code for XP bar color selection collection
  327.            
  328.  
  329.             if (collected.first().content === 'exit')
  330.             {
  331.               return message.reply('cancelled XP bar color setup.').then(q => q.delete(1e4));
  332.             }
  333.             else if (parseInt(collected.first().content) > 0 && parseInt(collected.first().content) <= images.length)
  334.             {
  335.               var choice = parseInt(collected.first().content) - 1;
  336.               var name = names[choice];
  337.               var selection = images[choice];
  338.               let xppath = './bg_images/' + selection;
  339.  
  340.               // Connecting to database
  341.               let dbpass = config.dbpassword;
  342.  
  343.               var db = mysql.createPool({
  344.                 host: 'localhost',
  345.                 user:'serien',
  346.                 password: dbpass,
  347.                 database: 'ami'
  348.               });
  349.  
  350.               db.getConnection(function(err, connection) {
  351.                   if (err) throw err;
  352.  
  353.                   connection.query(`SELECT bg FROM xp WHERE id = '${message.author.id}'`, (err, rows) => {
  354.                     if (err) {
  355.                         connection.end();
  356.                         console.error(err);
  357.                         return;
  358.                     }
  359.                    
  360.                     if (rows.length < 1)
  361.                     {
  362.                       return message.reply('you do not have a profile set up yet! Please try again.').then(q => q.delete(1e4));
  363.                     }
  364.                     message.channel.startTyping();
  365.                     var bgpath = './bg_images/' + rows[0].bg;
  366.                     let avatar = message.author.avatarURL;
  367.                     let overlay = './bg_images/overlay.png'
  368.                     let xpbarcrop_x = 166 + 268;   
  369.                     let mime = jimp.MIME_PNG;
  370.  
  371.                     let modelica_reg12 = './fonts/modelica_reg12/modelica_reg12.fnt';
  372.                     let modelica_reg10 = './fonts/opensans_12/opensans_12.fnt';
  373.                     let modelica_reg18 = './fonts/modelica_extrabold18/opensans_bold20.fnt';
  374.                     let modelica_bold30 = './fonts/modelica_bold30/modelica_bold30.fnt';
  375.                     let modelica_extrabold18 = './fonts/modelica_extrabold18/opensans_bold20.fnt';
  376.                  
  377.                     let getProfile = function() {
  378.                       var promise = new Promise(function(resolve, reject) {
  379.                         jimp.read(bgpath).then(function(background) {
  380.                           jimp.read(overlay).then(function(overlay1) {
  381.                             jimp.read(avatar).then(function(avatar) {
  382.                               jimp.read(xppath).then(function(xpbar1) {
  383.                                 jimp.loadFont(modelica_reg12).then(function(fontreg12) {
  384.                                   jimp.loadFont(modelica_bold30).then(function(fontbold30) {
  385.                                     jimp.loadFont(modelica_extrabold18).then(function(fontextrabold18) {
  386.                                       jimp.loadFont(modelica_reg10).then(function(fontreg10) {
  387.                                         jimp.loadFont(modelica_reg18).then(function(fontreg18) {
  388.                                           background.composite(overlay1, 0, 0);
  389.                                           background.composite(avatar.resize(94, 94), 23, 67);
  390.                                           background.composite(xpbar1.crop(0, 0, xpbarcrop_x, 250), 0, 0);
  391.                                           background.print(
  392.                                             fontextrabold18,
  393.                                             168,
  394.                                             115,
  395.                                             {
  396.                                               text: 'USERNAME',
  397.                                               alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  398.                                             },
  399.                                             254,
  400.                                             30
  401.                                             );
  402.                                             background.print(
  403.                                                 fontreg10,
  404.                                                 168,
  405.                                                 157,
  406.                                                 {
  407.                                                     text: 'XP: ' + '9999999' + ' / ' + '9999999',
  408.                                                     alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  409.                                                 },
  410.                                                 254,
  411.                                                 30
  412.                                             );
  413.                                             background.print(
  414.                                                 fontreg10,
  415.                                                 168,
  416.                                                 135,
  417.                                                 {
  418.                                                     text: '"..."',
  419.                                                     alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  420.                                                 },
  421.                                                 254,
  422.                                                 30
  423.                                             );
  424.                                             background.print(fontreg12, 43, 169, 'LEVEL');
  425.                                             background.print(
  426.                                                 fontbold30,
  427.                                                 21,
  428.                                                 193,
  429.                                                 {
  430.                                                     text: '99',
  431.                                                     alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  432.                                                 },
  433.                                                 94,
  434.                                                 90
  435.                                             );
  436.                                             background.print(
  437.                                                 fontreg10,
  438.                                                 230,
  439.                                                 215,
  440.                                                 {
  441.                                                     text: 'Ranking:\t#99',
  442.                                                     alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  443.                                                 },
  444.                                                 120,
  445.                                                   30
  446.                                                 );
  447.                                             background.print(
  448.                                                 fontreg10,
  449.                                                 230,
  450.                                                 198,
  451.                                                 {
  452.                                                   text: 'Credits:\t9999999',
  453.                                                   alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  454.                                                 },
  455.                                                 120,
  456.                                                 30
  457.                                                 );
  458.                                             background.print(
  459.                                                 fontreg10,
  460.                                                 230,
  461.                                                 181,
  462.                                                 {
  463.                                                     text: 'Total XP:\t9999999',
  464.                                                     alignmentX: jimp.HORIZONTAL_ALIGN_CENTER
  465.                                                 },
  466.                                                 120,
  467.                                                   30
  468.                                                 );
  469.                                             let result = background.getBufferAsync(mime);
  470.                                             resolve(result);    
  471.                                         });
  472.                                     });
  473.                                 });
  474.                             });
  475.                         });
  476.                     });
  477.                 });
  478.               });
  479.               });
  480.               });
  481.               return promise;
  482.               };
  483.               message.channel.send('🎨 | ' + name + ' Preview:')
  484.               getProfile().then(function(result) {
  485.                 /*const buff = fs.readFileSync('./memes.txt');
  486.                 console.log(buff);
  487.                 const attach = new MessageAttachment(buff, 'memes.txt');
  488.                 message.channel.send(`${message.author}`, attach);*/
  489.                
  490.                 var buffer = result;
  491.                 let msg = message.channel.send({
  492.                 files: [{
  493.                 attachment: buffer,
  494.                 name: 'profile.png'
  495.                 }]
  496.                 }).then(q => q.delete(3e4))
  497.                 .catch(console.error);
  498.                 await msg.channel.send('Would you like to update your XP bar color to: ' + name + '?\nPlease enter \'yes\' to confirm or \'no\' to cancel.').then(q => q.delete(3e4))
  499.                 });
  500.              
  501.               message.channel.awaitMessages(filter, {
  502.                 max: 1,
  503.                 time: 3e4
  504.               }).then(collected => {
  505.                 collected.delete(3e4);
  506.    
  507.                 if (collected.first().content.toLowerCase() === 'no')
  508.                 {
  509.                   return message.reply('cancelled XP bar setup.').then(q => q.delete(1e4));
  510.                 }
  511.                 else if (collected.first().content.toLowerCase() === 'yes')
  512.                 {
  513.                   connection.query(`UPDATE xp SET color = '${selection}' WHERE id = '${message.author.id}'`);
  514.                   return message.reply('XP bar color updated!').then(q => q.delete(1e4));
  515.                 }
  516.                 else
  517.                 {
  518.                   return message.reply('that is an invalid option. Please try again.').then(q => q.delete(1e4));
  519.                 }
  520.               }).catch(err => {
  521.                 message.reply('profile setup cancelled due to inactivity').then(q => q.delete(1e4));
  522.               })  
  523.               message.channel.stopTyping();
  524.               });
  525.               connection.release();
  526.               });
  527.             }
  528.             else
  529.             {
  530.               return message.reply('that color does not exist! Please try again.').then(q => q.delete())
  531.             }
  532.           }).catch(err => {
  533.             message.reply('profile setup cancelled due to inactivity').then(q => q.delete(1e4));
  534.           })
  535.  
  536.         }
  537.         else if (collected.first().content === '3')
  538.         {
  539.           // Code for changing bio
  540.           return message.reply('Coming soon!').then(q => q.delete())
  541.         }
  542.         else
  543.         {
  544.           // Code for invalid command
  545.           return message.reply('that is an invalid option. Please try again.').then(q => q.delete(1e4))
  546.         }
  547.  
  548.       }).catch(err => {
  549.         message.reply('Profile setup cancelled due to inactivity').then(q => q.delete(1e4));
  550.       })
  551.     }
  552. }
  553.  
  554.  
  555. module.exports = SetupCommand;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement