/src/commands/Utility/timezone.js

https://github.com/RayzrDev/SharpBot · JavaScript · 54 lines · 41 code · 13 blank · 0 comment · 8 complexity · f2dbd24acf72e0830158d0dbf6340e54 MD5 · raw file

  1. const got = require('got');
  2. exports.run = async (bot, msg, args) => {
  3. if (args.length < 1) {
  4. throw 'You must specify a time to convert';
  5. }
  6. let input = args.join(' ');
  7. let url = `https://api.duckduckgo.com/?q=${encodeURIComponent(input)}&format=json`;
  8. await msg.edit(':arrows_counterclockwise: Loading conversion...');
  9. const res = await got(url, { json: true });
  10. if (!res || !res.body) {
  11. throw 'Could not load data from DDG.';
  12. }
  13. let data = res.body;
  14. let answer = data['Answer'];
  15. let message;
  16. if (data['AnswerType'] === 'timezone_converter') {
  17. msg.delete();
  18. let matches = input.match(/(.*?)\s*(to|in)\s*(.*)/);
  19. let prefix;
  20. if (matches) {
  21. prefix = matches[1];
  22. } else {
  23. prefix = input;
  24. }
  25. message = bot.utils.embed('', '', [
  26. {
  27. name: 'Timezone:',
  28. value: `${prefix} \u2794 ${answer}`
  29. }
  30. ]);
  31. msg.channel.send({ embed: message });
  32. } else {
  33. throw `No conversion found for ${input}`;
  34. }
  35. };
  36. exports.info = {
  37. name: 'timezone',
  38. usage: 'timezone <time> to <time>',
  39. description: 'converts between timezones, using DuckDuckGo searches',
  40. credits: '<@136641861073764352>' // Abyss#0473
  41. };