From 8f82f384a60804865fe1dc001fd1332fc2d07a89 Mon Sep 17 00:00:00 2001 From: hayalat Date: Wed, 22 Jul 2020 13:23:49 +0300 Subject: [PATCH] added cronjob --- xi_news/main.js | 131 +++++++++++++++++++------------------- xi_news/package-lock.json | 89 ++++++++++++++++++++++++++ xi_news/package.json | 2 + 3 files changed, 158 insertions(+), 64 deletions(-) diff --git a/xi_news/main.js b/xi_news/main.js index d5b0f1b..3772b8a 100644 --- a/xi_news/main.js +++ b/xi_news/main.js @@ -1,65 +1,68 @@ const fs = require('fs') +let rssParser = require('rss-parser'); const Discord = require('discord.js'); const Client = require('./client'); const fetch = require("node-fetch"); +let feeds = { + "REDDIT": ['https://www.reddit.com/.rss', 'https://i.redd.it/rq36kl1xjxr01.png', "https://reddit.com"], + "WIRED": ['https://www.wired.com/feed/rss', "https://www.wired.com/wp-content/themes/Phoenix/assets/images/article-icon.jpg", "https://www.wired.com"] +}; +const threshold = 3; +var CronJob = require('cron').CronJob; const { prefix, - token, + token, } = require('./config.json'); - +var channels = '734387503464710165'; +async function execute() { + let channel = "734687050707632239"; + let parser = new rssParser(); + let news = [] + for (item in feeds) { + let feed = await parser.parseURL(feeds[item][0]); + for (var i = feed.items.length - 1; i > feed.items.length - 1 - threshold; i--) { + let data = feed.items[i]; + news.push({ + title: data.title, + url: data.link, + author: item, + author_img: feeds[item][1], + author_url: feeds[item][2], + content: data.contentSnippet + }); + } + }; + news.forEach((item) => { + let newsEmbed = new Discord.MessageEmbed() + .setColor('#0099ff') + .setTitle(item.title) + .setURL(item.url) + .setAuthor(item.author, item.author_img, item.author_url) + .setDescription(item.content) + .setTimestamp(); + client.channels.cache.get(`734687050707632239`).send(newsEmbed); + }); +}; const client = new Client(); client.commands = new Discord.Collection(); - const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); - for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(command.name, command); } - console.log(client.commands); - client.once('ready', () => { console.log('Ready!'); - - var minutes = 120, interval = minutes * 60 * 1000; - - async function get_news(){ - let channel = message.guild.channels.cache.get("734687050707632239"); - - let parser = new rssParser(); - let news = [] - for(item in feeds){ - let feed = await parser.parseURL(feeds[item][0]); - for(var i = feed.items.length - 1; i > feed.items.length - 1 - threshold; i --){ - let data = feed.items[i]; - - news.push({ - title: data.title, - url: data.link, - author: item, - author_img: feeds[item][1], - author_url: feeds[item][2], - content: data.contentSnippet - - }); - } - }; - - news.forEach((item) => { - let newsEmbed = new Discord.MessageEmbed() - .setColor('#0099ff') - .setTitle(item.title) - .setURL(item.url) - .setAuthor(item.author, item.author_img, item.author_url) - .setDescription(item.content) - .setTimestamp(); - channel.send(newsEmbed) - }); - } - - setInterval(delete_messages, interval); - + console.log('Before job instantiation'); + const job = new CronJob('00 21 13 * * *', function () { + client.channels.cache.get(`734387503464710165`).send(`rss time`); + execute(); + + const d = new Date(); + console.log('onTick:', d); + }); + console.log('After job instantiation'); + job.start(); }); client.once('reconnecting', () => { @@ -83,35 +86,35 @@ client.on('message', async message => { has_roles = false - if (permitted_roles){ - for(i = 0; i < permitted_roles.length; i++){ - if(message.member.roles.cache.has(permitted_roles[i])){ + if (permitted_roles) { + for (i = 0; i < permitted_roles.length; i++) { + if (message.member.roles.cache.has(permitted_roles[i])) { has_roles = true } } - if (!has_roles && permitted_roles.length != 0){ + if (!has_roles && permitted_roles.length != 0) { message.reply('You are not allowed to run this command!'); return; } } - if(permitted_channels){ - msg_channel = message.channel; - channel_allowed = false - for(i = 0; i < permitted_channels.length; i++){ - if(permitted_channels[i] == "dm" && msg_channel instanceof Discord.DMChannel){ - channel_allowed = true; - break; - }else if(permitted_channels[i] == msg_channel.id){ - channel_allowed = true; - break; - } - } + if (permitted_channels) { + msg_channel = message.channel; + channel_allowed = false + for (i = 0; i < permitted_channels.length; i++) { + if (permitted_channels[i] == "dm" && msg_channel instanceof Discord.DMChannel) { + channel_allowed = true; + break; + } else if (permitted_channels[i] == msg_channel.id) { + channel_allowed = true; + break; + } + } - if(!channel_allowed){ - return; - } + if (!channel_allowed) { + return; + } } try { diff --git a/xi_news/package-lock.json b/xi_news/package-lock.json index d18b95f..05e1243 100644 --- a/xi_news/package-lock.json +++ b/xi_news/package-lock.json @@ -4,11 +4,34 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@discordjs/collection": { + "version": "0.1.5", + "resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-0.1.5.tgz", + "integrity": "sha512-CU1q0UXQUpFNzNB7gufgoisDHP7n+T3tkqTsp3MNUkVJ5+hS3BCvME8uCXAUFlz+6T2FbTCu75A+yQ7HMKqRKw==" + }, + "@discordjs/form-data": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@discordjs/form-data/-/form-data-3.0.1.tgz", + "integrity": "sha512-ZfFsbgEXW71Rw/6EtBdrP5VxBJy4dthyC0tpQKGKmYFImlmmrykO14Za+BiIVduwjte0jXEBlhSKf0MWbFp9Eg==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, "@types/node": { "version": "14.0.23", "resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.23.tgz", "integrity": "sha512-Z4U8yDAl5TFkmYsZdFPdjeMa57NOvnaf1tljHzhouaPEp7LCj2JKkejpI1ODviIAQuW4CcQmxkQ77rnLsOOoKw==" }, + "abort-controller": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", + "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", + "requires": { + "event-target-shim": "^5.0.0" + } + }, "ajv": { "version": "6.12.3", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.3.tgz", @@ -99,6 +122,14 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" }, + "cron": { + "version": "1.8.2", + "resolved": "https://registry.npmjs.org/cron/-/cron-1.8.2.tgz", + "integrity": "sha512-Gk2c4y6xKEO8FSAUTklqtfSr7oTq0CiPQeLBG5Fl0qoXpZyMcj1SG59YL+hqq04bu6/IuEA7lMkYDAplQNKkyg==", + "requires": { + "moment-timezone": "^0.5.x" + } + }, "css-select": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/css-select/-/css-select-1.2.0.tgz", @@ -128,6 +159,21 @@ "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" }, + "discord.js": { + "version": "12.2.0", + "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-12.2.0.tgz", + "integrity": "sha512-Ueb/0SOsxXyqwvwFYFe0msMrGqH1OMqpp2Dpbplnlr4MzcRrFWwsBM9gKNZXPVBHWUKiQkwU8AihXBXIvTTSvg==", + "requires": { + "@discordjs/collection": "^0.1.5", + "@discordjs/form-data": "^3.0.1", + "abort-controller": "^3.0.0", + "node-fetch": "^2.6.0", + "prism-media": "^1.2.0", + "setimmediate": "^1.0.5", + "tweetnacl": "^1.0.3", + "ws": "^7.2.1" + } + }, "dom-serializer": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-0.1.1.tgz", @@ -173,6 +219,11 @@ "resolved": "https://registry.npmjs.org/entities/-/entities-1.1.2.tgz", "integrity": "sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==" }, + "event-target-shim": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", + "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==" + }, "extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", @@ -322,6 +373,24 @@ "mime-db": "1.44.0" } }, + "moment": { + "version": "2.27.0", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.27.0.tgz", + "integrity": "sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ==" + }, + "moment-timezone": { + "version": "0.5.31", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.31.tgz", + "integrity": "sha512-+GgHNg8xRhMXfEbv81iDtrVeTcWt0kWmTEY1XQK14dICTXnWJnT0dxdlPspwqF3keKMVPXwayEsk1DI0AA/jdA==", + "requires": { + "moment": ">= 2.9.0" + } + }, + "node-fetch": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" + }, "nth-check": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/nth-check/-/nth-check-1.0.2.tgz", @@ -348,6 +417,11 @@ "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=" }, + "prism-media": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.2.2.tgz", + "integrity": "sha512-I+nkWY212lJ500jLe4tN9tWO7nRiBAVdMv76P9kffZjYhw20raMlW1HSSvS+MLXC9MmbNZCazMrAr+5jEEgTuw==" + }, "psl": { "version": "1.8.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz", @@ -435,6 +509,11 @@ "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" }, + "setimmediate": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", + "integrity": "sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=" + }, "sshpk": { "version": "1.16.1", "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.16.1.tgz", @@ -474,6 +553,11 @@ "safe-buffer": "^5.0.1" } }, + "tweetnacl": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz", + "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==" + }, "uri-js": { "version": "4.2.2", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.2.2.tgz", @@ -502,6 +586,11 @@ "extsprintf": "^1.2.0" } }, + "ws": { + "version": "7.3.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.3.1.tgz", + "integrity": "sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==" + }, "xml2js": { "version": "0.4.23", "resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.23.tgz", diff --git a/xi_news/package.json b/xi_news/package.json index 25cdf8f..82a3509 100644 --- a/xi_news/package.json +++ b/xi_news/package.json @@ -9,6 +9,8 @@ }, "dependencies": { "cheerio": "^1.0.0-rc.3", + "cron": "^1.8.2", + "discord.js": "^12.2.0", "jquery": "^3.5.1", "request": "^2.88.2", "rss-parser": "^3.8.0"