Webhook似乎不起作用

时间:2018-07-28 12:21:47

标签: javascript node.js webhooks discord discord.js

我对Node.js和JavaScript以及其他方面并没有真正的经验,所以我想将Webhook添加到我的Discord服务器中。 我找到了我要实现的GitHub项目:https://github.com/FrankenMan/me_irl-webhook

因此,我遵循了Discord文档,进行了分叉,添加了Webhook,一切似乎都可以正常工作(每次提交时都会收到自动Discord消息),但是,该机器人没有执行任何操作。

这是我的叉子:https://github.com/Spyder-exe/me_irl-webhook/

所以我做了一些研究,发现该机器人需要config.json文件和posts.json文件。因此,我重命名了config.json.example并添加了Webhook的ID和令牌,创建了一个空白的posts.json文件。

我还更改了package.json文件,因为该项目成立了一年。

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

对此:

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

但是该bot似乎仍然什么也没做,这是主要的bot.js代码,同样,我对Javascript的经验也不多,所以我无法分辨出什么毛病

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

0 个答案:

没有答案