SHARE
TWEET

Canvas

a guest Feb 14th, 2017 19 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const stripIndents  = require('common-tags').stripIndents;
  2. const fs            = require('fs');
  3. const moment        = require("moment");
  4. const util          = require('util');
  5. const beautify      = require('js-beautify').js_beautify;
  6. const rp            = require('request-promise-native');
  7. const request       = require("request");
  8. const canvas        = require("canvas");
  9. const Image         = new canvas.Image();
  10. const streamToArray = require('stream-to-array');
  11.  
  12. canvas.fromBuffer = buffer => {
  13.   let img = new canvas.Image(), c;
  14.   img.src = buffer;
  15.   c = new canvas(img.width, img.height);
  16.   try{
  17.     c.getContext('2d').drawImage(img, 0, 0);
  18.     return c;
  19.   }catch(e){
  20.     console.log(e);
  21.     return "Error loading image.";
  22.   }
  23. };
  24.  
  25. canvas.fromFile = src => new Promise(resolve => fs.readFile(src, (err, data) => resolve(canvas.fromBuffer(data))));
  26. canvas.fromURL = url => streamToArray(request(url)).then(buffers => Buffer.concat(buffers)).then(canvas.fromBuffer);
  27.  
  28. const showProfile = (client, user) => new Promise(async (resolve, reject) => {
  29.   const url = user.avatarURL;
  30.   const clientUser = client.scores.get(user.id);
  31.   const currentLevel = Math.floor(0.2 * Math.sqrt(clientUser.points));
  32.   const previousLevel = Math.floor(Math.pow(currentLevel/0.2, 2));
  33.   const nextLevel = Math.floor(Math.pow((currentLevel + 1)/0.2, 2));
  34.   const Prog = Math.round(((clientUser.points-previousLevel)/(nextLevel-previousLevel))*228);
  35.   let red   = parseInt(clientUser.color.substring(0,2), 16);
  36.   let green = parseInt(clientUser.color.substring(2,4), 16);
  37.   let blue  = parseInt(clientUser.color.substring(4,6), 16);
  38.   return streamToArray(request(url)).then(buffers => Buffer.concat(buffers)).then(async buffer => {
  39.     const Image = canvas.Image;
  40.     let img = new Image(), c;
  41.     c = new canvas(370, 281);
  42.     ctx = c.getContext("2d");
  43.     ctx.save();
  44.     ctx.fillStyle = `rgba(${red} , ${green}, ${blue}, 1)`;
  45.     ctx.fillRect(90, 46, 275, 4);
  46.     const NunitoSansExtraLight = new canvas.Font('NunitoSansExtraLight', './assets/NunitoSans-ExtraLight.ttf');
  47.     const FiraSans = new canvas.Font('FiraSans', './assets/FiraSans-Regular.ttf');
  48.     const sortedList = client.scores.array().sort(function (a, b) {return b.points - a.points});
  49.     try{
  50.       return canvas.fromFile('profile.png').then(cv => {cv.src = cv.toBuffer(); ctx.drawImage(cv, 0, 0, 370, 281)}).then(async () => {
  51.         try {
  52.           ctx.antialias = 'subpixel';
  53.           ctx.scale(1, 1);
  54.           ctx.fillStyle = `rgba(${red} , ${green}, ${blue}, 1)`;
  55.           ctx.fillRect(116, 71, Prog, 20);
  56.           ctx.save();
  57.           ctx.beginPath();
  58.           ctx.arc(49, 54, 45, 0, Math.PI * 2, false);
  59.           ctx.clip();
  60.           img.onload = function () {
  61.             ctx.drawImage(img, 5, 9, 90, 90);
  62.           }
  63.           img.src = buffer;
  64.           ctx.restore();
  65.           ctx.font = '25px NunitoSansExtraLight';
  66.           ctx.fillStyle = "rgb(230,230,230)";
  67.           ctx.fillText(user.username, 106, 38);
  68.           ctx.fillStyle = "rgb(23,23,23)";
  69.           ctx.textAlign="right";
  70.           ctx.font = '18px FiraSans';
  71.           ctx.fillText(clientUser.points, 340, 156);
  72.           ctx.fillText(parseInt(sortedList.map(l => l.ID).indexOf(user.id)) + 1, 340, 179);
  73.           ctx.fillText(clientUser.money, 340, 202);
  74.           ctx.font = '30px FiraSans';
  75.           ctx.textAlign="center";
  76.           ctx.fillText(currentLevel, 70, 186);
  77.           ctx.restore();
  78.           resolve(c);
  79.         } catch (e) {
  80.           reject(`Woops! Something happened:\n\`\`\`${e}\`\`\``)
  81.         }
  82.       });
  83.     } catch(e) {
  84.       reject(`Woops! Something happened:\n\`\`\`${e}\`\`\``)
  85.     }
  86.   });
  87. });
  88.  
  89. exports.run = async (client, msg, [qs]) => {
  90.   let query = client.funcs.searchUser(qs, msg.guild);
  91.   if (!query) return msg.channel.send(`User not found: \`${qs}\``);
  92.   const user = query.user;
  93.   showProfile(client, user).then(cv => msg.channel.sendFile(cv.toBuffer()));
  94. };
  95.  
  96. exports.conf = {
  97.   enabled: true,
  98.   guildOnly: true,
  99.   aliases: [],
  100.   permLevel: 10,
  101.   botPerms: [],
  102.   requiredFuncs: [],
  103. };
  104.  
  105. exports.help = {
  106.   name: "kyrazen",
  107.   description: "",
  108.   usage: "<user:string>",
  109.   usageDelim: ""
  110. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top