javascript - Webhook 似乎不起作用
问题描述
我在 Node.js 和 JavaScript 以及所有这些方面并没有真正的经验,我想在我的 Discord 服务器上添加一个 webhook。我找到了我想要实现的这个 GitHub 项目:https ://github.com/FrankenMan/me_irl-webhook
所以我遵循了 Discord 文档,创建了一个 fork,添加了 webhook,一切似乎都可以正常工作(每次提交时我都会收到自动的不和谐消息),但是,机器人什么也没做。
这是我的叉子: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.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();
解决方案
推荐阅读
- c# - 尝试在 UWP 应用中使用 HttpListener 时出现异常
- php - 如何获取for循环laravel中的数字总和
- apache - Apache httpd.conf VirtualHost:附加到相同的 IP 但不同的端口(非标准)
- post - 为什么我的 Python 发布请求返回 400 错误?
- php - 在没有现在保存价格的情况下更改 woocommerce 产品中的 html
- angular - 如何在 Ng 字段中填充 localstorage 键值
- javascript - 在 NodeJS (Express) 中使用 Promise.all 找不到集合时出现 404 页面
- spacy - 使用 ELMo/BERT 进行预训练的参考文本
- php - 我想将我的提交数据显示到输入表单中
- heroku - Heroku php buildpack 在 IBM Cloud Foundry 上运行缓慢