gaber-elsayed

WIDE

Feb 8th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. exports.description = 'Turns provided attached image / emote / URL / server member\'s avatar into two wide images (Outputs only in PNG format for now).';
  2. exports.usage = `<:Kappa:561931926794141768>`
  3. +`\nhttps://static-cdn.jtvnw.net/emoticons/v1/114836/3.0`
  4. +`\n<@506606171906637855>`
  5. +`\n506606171906637855`;
  6. exports.level = 0;
  7. exports.perms = [];
  8. exports.cooldown = 7000;
  9. exports.dmable = true;
  10.  
  11. exports.run = async message => {
  12. const canvas = require('canvas');
  13. const fetch = require('node-fetch');
  14. let url;
  15. let msg = await message.channel.send('Analyzing image...');
  16.  
  17. //attached files
  18. if (message.attachments.size){
  19. if (!(await fetch(message.attachments.first().url)).headers.get('content-type').startsWith('image')) throw ['normal', 'Provided file is not an image!'];
  20. url = message.attachments.first().url;
  21. }
  22. else {
  23. //trying arguments
  24. if (!message.args.length) throw ['normal', 'You must attach an image, custom emote, URL, or userID / @Mention !'];
  25.  
  26. //emote
  27. let res = /<(a?):([a-z0-9-_]+):(\d+)>/i.exec(message.args[0]);
  28. //res[0] - whole string; res[1] - is animated; res[2] - name; res[3] - id
  29. if (res) url = `${client.options.http.cdn}/emojis/${res[3]}.${res[1].length ? 'gif' : 'png'}`;
  30.  
  31. //user avatars (mention or id)
  32. else if (client.users.cache.has(message.args[0])){
  33. if (!client.users.cache.get(message.args[0]).avatar) throw ['normal', 'Target user doesn\'t have an avatar.'];
  34. url = client.users.cache.get(message.args[0]).avatarURL({format: 'png', dynamic: true, size: 4096});
  35. }
  36. else if (message.mentions.users.first()){
  37. if (!message.mentions.users.first().avatar) throw ['normal', 'Target user doesn\'t have an avatar.'];
  38. url = message.mentions.users.first().avatarURL({format: 'png', dynamic: true, size: 4096});
  39. }
  40.  
  41. //url
  42. else {
  43. await fetch(message.args[0]).catch(e => {throw ['fetch', e.toString()];});
  44. if ((await fetch(message.args[0])).headers.get('content-type').startsWith('image')) url = message.args[0];
  45. else throw ['normal', 'You must attach an image, custom emote, URL, or userID / @Mention !'];
  46. }
  47. }
  48.  
  49. //squishing
  50. await msg.edit('Transforming image...');
  51. try {
  52. let img = await canvas.loadImage(url);
  53. let sq1 = canvas.createCanvas(img.width, img.height);
  54. let sq2 = canvas.createCanvas(img.width, img.height);
  55. let ctx1 = sq1.getContext('2d');
  56. let ctx2 = sq2.getContext('2d');
  57. ctx1.drawImage(img, 0, 0, sq1.width * 2, img.height);
  58. ctx2.drawImage(img, -(img.width), 0, sq2.width * 2, img.height);
  59. await message.channel.send('Wide Images', {files: [
  60. {
  61. attachment: sq1.toBuffer(),
  62. name: 'wide1.png'
  63. },
  64. {
  65. attachment: sq2.toBuffer(),
  66. name: 'wide2.png'
  67. }
  68. ]});
  69. msg.delete();
  70. }
  71. catch (err){throw ['canvas', err.toString()];}
  72. }
Advertisement
Add Comment
Please, Sign In to add comment