diff --git a/README.md b/README.md index 1d3a18a..faa0ffa 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,11 @@ Please create an issue if something wrong happens. ## Current Features :statue_of_liberty: -- Grab a recent post from the [r/Megumin](https://www.reddit.com/r/Megumin/) subreddit +- Grab a recent post from the [r/Megumin](https://www.reddit.com/r/Megumin/) subreddit _NOTE: Randomly selected from the 50 most recent posts_ - Using the selenium module for python to extract quotes from [animecharactersdatabase](https://www.animecharactersdatabase.com) - Being able to set up a custom prefix so it doesn't potentially interfere with other bots. _NOTE: Only works with select ASCII characters. The alphabet, punctuation, and numbers._ - Custom prefix has character limit of 20 +- If you are hosting the bot, there is a log of all messages that go through the channel/guild - Other small stuff ## Languages :speech_balloon: @@ -29,7 +30,7 @@ Please create an issue if something wrong happens. - js - python -## Resources :books: +## Resources/Dependencies :books: - Node.JS (v8.11.3) [https://nodejs.org/en/] - discord.js v(11.3.2) [https://discord.js.org/#/] @@ -38,6 +39,7 @@ Please create an issue if something wrong happens. - µWS (v10.148.1) [https://www.npmjs.com/package/uuws] - ChromeDriver (v2.40) [http://chromedriver.chromium.org/downloads] - Selenium (v3.13.0) [https://www.seleniumhq.org/projects/webdriver/] +- is-image-url (v1.1.8) [https://github.com/wzbg/is-image-url] - reddit [https://www.reddit.com/r/Megumin/] diff --git a/package-lock.json b/package-lock.json index 6a52354..414c90f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,11 +4,21 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "asap": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz", + "integrity": "sha1-5QNHYR1+aQlDIIu9r+vLwvuGbUY=" + }, "async-limiter": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz", "integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==" }, + "buffer-from": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", + "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==" + }, "bufferutil": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.0.tgz", @@ -17,6 +27,27 @@ "node-gyp-build": "3.4.0" } }, + "caseless": { + "version": "0.11.0", + "resolved": "https://registry.npmjs.org/caseless/-/caseless-0.11.0.tgz", + "integrity": "sha1-cVuW6phBWTzDMGeSP17GDr2k99c=" + }, + "concat-stream": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", + "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", + "requires": { + "buffer-from": "1.1.1", + "inherits": "2.0.3", + "readable-stream": "2.3.6", + "typedarray": "0.0.6" + } + }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, "discord.js": { "version": "11.3.2", "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-11.3.2.tgz", @@ -29,6 +60,59 @@ "ws": "4.1.0" } }, + "http-basic": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/http-basic/-/http-basic-2.5.1.tgz", + "integrity": "sha1-jORHvbW2xXf4pj4/p4BW7Eu02/s=", + "requires": { + "caseless": "0.11.0", + "concat-stream": "1.6.2", + "http-response-object": "1.1.0" + } + }, + "http-response-object": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/http-response-object/-/http-response-object-1.1.0.tgz", + "integrity": "sha1-p8TnWq6C87tJBOT0P2FWc7TVGMM=" + }, + "image-extensions": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/image-extensions/-/image-extensions-1.1.0.tgz", + "integrity": "sha1-uOa/YDnfAFbjM1AqALZjejEF2JQ=" + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "is-image": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-image/-/is-image-1.0.1.tgz", + "integrity": "sha1-b9UadSoaERUG0GDZUhGLC5ibQm4=", + "requires": { + "image-extensions": "1.1.0" + } + }, + "is-image-url": { + "version": "1.1.8", + "resolved": "https://registry.npmjs.org/is-image-url/-/is-image-url-1.1.8.tgz", + "integrity": "sha1-qmK/l1fFvlQCJpmcdMOiGtqYuDw=", + "requires": { + "is-image": "1.0.1", + "is-url": "1.2.4", + "sync-request": "2.2.0" + } + }, + "is-url": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/is-url/-/is-url-1.2.4.tgz", + "integrity": "sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww==" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, "long": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", @@ -44,11 +128,48 @@ "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-3.4.0.tgz", "integrity": "sha512-YoviGBJYGrPdLOKDIQB0sKxuKy/EEsxzooNkOZak4vSTKT/qH0Pa6dj3t1MJjEQGsefih61IyHDmO1WW7xOFfw==" }, + "os-shim": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/os-shim/-/os-shim-0.1.3.tgz", + "integrity": "sha1-a2LDeRz3kJ6jXtRuF2WLtBfLORc=" + }, "prism-media": { "version": "0.0.2", "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-0.0.2.tgz", "integrity": "sha512-L6yc8P5NVG35ivzvfI7bcTYzqFV+K8gTfX9YaJbmIFfMXTs71RMnAupvTQPTCteGsiOy9QcNLkQyWjAafY/hCQ==" }, + "process-nextick-args": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz", + "integrity": "sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==" + }, + "promise": { + "version": "7.3.1", + "resolved": "https://registry.npmjs.org/promise/-/promise-7.3.1.tgz", + "integrity": "sha512-nolQXZ/4L+bP/UGlkfaIujX9BKxGwmQ9OT4mOt5yvy8iK1h3wqTEJCijzGANTCCl9nWjY41juyAn2K3Q1hLLTg==", + "requires": { + "asap": "2.0.6" + } + }, + "qs": { + "version": "6.5.2", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", + "integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==" + }, + "readable-stream": { + "version": "2.3.6", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", + "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", + "requires": { + "core-util-is": "1.0.2", + "inherits": "2.0.3", + "isarray": "1.0.0", + "process-nextick-args": "2.0.0", + "safe-buffer": "5.1.2", + "string_decoder": "1.1.1", + "util-deprecate": "1.0.2" + } + }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", @@ -59,11 +180,62 @@ "resolved": "https://registry.npmjs.org/snekfetch/-/snekfetch-3.6.4.tgz", "integrity": "sha512-NjxjITIj04Ffqid5lqr7XdgwM7X61c/Dns073Ly170bPQHLm6jkmelye/eglS++1nfTWktpP6Y2bFXjdPlQqdw==" }, + "spawn-sync": { + "version": "1.0.15", + "resolved": "https://registry.npmjs.org/spawn-sync/-/spawn-sync-1.0.15.tgz", + "integrity": "sha1-sAeZVX63+wyDdsKdROih6mfldHY=", + "requires": { + "concat-stream": "1.6.2", + "os-shim": "0.1.3" + } + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "5.1.2" + } + }, + "sync-request": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/sync-request/-/sync-request-2.2.0.tgz", + "integrity": "sha1-p70sES+glGPrkUnP8OnUKMR5do8=", + "requires": { + "concat-stream": "1.6.2", + "http-response-object": "1.1.0", + "spawn-sync": "1.0.15", + "then-request": "2.2.0" + } + }, + "then-request": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/then-request/-/then-request-2.2.0.tgz", + "integrity": "sha1-ZnizL6DKIY/laZgbvYhxtZQGDYE=", + "requires": { + "caseless": "0.11.0", + "concat-stream": "1.6.2", + "http-basic": "2.5.1", + "http-response-object": "1.1.0", + "promise": "7.3.1", + "qs": "6.5.2" + } + }, "tweetnacl": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.0.tgz", "integrity": "sha1-cT2LgY2kIGh0C/aDhtBHnmb8ins=" }, + "typedarray": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", + "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=" + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + }, "uws": { "version": "10.148.1", "resolved": "https://registry.npmjs.org/uws/-/uws-10.148.1.tgz", diff --git a/package.json b/package.json index 8ba8e0b..a21ce89 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,7 @@ { "name": "megubot", "version": "1.0.0", - "description": - "A discord bot inspired by the amazing Arch Wizard of the Crimson Wizard Clan, Megumin! From the anime, Konosuba.", + "description": "A discord bot inspired by the amazing Arch Wizard of the Crimson Wizard Clan, Megumin! From the anime, Konosuba.", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" @@ -12,6 +11,7 @@ "dependencies": { "bufferutil": "^4.0.0", "discord.js": "^11.3.2", + "is-image-url": "^1.1.8", "node-fetch": "^2.1.2", "uws": "^10.148.1" }, diff --git a/src/index.js b/src/index.js index 30a9f20..9fcc2dc 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,7 @@ const explosionQuotes = require(path.join( )); const bot = new Discord.Client({ disableEveryone: true }); const fetch = require('node-fetch'); +const isImageUrl = require('is-image-url'); // UPDATE 8/2/2018 // Found out that people could use Cryllic characters to set the bot prefix and other people couldn't use the bot @@ -34,7 +35,41 @@ let botconfigRaw = fs.readFileSync( ); let botconfig = JSON.parse(botconfigRaw); -// Lete the person hosting the bot know when the bot is online +// Responsible for getting the image link and validating it when command img is called +const getImgLink = message => { + fetch('https://www.reddit.com/r/megumin.json?limit=55') + .then(res => res.json()) + .then(res => res.data.children) + .then(res => + res.map(post => ({ + author: post.data.author, + img: post.data.url, + title: post.data.title, + permlink: post.data.permalink + })) + ) + .then(res => { + // Checks if the image link is actually an image since some reddit posts include external links/gifs + let temp = Math.floor(Math.random() * 51); + console.log(res[temp].img + ' ' + temp); + while (isImageUrl(res[temp].img) === false) { + console.log(res[temp].img + 'is not an image'); + temp = Math.floor(Math.random() * 51); + } + message.channel.send({ + embed: { + title: res[temp].title, + url: 'https://www.reddit.com' + res[temp].permlink, + description: `Post by: ${res[temp].author}`, + color: 16077395, + image: { + url: res[temp].img + } + } + }); + }); +}; +// Lets the person hosting the bot know when the bot is online bot.on('ready', async () => { console.log(` ███╗ ███╗███████╗ ██████╗ ██╗ ██╗ ██████╗ ██████╗ ████████╗ @@ -65,7 +100,9 @@ bot.on('message', async message => { // Sets all characters messageArray = messageArray.map(text => text.toLowerCase()); - console.log(message.content); + console.log( + `${message.content} || ${message.author.username} || ${message.guild}` + ); // Talking to the bot in dm's won't work, also it prevents the bot from // potentially activating itself @@ -113,31 +150,7 @@ bot.on('message', async message => { // Gets an image from reddit r/megumin and posts it in an embed case 'img': - fetch('https://www.reddit.com/r/megumin.json?limit=50') - .then(res => res.json()) - .then(res => res.data.children) - .then(res => - res.map(post => ({ - author: post.data.author, - img: post.data.url, - title: post.data.title, - permlink: post.data.permalink - })) - ) - .then(res => { - let temp = Math.floor(Math.random() * 46); - message.channel.send({ - embed: { - title: res[temp].title, - url: 'https://www.reddit.com' + res[temp].permlink, - description: `Post by: ${res[temp].author}`, - color: 16077395, - image: { - url: res[temp].img - } - } - }); - }); + getImgLink(message); break; // Requests the bot for a list of commands and how to use them @@ -168,7 +181,7 @@ bot.on('message', async message => { { name: 'img', value: - 'Shows a random picture from the 40 most recent posts from r/Megumin! \nMay include **NSFW** pics :wink:.' + 'Shows a random picture from the 50 most recent posts from r/Megumin! \nMay include **NSFW** pics :wink:.' }, { name: 'hello',