Need help with Discord Webhooks

spyderexe

Spyder

Posted on July 28, 2018

Need help with Discord Webhooks

Hello DEV.to community! First off I'd like to apologize if this is not the correct place to post this, it seems at first glance that this is a place to write and post articles and less to post coding errors, but in the doubt I really would like answers as other forums (Stack) didn't help much

I'm not really experienced in Node.js and javascript and all that and I wanted to add a webhook to my discord server. I found this github project that I'd like to implement: https://github.com/FrankenMan/me_irl-webhook

So I followed the discord documentation, made a fork, added the webhook, and everything seems to work (I get automatic discord messages everytime I make a commit), however the bot doesn't do anything.

here's my fork: https://github.com/Spyder-exe/me_irl-webhook/

So I did a bit of research, and I found that the bot needed a config.json file and a posts.json file. So I renammed the config.json.example and added my webhook's id and token, I created a blank posts.json file.

I also changed the package.json file as the project was a year old from that:

"dependencies": {
  "discord.js": "^11.0.0",
  "erlpack": "github:hammerandchisel/erlpack",
  "request": "^2.79.0",
  "uws": "^0.13.0"
}

To this:

"dependencies": {
  "discord.js": ">=11.0.0",
  "erlpack": "github:discordapp/erlpack",
  "request": ">=2.79.0",
  "uws": ">=0.13.0"
}

However the bot still doesn't seem to do anything, here's the main bot.js code, again I'm not that experienced with Javascript so I can't tell what's wrong

const Discord = require('discord.js');
const request = require('request');
const fs = require('fs');
const config = require('./config.json');
const posts = require('./posts.json');


const webhook = new Discord.WebhookClient(config.webhookid, config.webhooktoken);

const postDict = JSON.parse(fs.readFileSync('./posts.json', 'utf8'));
//function for logging ids and urls of posts to stop repeat posts.
function postLog(postId, postUrl) {
    postDict[postId] = {
    url: postUrl
  }
  fs.writeFile('./posts.json',JSON.stringify(postDict), (err) => {
    if (err) console.error(err)
  })
}


function fetchRedditPost() {
request(config.url, function(error,response,body) {
      var ok = JSON.parse(body)
      ok.data.children.forEach(function(ok){
        let NUT = "imgur.com"
        let ext = ".jpg"
        let otherExt = ".gif"
        let dril = ".gifv"
        let r34 = ".png"
        let alb = "/a/"
        //checking if it's an imgur link without .jpg, .gif, .gifv, .png
    if (ok.data.url.includes(NUT) && !ok.data.url.includes(ext && otherExt && dril && r34)) {
       const SHACK = ok.data.url + ext
       //skip imgur album links
     if (ok.data.url.includes(alb)) return;
     //check if this post has been logged. If false, post it on Discord and log it, if true, do not post it
      if (!postDict[ok.data.id]){
        webhook.sendMessage(`${ok.data.title}\n${SHACK}`);
       postLog(ok.data.id, SHACK)
      }
      else {
         return
      }
    }
    //urls containing i.reddituploads.com don't show up in Discord
    else if (ok.data.url.includes("i.reddituploads.com")){
     if (!postDict[ok.data.id]) {
     postLog(ok.data.id,ok.data.preview.images[0].source.url);
     webhook.sendMessage(`${ok.data.title}\n${ok.data.preview.images[0].source.url}`)
     }
     else {
        return;
     }
    }
    else{
      if (!postDict[ok.data.id]){
        postLog(ok.data.id, ok.data.url)
        webhook.sendMessage(`${ok.data.title}\n${ok.data.url}`);
      }
      else{
        return;
      }

    };
      })
})
};
function redditInterval() {
 setInterval(() => (fetchRedditPost()), 36000);
}
redditInterval();
💖 💪 🙅 🚩
spyderexe
Spyder

Posted on July 28, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Need help with Discord Webhooks
javascript Need help with Discord Webhooks

July 28, 2018