Browse Source

Separate DB into external file; add RSS feed poller

denikson 6 years ago
parent
commit
d8a6afc776
4 changed files with 103 additions and 78 deletions
  1. 2 1
      .eslintrc.js
  2. 73 0
      db.js
  3. 26 77
      main.js
  4. 2 0
      package.json

+ 2 - 1
.eslintrc.js

@@ -6,7 +6,8 @@ module.exports = {
     },
     "extends": "eslint:recommended",
     "parserOptions": {
-        "sourceType": "module"
+        "sourceType": "module",
+        "ecmaVersion": 2017
     },
     "rules": {
         "indent": [

+ 73 - 0
db.js

@@ -0,0 +1,73 @@
+const lowdb = require("lowdb");
+const FileSync = require("lowdb/adapters/FileSync");
+
+const adapter = new FileSync(__dirname + "/db.json");
+const db = lowdb(adapter);
+
+db._.mixin({
+    randomElement: array => array[Math.floor(Math.random() * array.length)]
+});
+
+db.defaults({
+    emotes: {
+        angery: [
+            "488797492352385034",
+            "488797455899688971",
+            "488796668750200842",
+            "488796396175097876",
+            "488793566982963231",
+            "488793511181811749",
+            "488791172085448718",
+            "489091926474096651"
+        ],
+        hug: [
+            "489094486132260864",
+            "489094428410249218",
+            "489094553148850176",
+            "489094604935790622",
+            "485109695846023209",
+            "490234326210969624",
+            "490234276676239370",
+            "490234106370588681",
+            "490234821696815125"
+        ],
+        big: [
+            "489773326386855937",
+            "489771762351996928",
+            "489771390279483394",
+            "489773282036547599"
+        ],
+        ded: [
+            "490234399351373825",
+            "490234360558125057"
+        ]
+    },
+    specialUsers: [
+        "141880968800763905",
+        "307897683849510912",
+        "335898304472678402"
+    ],
+    reactableMentionedUsers: ["307897683849510912"],
+    bigUsers : ["432821242366656512"],
+    dedUsers: ["326520875027267584"],
+    guides: [],
+    editors: {
+        roles: ["484046157971193866", "305844721622712322"],
+        users: [
+            "307897683849510912",
+            "170116346095468545",
+            "335898304472678402"
+        ]
+    },
+    rssFeeds: [
+        {
+            url: "http://custommaid3d2.com/index.php?forums/news.49/index.rss",
+            lastUpdate: ""
+        }
+    ],
+    feedOutputs: [
+        "422693157692637184"
+    ]
+}).write();
+
+module.exports = db;

+ 26 - 77
main.js

@@ -1,87 +1,35 @@
 const Discord = require("discord.js");
 const TOKEN = require("./token.js");
-const lowdb = require("lowdb");
-const FileSync = require("lowdb/adapters/FileSync");
-
-const adapter = new FileSync(__dirname + "/db.json");
-const db = lowdb(adapter);
-
-db._.mixin({
-    randomElement: array => array[Math.floor(Math.random() * array.length)]
-});
-
-//https://cdn.discordapp.com/emojis/490234360558125057.png?v=1
-
-db.defaults({
-    emotes: {
-        angery: [
-            "488797492352385034",
-            "488797455899688971",
-            "488796668750200842",
-            "488796396175097876",
-            "488793566982963231",
-            "488793511181811749",
-            "488791172085448718",
-            "489091926474096651"
-        ],
-        hug: [
-            "489094486132260864",
-            "489094428410249218",
-            "489094553148850176",
-            "489094604935790622",
-            "485109695846023209",
-            "490234326210969624",
-            "490234276676239370",
-            "490234106370588681",
-            "490234821696815125"
-        ],
-        big: [
-            "489773326386855937",
-            "489771762351996928",
-            "489771390279483394",
-            "489773282036547599"
-        ],
-        ded: [
-            "490234399351373825",
-            "490234360558125057"
-        ]
-    },
-    specialUsers: [
-        "141880968800763905",
-        "307897683849510912",
-        "335898304472678402"
-    ],
-    reactableMentionedUsers: ["307897683849510912"],
-    bigUsers : ["432821242366656512"],
-    dedUsers: ["326520875027267584"],
-    guides: [],
-    editors: {
-        roles: ["484046157971193866", "305844721622712322"],
-        users: [
-            "307897683849510912",
-            "170116346095468545",
-            "335898304472678402"
-        ]
-    }
-}).write();
+const db = require("./db.js");
+const RSSParser = require("rss-parser");
+const interval = require("interval-promise");
 
 const client = new Discord.Client();
+const parser = new RSSParser();
+const RSS_UPDATE_INTERVAL_MIN = 5;
+
+async function checkFeeds() {
+    let feeds = db.get("rssFeeds").value();
+    let outlets = db.get("feedOutputs").value();
+    for(let feedEntry of feeds) {
+        let feed = await parser.parseURL(feedEntry.url);
+        if(feed.items.length == 0)
+            continue;
+        let latestPost = feed.items[0];
+        if(latestPost.isoDate > feedEntry.lastUpdate) {
+            db.get("rssFeeds").find({ url: feedEntry.url}).assign({lastUpdate: latestPost.isoDate}).write();
+
+            outlets.forEach(ch => {
+                client.channels.get(ch).send(`**${latestPost.title}**\nPosted by ${latestPost.creator}\n${latestPost.link}`);
+            });
+        }
+    }
+}
 
 function isAuthorised(member) {
-    if (
-        db
-            .get("editors.users")
-            .includes(member.id)
-            .value()
-    )
+    if (db.get("editors.users").includes(member.id).value())
         return true;
-    if (
-        db
-            .get("editors.roles")
-            .intersectionWith(member.roles.keyArray())
-            .isEmpty()
-            .value()
-    )
+    if (db.get("editors.roles").intersectionWith(member.roles.keyArray()).isEmpty().value())
         return false;
     return true;
 }
@@ -142,6 +90,7 @@ const commands = {
 client.on("ready", () => {
     console.log("Ready!");
     client.user.setActivity("@NoctBot help", { type: "PLAYING" });
+    interval(checkFeeds, RSS_UPDATE_INTERVAL_MIN * 60 * 1000);
 });
 
 client.on("message", m => {

+ 2 - 0
package.json

@@ -21,7 +21,9 @@
   "license": "MIT",
   "dependencies": {
     "discord.js": "^11.4.2",
+    "interval-promise": "^1.2.0",
     "lowdb": "^1.0.0",
+    "rss-parser": "^3.4.3",
     "uws": "^99.0.0"
   },
   "devDependencies": {