Advertisement
gaber-elsayed

yearprogress

Feb 8th, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. exports.description = 'Displays progress of current year. (by UTC)';
  2. exports.usage = '';
  3. exports.level = 0;
  4. exports.perms = [];
  5. exports.cooldown = 5000;
  6. exports.dmable = true;
  7.  
  8. exports.run = async message => {
  9. let date = message.createdAt;
  10. let cy = date.getUTCFullYear();
  11. let notLeap = cy % 4; //is year not-leap (counts extra day in feb)
  12. function getProgress(d){
  13. let full = 31536000;
  14. let total = 0;
  15. if (!notLeap){
  16. full += 86400;
  17. if (d.getUTCMonth() >= 2) total += 86400;
  18. }
  19. let monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30];
  20. total += monthDays.slice(0, d.getUTCMonth()).reduce((a, b) => a + b, 0) * 86400;
  21. total += (d.getUTCDate()-1) * 86400;
  22. total += d.getUTCHours() * 3600;
  23. total += d.getUTCMinutes() * 60;
  24. total += d.getUTCSeconds();
  25. return total * 100 / full;
  26. }
  27. //attached image loading-bar
  28. let canvas = require('canvas');
  29. let cv = canvas.createCanvas(400, 40);
  30. let ctx = cv.getContext('2d');
  31. ctx.fillStyle = '#000000'; ctx.fillRect(0, 0, 400, 40); //background, black borders
  32. ctx.fillStyle = '#747f8d'; ctx.fillRect(5, 5, 390, 30); //grey inside part
  33. ctx.fillStyle = '#43b581'; ctx.fillRect(5, 5, (Math.floor(390/100 * getProgress(date))), 30); //green progress bar
  34. let {round} = require('../src/utils/formatter');
  35. message.channel.send(`**${cy}** is **${round(getProgress(date), 13)}%** complete.`, {files:[ {attachment: cv.toBuffer(), name: 'yearprogress.jpg'} ]});
  36. }
  37.  
  38. ///formatter
  39.  
  40. //numeric operations
  41. exports.leadZero = function(n){ return n < 10 ? '0'+n : n.toString(); }
  42. exports.numSuffix = function(n){
  43. if ([11, 12, 13].includes(n%100)) return 'th';
  44. else switch(n%10){
  45. case 1: return 'st';
  46. case 2: return 'nd';
  47. case 3: return 'rd';
  48. default: return 'th';
  49. }
  50. }
  51. exports.round = function(n, k){
  52. let factor = 10**k;
  53. return Math.round(n*factor)/factor;
  54. }
  55. //date and time operations
  56. exports.dateFormat = function(date){ return `${exports.leadZero(date.getDate())}/${exports.leadZero(date.getMonth()+1)}/${exports.leadZero(date.getFullYear())}`; }
  57. exports.hourFormat = function(date){ return `${exports.leadZero(date.getHours())}:${exports.leadZero(date.getMinutes())}:${exports.leadZero(date.getSeconds())}`; }
  58. exports.msToHuman = function(n, ln){
  59. n = parseInt(n);
  60. let str = [];
  61. if (n >= 1000 * 60 * 60 * 24 * 365) cut(1000 * 60 * 60 * 24 * 365, 'y');
  62. // if (n >= 1000 * 60 * 60 * 24 * 30) cut(1000 * 60 * 60 * 24 * 30, 'mo'); //decided to not use that for now, because months have different amounts of days
  63. if (n >= 1000 * 60 * 60 * 24) cut(1000 * 60 * 60 * 24, 'd');
  64. if (n >= 1000 * 60 * 60) cut(1000 * 60 * 60, 'h');
  65. if (n >= 1000 * 60) cut(1000 * 60, 'm');
  66. if (n >= 1000) cut(1000, 's');
  67. if (!str.length && n < 1000) cut(1, 'ms'); //only when there's no other time units specified
  68. //execution part
  69. function cut(v, c){
  70. str.push(Math.floor(n / v)+c);
  71. n = n % v;
  72. }
  73. return str.slice(0, ln || 420).join(' ');
  74. }
  75. exports.humanToSec = function(timeStr){
  76. //parsing human-readable-like strings to amount of seconds
  77. const regex = /(\d+)\s*([a-z]+)/gi;
  78. let res = 0;
  79. let m;
  80.  
  81. while ((m = regex.exec(timeStr)) !== null){
  82. if (m.index === regex.lastIndex) regex.lastIndex++;
  83. switch(m[2]){
  84. case 's':
  85. case 'second':
  86. case 'seconds':
  87. res += parseInt(m[1]);
  88. break;
  89. case 'm':
  90. case 'minute':
  91. case 'minutes':
  92. res += parseInt(m[1]) * 60;
  93. break;
  94. case 'h':
  95. case 'hour':
  96. case 'hours':
  97. res += parseInt(m[1]) * 60 * 60;
  98. break;
  99. case 'd':
  100. case 'day':
  101. case 'days':
  102. res += parseInt(m[1]) * 60 * 60 * 24;
  103. break;
  104. case 'w':
  105. case 'week':
  106. case 'weeks':
  107. res += parseInt(m[1]) * 60 * 60 * 24 * 7;
  108. break;
  109. case 'y':
  110. case 'year':
  111. case 'years':
  112. res += parseInt(m[1]) * 60 * 60 * 24 * 365;
  113. break;
  114. }
  115. }
  116. return res;
  117. }
  118. //misc convertions
  119. exports.snowflake = function(sf){
  120. //hard assume, that given snowflake is 100% valid
  121. const DiscordEpoch = 1420070400000;
  122. function getBinSlice(start, end){return parseInt(parseInt(sf).toString(2).slice(start, end), 2);}
  123. return {
  124. timestamp: DiscordEpoch+getBinSlice(0, -22),
  125. worker: getBinSlice(-22, -17),
  126. process: getBinSlice(-17, -12),
  127. increment: getBinSlice(-12)
  128. }
  129. }
  130. exports.bytesToUnits = function(n){
  131. n = parseInt(n);
  132. let str = [];
  133. if (n >= 1024 * 1024 * 1024) cut(1024 * 1024 * 1024, 'GB');
  134. if (n >= 1024 * 1024) cut(1024 * 1024, 'MB');
  135. if (n >= 1024) cut(1024, 'kB');
  136. if (!str.length && n < 1024) cut(1, 'B');
  137. function cut(v, c){
  138. str.push(Math.floor(n / v)+c);
  139. n = n % v;
  140. }
  141. return str.join(' ');
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement