/Commands/Public/ddg.js

https://github.com/BitQuote/AwesomeBot · JavaScript · 20 lines · 19 code · 1 blank · 0 comment · 7 complexity · b5632bb6f7dfab83de60e65a86b7e849 MD5 · raw file

  1. const unirest = require("unirest");
  2. module.exports = (bot, db, config, winston, userDocument, serverDocument, channelDocument, memberDocument, msg, suffix, commandData) => {
  3. if(suffix) {
  4. unirest.get(`http://api.duckduckgo.com/?format=json&q=${encodeURIComponent(suffix)}`).header("Accept", "application/json").end(res => {
  5. if(res.status==200) {
  6. const data = JSON.parse(res.body);
  7. if(data.Results.length>0 && data.AbstractText) {
  8. msg.channel.createMessage(`\`\`\`${data.AbstractText}\`\`\`${data.Results[0].FirstURL}`);
  9. return;
  10. }
  11. }
  12. winston.warn(`DuckDuckGo instant answer for '${suffix}' not found`, {svrid: msg.guild.id, chid: msg.channel.id, usrid: msg.author.id});
  13. msg.channel.createMessage("DuckDuckGo can't answer that. Maybe try Google? 😉");
  14. });
  15. } else {
  16. winston.warn(`Parameters not provided for ${commandData.name} command`, {svrid: msg.guild.id, chid: msg.channel.id, usrid: msg.author.id});
  17. msg.channel.createMessage(`${msg.author.mention} Wtf am I supposed to do with nothing?`);
  18. }
  19. };