/src/commands/useful/duckduckgo.js

https://github.com/Gravebot/Gravebot · JavaScript · 92 lines · 83 code · 9 blank · 0 comment · 27 complexity · ea955ab7c61cea3b56191e7e20051b80 MD5 · raw file

  1. import Promise from 'bluebird';
  2. import T from '../../translate';
  3. const request = Promise.promisify(require('request'));
  4. function ddg(client, evt, suffix, lang) {
  5. if (!suffix) return Promise.resolve(T('ddg_usage', lang));
  6. const options = {
  7. method: 'GET',
  8. url: 'http://api.duckduckgo.com/',
  9. json: true,
  10. qs: {
  11. q: `${suffix}`,
  12. format: 'json',
  13. pretty: '0',
  14. no_redirect: '1',
  15. no_html: '1',
  16. skip_disambig: '0',
  17. t: 'Gravebot-Discord'
  18. }
  19. };
  20. return request(options)
  21. .then(response => {
  22. let text = '';
  23. if (response.body.Redirect) {
  24. text += response.body.Redirect;
  25. } else if (response.body.Heading || response.body.Answer) {
  26. if (response.body.Answer) text += `${response.body.Answer}\n\n`;
  27. if (response.body.Definition) text += `${response.body.Definition}\n<${response.body.DefinitionURL}>\n\n`;
  28. if (response.body.AbstractText) text += `${response.body.AbstractText}\n\n`;
  29. if (response.body.Results[0]) text += `${response.body.Results[0].Text} - <${response.body.Results[0].FirstURL}>\n\n`;
  30. if (response.body.RelatedTopics[0]) {
  31. if (response.body.RelatedTopics[0].Text) {
  32. text += response.body.RelatedTopics[0].Text;
  33. } else {
  34. text += response.body.RelatedTopics[0].Topics[0].Text;
  35. }
  36. }
  37. if (response.body.RelatedTopics[1]) {
  38. if (response.body.RelatedTopics[1].Text) {
  39. text += `\n${response.body.RelatedTopics[1].Text}`;
  40. } else {
  41. text += `\n${response.body.RelatedTopics[1].Topics[0].Text}`;
  42. }
  43. }
  44. if (response.body.RelatedTopics[2]) {
  45. if (response.body.RelatedTopics[2].Text) {
  46. text += `\n${response.body.RelatedTopics[2].Text}`;
  47. } else {
  48. text += `\n${response.body.RelatedTopics[2].Topics[0].Text}`;
  49. }
  50. }
  51. if (response.body.RelatedTopics[3]) {
  52. if (response.body.RelatedTopics[3].Text) {
  53. text += `\n${response.body.RelatedTopics[3].Text}`;
  54. } else {
  55. text += `\n${response.body.RelatedTopics[3].Topics[0].Text}`;
  56. }
  57. }
  58. if (response.body.RelatedTopics[4]) {
  59. if (response.body.RelatedTopics[4].Text) {
  60. text += `\n${response.body.RelatedTopics[4].Text}`;
  61. } else {
  62. text += `\n${response.body.RelatedTopics[4].Topics[0].Text}`;
  63. }
  64. }
  65. if (response.body.AbstractURL) text += `\n<${response.body.AbstractURL}>`;
  66. if (response.body.Image) text += `\n${response.body.Image}`;
  67. } else {
  68. text += `${T('ddg_error', lang)}: ${suffix}`;
  69. }
  70. return text;
  71. });
  72. }
  73. export default {
  74. d: ddg,
  75. ddg,
  76. duck: ddg,
  77. duckduckgo: ddg,
  78. s: ddg,
  79. search: ddg
  80. };
  81. export const help = {
  82. ddg: {parameters: 'search terms'}
  83. };