我正在设置一台新服务器,并想运行一个nodejs脚本来解决问题。但是问题是;在脚本启动/或运行后,它将发布错误。
我试图在GitHub上没有答案的情况下联系“作者”以修复该问题,并且我确实尝试在我在线的其他服务器上进行了测试,但它表示相同的错误。 (包括降级nodejs / npm版本。)
我找到的代码来自以下GitHub页面:https://github.com/Triniayo/nodejs-discord-csgoupdate
// Requirements
const { Client } = require('discord.js');
const fs = require('fs');
const Parser = require('rss-parser');
const htmlToText = require('html-to-text');
const log = require('npmlog');
// Load Config
const cfg = require('./config.json');
// Create feeder instance
let rssParser = new Parser({
// Renaming 'content:encoded' to 'contentHtml' because it won't be useable otherwise
customFields: {
item: [
['content:encoded', 'contentHtml']
]
},
// Setting User Agent
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
});
// Setting variables for the RSS output
let updateTitle;
let updateURL;
let updateContent;
let updateDate;
// Setting last update variable
let lastUpdate;
function getLastUpdate() {
// Getting date of last update from check-update.txt file
let dateFromFile = fs.readFileSync('check-update.txt');
lastUpdate = dateFromFile.toString();
}
// Getting last date from check-update.txt file every 5 seconds
setInterval(getLastUpdate, 5000);
// Create Discord Bot Instance
const bot = new Client();
// Logging into Bot Account
bot.on('ready', () => {
log.info('discord', `Logged in as ${bot.user.tag} (User ID: ${bot.user.id}) on ${bot.guilds.size} server(s)`);
bot.user.setActivity('Fetching Updates');
// Checking every 10 seconds if there's a new update, and if it's been posted already
setInterval(getUpdate, 10000);
});
function getUpdate() {
// Get current date and edit format
let getDate = new Date();
let date = [
getDate.getFullYear(),
('0' + (getDate.getMonth() + 1)).slice(-2),
('0' + getDate.getDate()).slice(-2)
].join('-');
// Fetching updates from CS:GO Blog
(async () => {
// Setting RSS Feed URL
let feed = await rssParser.parseURL('http://blog.counter-strike.net/index.php/category/updates/feed/');
// Setting items into variables
feed.items.forEach(item => {
if (item.isoDate.includes(`${date}`)) {
updateTitle = item.title;
updateURL = item.link;
updateContent = item.contentHtml;
updateDate = item.isoDate;
}
});
// Converting from HTML to readable text
let format = htmlToText.fromString(updateContent, {
wordwrap: 130
});
// Limiting the update to 10 lines
let updateNotes = format.split('\n', 10);
if (updateDate.includes(`${date}`)) {
if (lastUpdate !== date) {
bot.channels.get(cfg.channelid).send("@everyone A new CS:GO Update has been released!", {
embed: {
"title": `${updateTitle}`,
"description": `${updateNotes.join('\n')}...\n\n[Continue reading on the CS:GO Blog](${updateURL})`,
"url": `${updateURL}`,
"color": 5478908,
"thumbnail": {
"url": "https://raw.githubusercontent.com/Triniayo/nodejs-discord-csgoupdate/master/csgo-icon.png"
}
}
});
// Storing date of the latest update in the check-update.txt
fs.writeFileSync('check-update.txt', `${date}`)
}
} else {
// Do nothing if update has been posted already.
}
})();
}
// Logging into the Bot Account
bot.login(cfg.token);
在此之下是错误,我在将nodejs&npm更新到最新版本后得到了。
(node:23692) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'includes' of undefined
at /home/mikkel/bots/csgo/app.js:87:24
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
(node:23692) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 13)
我不知道,这个错误是什么意思,因为我不喜欢nodejs。
npm / node的版本:
node: v8.16.1
npm: 6.12.0
我必须更改什么?我对javascript或nodejs没有任何了解。该脚本可自由放置在GitHub上。
更新:已编辑10/10/2019-15:28 : 日期格式如何? -示例;在github上,它从check-update.txt读取,格式为:10/23/2018(例如)
似乎它不适合我在Discord上发布。 (如果不这样做,应该这样做)
示例:
// Checking every 10 seconds if there's a new update, and if it's been posted already
setInterval(getUpdate, 10000);
问题出在这里
// Do nothing if update has been posted already.
}
})();
}
也许我应该从不和谐的开发商那里创建一个新的机器人?
答案 0 :(得分:0)
更改它。向http
请求https
到isoDate
的URL。 feedD中没有isoDate。
pubDate
答案 1 :(得分:0)
您发布的错误内容如下:
(node:23692) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'includes' of undefined
at /home/mikkel/bots/csgo/app.js:87:24
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
错误消息的第一行表示正在尝试调用includes
或获取对象的includes
属性,但是该对象未定义。
错误消息的第二行告诉您问题出在哪里;第87行(第24列)。
第87行显示:
if (updateDate.includes(`${date}`)) {
因此,updateDate
对象必须以某种方式未定义。通过浏览,我们看到它仅在第70行的forEach循环中设置,但在该循环内的if语句中。
feed.items.forEach(item => {
if (item.isoDate.includes(`${date}`)) {
updateTitle = item.title;
updateURL = item.link;
updateContent = item.contentHtml;
updateDate = item.isoDate;
}
});
如果循环永远不会运行,那么将永远不会设置updateDate
。如果循环中的每个项目都未在其isoDate
属性中包含日期,那么将永远不会设置updateDate
。
所以我们必须谨防这些情况。
我们可以通过将第87行更改为updateDate
来添加一个条件:
if (updateDate && updateDate.includes(`${date}`)) {