Javascript Webhook没有';好像不行
我对Node.js和JavaScript以及所有这些都没有真正的经验,我想在我的Discord服务器上添加一个webhook。 我找到了我想要实施的GitHub项目: 因此,我遵循Discord文档,制作了一个fork,添加了webhook,一切似乎都正常(每次提交时,我都会收到自动的Discord消息),但是,bot什么也不做 这是我的叉子: 所以我做了一些研究,发现bot需要一个config.json文件和一个posts.json文件。因此,我重命名了config.json.example并添加了我的webhook的id和令牌,创建了一个空白的posts.json文件 我还更改了package.json文件,因为该项目已有一年的历史:Javascript Webhook没有';好像不行,javascript,node.js,webhooks,discord,discord.js,Javascript,Node.js,Webhooks,Discord,Discord.js,我对Node.js和JavaScript以及所有这些都没有真正的经验,我想在我的Discord服务器上添加一个webhook。 我找到了我想要实施的GitHub项目: 因此,我遵循Discord文档,制作了一个fork,添加了webhook,一切似乎都正常(每次提交时,我都会收到自动的Discord消息),但是,bot什么也不做 这是我的叉子: 所以我做了一些研究,发现bot需要一个config.json文件和一个posts.json文件。因此,我重命名了config.json.example并
"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();
不需要对>=进行更改,请使用原始文件进行尝试。调试的第一步是确定代码是否尝试发出任何请求,只需在每个
webhook.sendMessage
之前添加console.log
语句,并在控制台中验证您是否获得了一些输出。我检查了您的webhook,发现它不存在。为了安全起见,您可能在GitHub中输入了一个错误的。