From 152c04acd160570d443e7147eed6884b895cd70d Mon Sep 17 00:00:00 2001 From: Mental <73140396+MaximKing1@users.noreply.github.com> Date: Mon, 22 Feb 2021 11:27:17 +0000 Subject: [PATCH] Latest Changes Massive Changes! --- README.md | 187 ++++++++++++++++++++----------- src/Client.js | 304 ++++++++++++++++++++++++++++---------------------- 2 files changed, 287 insertions(+), 204 deletions(-) diff --git a/README.md b/README.md index ee4d1d7..93ceb0f 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,8 @@ BLWebhooks fully supports external and discord.js internal sharding, make sure t Please do remember this is in beta, before writing a negative review please open a issue first so we can have a chance to resolve the issue. -**NPM (recommended)** +## NPM (recommended) + ```js # Stable npm i --save blwebhooks @@ -42,7 +43,8 @@ npm i --save blwebhooks npm i --save blwebhooks@nightly ``` -**Yarn** +## Yarn + ```js # Stable yarn add blwebhooks @@ -53,10 +55,10 @@ yarn add blwebhooks@nightly # Supported Libraries -| Name | Features | -| --------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | +| Name | Features | +| ------------------------------------------------------ | ------------------------------------------ | | [discord.js](https://www.npmjs.com/package/discord.js) | `DATABASE`, `EXPRESS`, `HOOKS`, `SECURITY` | -| [Eris](https://www.npmjs.com/package/eris) | `EXPRESS`, `HOOKS`, `SECURITY` | +| [Eris](https://www.npmjs.com/package/eris) | `EXPRESS`, `HOOKS`, `SECURITY` | # Supported Lists @@ -64,13 +66,14 @@ yarn add blwebhooks@nightly > InfinityBotList
> VoidBots
> DiscordLabs
-> Botrix
+> Botrix - Coming Soon!
> Blist
> DiscordBots.co
# Usage -**Import the lib via ES6 or commonJS modules** +### Import the lib via ES6 or commonJS modules + ```js // ES6 import * as blwebhooks from "blwebhooks"; @@ -79,68 +82,102 @@ const blwebhooks = require("blwebhooks"); ``` ### With discord.js + ```js const discord = require("discord.js"); const client = discord.Client(); const { WebhooksManager } = require("blwebhooks"); -const voteClient = new WebhooksManager(client, 80); +const voteClient = new WebhooksManager(client, 80, { + database: "none", // mongoose or sqlite + string: "MongooseURL", // Only Use This If The Database Is Set To Mongoose + extra: { + extraLogging: false, // This will enable extraLogging {Debugging} + extraProtection: true, // Leave Enabled Unless Using Small Amount Of RAM + proxyTrust: false, // Enable this if your behind a proxy, Heroku, + }, +}); client.voteManager = voteClient; ``` ### With Eris + ```js const Eris = require("eris"); - + var bot = new Eris("BOT_TOKEN"); // Replace BOT_TOKEN with your bot account's token - -bot.on("ready", () => { // When the bot is ready - console.log("Ready!"); // Log "Ready!" + +bot.on("ready", () => { + // When the bot is ready + console.log("Ready!"); // Log "Ready!" }); - + const { WebhooksManager } = require("blwebhooks"); -const voteClient = new WebhooksManager(bot, 80); +const voteClient = new WebhooksManager(bot, 80, { + database: "none", // mongoose or sqlite + string: "MongooseURL", // Only Use This If The Database Is Set To Mongoose + extra: { + extraLogging: false, // This will enable extraLogging {Debugging} + extraProtection: true, // Leave Enabled Unless Using Small Amount Of RAM + proxyTrust: false, // Enable this if your behind a proxy, Heroku, + }, +}); bot.voteManager = voteClient; bot.connect(); // Get the bot to connect to Discord ``` -**Vote's Storage** -```js -// The DB can be set to either mongo, sqlite -// Only set the string if using the mongoose db -voteClient.setStroage(DB, String); - -// MongooseDB Example (recommended) -voteClient.setStroage("mongo", "mongodb://localhost/my_database"); +### Vote Storage -// SQLITE Example -voteClient.setStroage("sqlite"); +```js +// Set the vote storage in the voteClient +const voteClient = new WebhooksManager(client, 80, { + database: "mongoose", // mongoose or sqlite + string: "mongooseDB-URL", + extra: { + extraLogging: false, + extraProtection: true, + proxyTrust: false, + }, +}); ``` + For usage on pulling data see the [Database Vote](#database-vote) Section. -**Turn On Sharding Support** -```js -voteClient.shardedClient(true); -``` +### Extended Security -**Turn On Extended Security** ```js -// This will enable bruteforce protection for module, once enabled -// it will start the protection also using more CPU. -voteClient.extraProtection(true); +// You can enable this via the voteClient +const voteClient = new WebhooksManager(client, 80, { + database: "mongoose", + string: "mongooseDB-URL", + extra: { + extraLogging: false, + extraProtection: true, // Enable this here + proxyTrust: false, + }, +}); ``` -**Turn On Proxy Trust** +### Proxy Trust + ```js -// Enable this option is you use this behind a proxy like -// Heroku services etc -voteClient.proxyTrust(true); +// Enable this in the voteClient +const voteClient = new WebhooksManager(client, 80, { + database: "mongoose", + string: "mongooseDB-URL", + extra: { + extraLogging: false, + extraProtection: true, + proxyTrust: true, // Enable it here + }, +}); ``` -**Emit Test Vote Events** +### Test Events + ```js // Emit a test event to test your Voted Event voteClient.testVote(userID, botID); @@ -149,6 +186,7 @@ voteClient.testVote(userID, botID); # Vote Hooks **TopGG Vote Hooks:** + ```js // This will listen to votes from top.gg, the url is the end not // including the / and auth is the webhook auth. You can enable and @@ -156,12 +194,13 @@ voteClient.testVote(userID, botID); client.voteManager.topggVoteHook(url, auth, true); // This code will run after a new vote was received from top.gg -client.on('topgg-voted', async function (userID, botID, type) { - console.log(userID) -}) +client.on("topgg-voted", async function (userID, botID, type) { + console.log(userID); +}); ``` **InfinityBotList Vote Hooks:** + ```js // This will listen to votes from InfinityBotList, the url is the end not // including the / and auth is the webhook auth. You can enable and @@ -169,12 +208,13 @@ client.on('topgg-voted', async function (userID, botID, type) { client.voteManager.IBLVoteHook(url, auth, true); // This code will run after a new vote was received from InfinityBotList -client.on('IBL-voted', async function (userID, botID, type) { - console.log(userID) -}) +client.on("IBL-voted", async function (userID, botID, type) { + console.log(userID); +}); ``` **VoidBots Vote Hooks:** + ```js // This will listen to votes from VoidBots, the url is the end not // including the / and auth is the webhook auth. You can enable and @@ -182,12 +222,13 @@ client.on('IBL-voted', async function (userID, botID, type) { client.voteManager.VoidBotsVoteHook(url, auth, true); // This code will run after a new vote was received from VoidBots -client.on('VB-voted', async function (userID, botID) { - console.log(userID + " Voted For " + botID) -}) +client.on("VB-voted", async function (userID, botID) { + console.log(userID + " Voted For " + botID); +}); ``` **DiscordLabs Vote Hooks:** + ```js // This will listen to votes from DiscordLabs, the url is the end not // including the / and auth is the webhook auth. You can enable and @@ -195,12 +236,13 @@ client.on('VB-voted', async function (userID, botID) { client.voteManager.DiscordLabsVoteHook(url, auth, true); // This code will run after a new vote was received from DiscordLabs -client.on('DL-voted', async function (userID, botID, wasTest) { - console.log(`${userID} Voted For ${botID}. Was Test: ${wasTest}`) -}) +client.on("DL-voted", async function (userID, botID, wasTest) { + console.log(`${userID} Voted For ${botID}. Was Test: ${wasTest}`); +}); ``` **Botrix Vote Hooks:** + ```js // This will listen to votes from Botrix, the url is the end not // including the / and auth is the webhook auth. You can enable and @@ -208,12 +250,13 @@ client.on('DL-voted', async function (userID, botID, wasTest) { client.voteManager.BotrixVoteHook(url, auth, true); // This code will run after a new vote was received from Botrix -client.on('BTR-voted', async function (userID, botID) { - console.log(`${userID} Voted For ${botID}.`) -}) +client.on("BTR-voted", async function (userID, botID) { + console.log(`${userID} Voted For ${botID}.`); +}); ``` **BList Vote Hooks:** + ```js // This will listen to votes from BList, the url is the end not // including the / and auth is the webhook auth. You can enable and @@ -221,12 +264,13 @@ client.on('BTR-voted', async function (userID, botID) { client.voteManager.BListVoteHook(url, auth, true); // This code will run after a new vote was received from BList -client.on('BLT-voted', async function (userID, botID) { - console.log(`${userID} Voted.`) -}) +client.on("BLT-voted", async function (userID, botID) { + console.log(`${userID} Voted.`); +}); ``` **DiscordBots.co Vote Hooks:** + ```js // This will listen to votes from BList, the url is the end not // including the / and auth is the webhook auth. You can enable and @@ -234,41 +278,46 @@ client.on('BLT-voted', async function (userID, botID) { client.voteManager.DBCVoteHook(url, auth, true); // This code will run after a new vote was received from BList -client.on('DBC-voted', async function (userID, botID) { - console.log(`${userID} Voted.`) -}) +client.on("DBC-voted", async function (userID, botID) { + console.log(`${userID} Voted.`); +}); ``` # Events **Global Voted Event** + ```js // This will run after any vote has been received, use this if you don't want seprate events for each list -client.on('vote', async function (userID, botID, List) { - console.log(`${userID} Voted For ${botID} on ${List}`) -}) +client.on("vote", async function (userID, botID, List) { + console.log(`${userID} Voted For ${botID} on ${List}`); +}); ``` **Vote Expired Event** + ```js // This will run after the vote expires, this will be different for each list -client.on('vote-expired', async function (userID, botID, List) { - console.log(userID) -}) +client.on("vote-expired", async function (userID, botID, List) { + console.log(userID); +}); ``` **Error Event** + ```js // This code will run in the event of a error, normally it will just console.log the error but you can add custom error events here -client.on('webhookError', async function (error) { - console.log(userID) -}) +client.on("webhookError", async function (error) { + console.log(userID); +}); ``` # Vote Database + The vote Database feature will save the total votes a User had made for your bot / server, it will add all the vote in a database and the methods below show you how to set it up and pull data. **discord.js example** + ```js // The DB can be set to either mongo, sqlite // Only set the string if using the mongoose db @@ -282,10 +331,12 @@ voteClient.setStroage("sqlite"); ``` **Pulling User Votes** + ```js voteClient.getVotes(userID, option); ``` -*Options:*
+ +_Options:_
- `daily` - Get Daily Votes
- `weekly` - Get Weekly Votes
diff --git a/src/Client.js b/src/Client.js index ea95e90..c53722b 100644 --- a/src/Client.js +++ b/src/Client.js @@ -34,7 +34,9 @@ class WebhooksManager extends EventEmitter { constructor(client, port, options, init = true) { super(); console.log(chalk.red("-----------------------")); - console.log("All Manager Options Updated Check Our Docs Now! https://github.com/MaximKing1/BLWebhooks#readme"); + console.log( + "All Manager Options Updated Check Our Docs Now! https://github.com/MaximKing1/BLWebhooks#readme" + ); console.log(chalk.red("----------------------")); /** @@ -70,7 +72,11 @@ class WebhooksManager extends EventEmitter { app.use(limiter); app.use(speedLimiter); app.use(cookieParser()); - console.log(chalk.green(`[BLWEBHOOKS] The Vote Webserver Has Started On Port ${port}.`)); + console.log( + chalk.green( + `[BLWEBHOOKS] The Vote Webserver Has Started On Port ${port}.` + ) + ); } if (init) this._init(); } @@ -185,14 +191,12 @@ class WebhooksManager extends EventEmitter { 1000 * 60 * 60 * 24 ); - res - .status(200) - .send( - JSON.stringify({ - error: false, - message: "[BLWEBHOOKS] Received The Request!", - }) - ); + res.status(200).send( + JSON.stringify({ + error: false, + message: "[BLWEBHOOKS] Received The Request!", + }) + ); }); } @@ -214,15 +218,13 @@ class WebhooksManager extends EventEmitter { if (req.header("Authorization") != auth) console.log("Failed Access - InfinityBotList Endpoint"); if (req.header("Authorization") != auth) - return res - .status(403) - .send( - JSON.stringify({ - error: true, - message: - "[BLWEBHOOKS] You don't have access to use this endpoint. - InfinityBotList", - }) - ); + return res.status(403).send( + JSON.stringify({ + error: true, + message: + "[BLWEBHOOKS] You don't have access to use this endpoint. - InfinityBotList", + }) + ); // Use the data on whatever you want console.log(req.body); @@ -239,14 +241,12 @@ class WebhooksManager extends EventEmitter { ); // Respond to IBL API - res - .status(200) - .send( - JSON.stringify({ - error: false, - message: "[BLWEBHOOKS] Received the request!", - }) - ); + res.status(200).send( + JSON.stringify({ + error: false, + message: "[BLWEBHOOKS] Received the request!", + }) + ); }); } @@ -266,15 +266,13 @@ class WebhooksManager extends EventEmitter { if (req.header("Authorization") != auth) console.log("Failed Access - VoidBots Endpoint"); if (req.header("Authorization") != auth) - return res - .status(403) - .send( - JSON.stringify({ - error: true, - message: - "[BLWEBHOOKS] You don't have access to this endpoint - VoidBots", - }) - ); + return res.status(403).send( + JSON.stringify({ + error: true, + message: + "[BLWEBHOOKS] You don't have access to this endpoint - VoidBots", + }) + ); // Use the data on whatever you want console.log(req.body); @@ -290,14 +288,12 @@ class WebhooksManager extends EventEmitter { ); // Respond to VoidBots API - res - .status(200) - .send( - JSON.stringify({ - error: false, - message: "[BLWEBHOOKS] Received the request!", - }) - ); + res.status(200).send( + JSON.stringify({ + error: false, + message: "[BLWEBHOOKS] Received the request!", + }) + ); }); } @@ -315,15 +311,13 @@ class WebhooksManager extends EventEmitter { if (req.header("Authorization") != auth) console.log("Failed Access - DiscordLabs Endpoint"); if (req.header("Authorization") != auth) - return res - .status(403) - .send( - JSON.stringify({ - error: true, - message: - "[BLWEBHOOKS] You don't have access to use this endpoint - DiscordLabs", - }) - ); + return res.status(403).send( + JSON.stringify({ + error: true, + message: + "[BLWEBHOOKS] You don't have access to use this endpoint - DiscordLabs", + }) + ); // Use the data on whatever you want console.log(req.body); @@ -340,14 +334,12 @@ class WebhooksManager extends EventEmitter { ); // Respond to DiscordLabs API - res - .status(200) - .send( - JSON.stringify({ - error: false, - message: "[BLWEBHOOKS] Received the request!", - }) - ); + res.status(200).send( + JSON.stringify({ + error: false, + message: "[BLWEBHOOKS] Received the request!", + }) + ); }); } @@ -367,15 +359,13 @@ class WebhooksManager extends EventEmitter { if (req.header("Authorization") != auth) console.log("Failed Access - Botrix Endpoint"); if (req.header("Authorization") != auth) - return res - .status(403) - .send( - JSON.stringify({ - error: true, - message: - "[BLWEBHOOKS] You don't have access to use this endpoint. - Botrix", - }) - ); + return res.status(403).send( + JSON.stringify({ + error: true, + message: + "[BLWEBHOOKS] You don't have access to use this endpoint. - Botrix", + }) + ); // Use the data on whatever you want console.log(req.body); @@ -391,14 +381,12 @@ class WebhooksManager extends EventEmitter { ); // Respond to Botrix API - res - .status(200) - .send( - JSON.stringify({ - error: false, - message: "[BLWEBHOOKS] Received the request!", - }) - ); + res.status(200).send( + JSON.stringify({ + error: false, + message: "[BLWEBHOOKS] Received the request!", + }) + ); }); } @@ -418,15 +406,13 @@ class WebhooksManager extends EventEmitter { if (req.header("Authorization") != auth) console.log("Failed Access - BList Endpoint"); if (req.header("Authorization") != auth) - return res - .status(403) - .send( - JSON.stringify({ - error: true, - message: - "[BLWEBHOOKS] You don't have access to use this endpoint - BList", - }) - ); + return res.status(403).send( + JSON.stringify({ + error: true, + message: + "[BLWEBHOOKS] You don't have access to use this endpoint - BList", + }) + ); // Use the data on whatever you want console.log(req.body); @@ -442,14 +428,12 @@ class WebhooksManager extends EventEmitter { ); // Respond to BList API - res - .status(200) - .send( - JSON.stringify({ - error: false, - message: "[BLWEBHOOKS] Received the request!", - }) - ); + res.status(200).send( + JSON.stringify({ + error: false, + message: "[BLWEBHOOKS] Received the request!", + }) + ); }); } @@ -469,15 +453,13 @@ class WebhooksManager extends EventEmitter { if (req.header("Authorization") != auth) console.log("Failed Access - Mythicalbots Endpoint"); if (req.header("Authorization") != auth) - return res - .status(403) - .send( - JSON.stringify({ - error: true, - message: - "[BLWEBHOOKS] You don't have access to use this endpoint - Mythicalbots", - }) - ); + return res.status(403).send( + JSON.stringify({ + error: true, + message: + "[BLWEBHOOKS] You don't have access to use this endpoint - Mythicalbots", + }) + ); // Use the data on whatever you want console.log(req.body); @@ -493,14 +475,12 @@ class WebhooksManager extends EventEmitter { ); // Respond to Mythicalbots API - res - .status(200) - .send( - JSON.stringify({ - error: false, - message: "[BLWEBHOOKS] Received the request!", - }) - ); + res.status(200).send( + JSON.stringify({ + error: false, + message: "[BLWEBHOOKS] Received the request!", + }) + ); }); } @@ -520,15 +500,13 @@ class WebhooksManager extends EventEmitter { if (req.header("Authorization") != auth) console.log("Failed Access - DiscordBots.co Endpoint"); if (req.header("Authorization") != auth) - return res - .status(403) - .send( - JSON.stringify({ - error: true, - message: - "[BLWEBHOOKS] You don't have access to use this endpoint - DiscordBots.co", - }) - ); + return res.status(403).send( + JSON.stringify({ + error: true, + message: + "[BLWEBHOOKS] You don't have access to use this endpoint - DiscordBots.co", + }) + ); // Use the data on whatever you want console.log(req.body); @@ -544,14 +522,12 @@ class WebhooksManager extends EventEmitter { ); // Respond to BList API - res - .status(200) - .send( - JSON.stringify({ - error: false, - message: "[BLWEBHOOKS] Received the request!", - }) - ); + res.status(200).send( + JSON.stringify({ + error: false, + message: "[BLWEBHOOKS] Received the request!", + }) + ); }); } @@ -567,15 +543,71 @@ class WebhooksManager extends EventEmitter { } } - /** - * Inits the manager - * @ignore - * @private - */ - async _init() { - this.ready = true; + /** + * Inits the manager + * @ignore + * @private + */ + async _init() { + if (this.options.extra.proxyTrust == true) { + console.log(chalk.green("[BLWEBHOOKS] Proxy trust enabled.")); + return app.enable("trust proxy"); + } else if (this.options.extra.proxyTrust == false) { + console.log(chalk.red("[BLWEBHOOKS] Proxy trust disabled.")); + } // Enable this if your behind a proxy, Heroku, Docker, Replit, etc + + if (this.options.extra.shardedClient == true) { + console.log( + chalk.green("[BLWEBHOOKS] Sharding client has been enabled.") + ); + } else if (this.options.extra.shardedClient == false) { + console.log(chalk.red("[BLWEBHOOKS] Sharding client has been disabled.")); } + if (this.options.extraLogging == true) { + console.log(chalk.green("[BLWEBHOOKS] Advanced logging enabled.")); + return app.use(errorhandler()); + } else if (this.options.extraLogging == false) { + console.log(chalk.red("[BLWEBHOOKS] Advance logging disabled")); + } + + if (this.options.database == "mongo") { + console.log(chalk.yellow("[BLWEBHOOKS] Enabled mongoose database.")); + mongoose.connect(this.options.string, { + useNewUrlParser: true, + useUnifiedTopology: true, + useFindAndModify: false, + useCreateIndex: true, + }); + } else if (this.options.database == "sqlite") { + var sqlite3 = require("sqlite3").verbose(); + let db = new sqlite3.Database("voteHooks.db", async (err) => { + if (err) { + console.error(chalk.red(err.message)); + } + console.log(chalk.yellow("[BLWEBHOOKS] Enabled SQLITE database.")); + console.log( + chalk.yellow("[BLWEBHOOKS] Connected to the voteHooks.db database.") + ); + }); + } else if (this.options.database == "none") { + console.log(chalk.red("Database Disabled")); + } + + if (this.options.extra.extraProtection == true) { + console.log(chalk.green("[BLWEBHOOKS] Extra protection enabled.")); + return app.use( + helmet({ + contentSecurityPolicy: false, + permittedCrossDomainPolicies: false, + }) + ); + } else if (this.options.extra.extraProtection == false) { + console.log(chalk.red("[BLWEBHOOKS] Extra protection disabled.")); + } + + this.ready = true; + } } module.exports.WebhooksManager = WebhooksManager;