Advertisement
Guest User

clock code

a guest
Feb 21st, 2020
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. const Discord = require("discord.js");
  2. const client = new Discord.Client();
  3. const { readFile, readFileSync } = require('fs-nextra');
  4. const fs = require("fs")
  5.  
  6. const { get } = require('snekfetch');
  7. client.on('ready', () => {
  8. console.log("Hello, I'm ready")
  9. })
  10. const jimp = require('jimp');
  11. const Canvas = require("canvas");
  12. client.on('message', message => {
  13. if (message.content == ('-clock')) {
  14.  
  15. const w = ['./file.png'];
  16.  
  17. let Image = Canvas.Image,
  18. canvas = new Canvas.Canvas(400, 400),
  19. ctx = canvas.getContext('2d');
  20. var radius = canvas.height / 2;
  21. var currentTime = new Date(),
  22. hours = currentTime.getHours(),
  23. minute = currentTime.getMinutes();
  24. var second = currentTime.getSeconds();
  25. ctx.translate(radius, radius);
  26. radius = radius * 0.90
  27.  
  28. ctx.patternQuality = 'bilinear';
  29. ctx.filter = 'bilinear';
  30. ctx.antialias = 'subpixel';
  31. ctx.shadowColor = 'rgba(0, 0, 0, 0.4)';
  32. ctx.shadowOffsetY = 2;
  33. ctx.shadowBlur = 2;
  34. fs.readFile(`${w[Math.floor(Math.random() * w.length)]}`, function (err, Background) {
  35. if (err) return console.log(err);
  36. let BG = Canvas.Image;
  37. let ground = new Image;
  38. ground.src = Background;
  39. ctx.drawImage(ground, 0, 0, 400, 169);
  40. })
  41.  
  42.  
  43. ctx = canvas.getContext('2d');
  44. ctx.patternQuality = 'bilinear';
  45. ctx.filter = 'bilinear';
  46. ctx.antialias = 'subpixel';
  47. ctx.shadowColor = 'rgba(0, 0, 0, 0.4)';
  48. ctx.shadowOffsetY = 2;
  49. ctx.shadowBlur = 2;
  50. fs.readFile(`${w[Math.floor(Math.random() * w.length)]}`, function (err, Background) {
  51. if (err) return console.log(err);
  52. let BG = Canvas.Image;
  53.  
  54. })
  55. let user = message.mentions.users.first();
  56. var men = message.mentions.users.first();
  57. var heg;
  58. if (men) {
  59. heg = men
  60. } else {
  61. heg = message.author
  62. }
  63. var mentionned = message.mentions.members.first();
  64. var h;
  65. if (mentionned) {
  66. h = mentionned
  67. } else {
  68. h = message.member
  69. }
  70. var ment = message.mentions.users.first();
  71. var getvalueof;
  72. if (ment) {
  73. getvalueof = ment;
  74. } else {
  75. getvalueof = message.author;
  76. }
  77. let url = getvalueof.displayAvatarURL.endsWith(".webp") ? getvalueof.displayAvatarURL.slice(5, -20) + ".png" : getvalueof.displayAvatarURL;
  78. jimp.read(url, (err, ava) => {
  79. if (err) return console.log(err);
  80. ava.getBuffer(jimp.MIME_PNG, (err, buf) => {
  81. if (err) return console.log(err);
  82.  
  83.  
  84. function drawFace(ctx, radius) {
  85. var grad;
  86. ctx.beginPath();
  87. ctx.arc(0, 0, radius, 0, 2 * Math.PI);
  88. ctx.fillStyle = 'white';
  89. ctx.fill();
  90. grad = ctx.createRadialGradient(0, 0, radius * 0.95, 0, 0, radius * 1.05);
  91. grad.addColorStop(0, '#333');
  92. grad.addColorStop(0.5, 'black');
  93. grad.addColorStop(1, '#333');
  94. ctx.strokeStyle = grad;
  95. ctx.lineWidth = radius * 0.1;
  96. ctx.stroke();
  97. ctx.beginPath();
  98. ctx.arc(0, 0, radius * 0.1, 0, 2 * Math.PI);
  99. ctx.fillStyle = '#333';
  100. ctx.fill();
  101. }
  102. drawClock();
  103.  
  104. function drawClock() {
  105. drawFace(ctx, radius);
  106. drawNumbers(ctx, radius);
  107. drawTime(ctx, radius);
  108. }
  109.  
  110. function drawNumbers(ctx, radius) {
  111. var ang;
  112. var num;
  113. ctx.font = radius * 0.15 + "px arial";
  114. ctx.textBaseline = "middle";
  115. ctx.textAlign = "center";
  116. for (num = 1; num < 13; num++) {
  117. ang = num * Math.PI / 6;
  118. ctx.rotate(ang);
  119. ctx.translate(0, -radius * 0.85);
  120. ctx.rotate(-ang);
  121. ctx.fillText(num.toString(), 0, 0);
  122. ctx.rotate(ang);
  123. ctx.translate(0, radius * 0.85);
  124. ctx.rotate(-ang);
  125. }
  126. }
  127.  
  128. function drawTime(ctx, radius) {
  129.  
  130. if (hours > 12) {
  131. hours -= 12;
  132. } else if (hours == 0) {
  133. hours = "12";
  134. }
  135. if (minute < 10) {
  136. minute = '0' + minute;
  137. }
  138. //hour
  139. hours = hours % 12;
  140. hours = (hours * Math.PI / 6) +
  141. (minute * Math.PI / (6 * 60)) +
  142. (second * Math.PI / (360 * 60));
  143. drawHand(ctx, hours, radius * 0.5, radius * 0.07);
  144. //minute
  145. minute = (minute * Math.PI / 30) + (second * Math.PI / (30 * 60));
  146. drawHand(ctx, minute, radius * 0.8, radius * 0.07);
  147. }
  148.  
  149. function drawHand(ctx, pos, length, width) {
  150. ctx.beginPath();
  151. ctx.lineWidth = width;
  152. ctx.lineCap = "round";
  153. ctx.moveTo(0, 0);
  154. ctx.rotate(pos);
  155. ctx.lineTo(0, -length);
  156. ctx.stroke();
  157. ctx.rotate(-pos);
  158. }
  159. var currentTime = new Date(),
  160. hours2 = currentTime.getHours(),
  161. minutes2 = currentTime.getMinutes(),
  162. seconds2 = currentTime.getSeconds();
  163.  
  164. if (hours2 > 12) {
  165. hours2 -= 12;
  166. } else if (hours2 == 0) {
  167. hours2 = "12";
  168. }
  169. if (minutes2 < 10) {
  170. minutes2 = '0' + minutes2;
  171. }
  172.  
  173. message.channel.send((`**:clock: | الساعة في الوقت الحالي : ${hours2}:${minutes2}:${seconds2}**`));
  174. message.channel.sendFile(canvas.toBuffer());
  175. })
  176. })
  177. }
  178. });
  179. client.login(process.env.BOT_TOKEN);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement