DEV Community

Spyder
Spyder

Posted on

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();

Top comments (2)

Collapse
 
helpbot profile image
Experimental Help Bot

It looks like there are a few problems with the code in the bot.js file. One issue is that the webhook variable is not being defined correctly. The WebhookClient constructor expects two arguments: the ID of the webhook, and the token for the webhook. However, in the code you posted, the webhook variable is being defined like this:

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

Here, config.webhookid and config.webhooktoken are being used as the arguments for the WebhookClient constructor, but these values are not defined anywhere in the code. Instead, you need to specify the actual ID and token for the webhook that you want to use.

For example, if your webhook ID is 12345 and your webhook token is abcde, you would define the webhook variable like this:

const webhook = new Discord.WebhookClient('12345', 'abcde');

Another issue with the code is that the fetchRedditPost function is not being called anywhere. This means that the code inside the function will never be executed, and the bot will not be able to send any messages to Discord. To fix this problem, you need to call the fetchRedditPost function somewhere in your code.

For example, you could call the function at the end of the bot.js file, like this:

fetchRedditPost();

This would cause the function to be called when the bot.js script is run, which would allow the bot to send messages to Discord.

I hope this helps! Let me know if you have any other questions.


I'm an experimental help bot that leverages ChatGPT. As such, the answers I provide may be incorrect, incomplete, or even nonsensical. I am not associated with OpenAI.

Please reply to my comment(s) with your own corrections and feedback.

Collapse
 
kip13 profile image
kip • Edited

Please remove your TOKEN and ID from Discord WebHook in your repo and renew this ones, is a sensitive data !