diff --git a/bot.js b/bot.js index faec2b1..c193af5 100644 --- a/bot.js +++ b/bot.js @@ -1,17 +1,16 @@ require("dotenv").config(); -const { join } = require("path"); const Discord = require("discord.js"); const getFiles = require("./getFiles"); const remind = require("./remind"); -const addRole = require("./utils/addRole"); const verify = require("./verify"); +const user = require("./utils/usersHandlers"); const bot = new Discord.Client(); bot.commands = new Discord.Collection(); // Greet a new user -bot.on("guildMemberAdd", (member) => { +bot.on("guildMemberAdd", async (member) => { const channel = member.guild.channels.cache.find( (ch) => ch.name === "welcome" ); @@ -27,7 +26,12 @@ bot.on("guildMemberAdd", (member) => { ) .setFooter("Use !help command to know more about me "); channel.send(welcomeEmbed); - verify(bot, member); + + // adding the member to the "users" collection in DB + const exists = await user.existsInUsers(member.id); + if (exists === false) { + await user.add(member.id); + } }); bot.on("ready", async () => { @@ -45,11 +49,13 @@ bot.on("ready", async () => { setInterval(() => remind(bot), 3000000); bot.on("message", (message) => { + if (message.channel.type === "dm") return; + const args = message.content.trim().split(/\r\n|\r|\n| +/); const command = args.shift().toLowerCase(); - if (command === "!verify") { - verify(bot, message.author); + if (message.channel.id === process.env.VERIFY_CHANNEL_ID) { + if (command === "!verify") verify(bot, message.author); return; } diff --git a/models/user.model.js b/models/user.model.js new file mode 100644 index 0000000..0b1b171 --- /dev/null +++ b/models/user.model.js @@ -0,0 +1,15 @@ +const mongoose = require("mongoose"); + +const userSchema = new mongoose.Schema( + { + discordId: { type: String, required: true }, + failCount: { type: Number, required: true }, + cfHandle: { type: String, required: false }, + }, + { + timestamps: true, + collection: "users", + } +); + +module.exports = new mongoose.model("User", userSchema); diff --git a/package-lock.json b/package-lock.json index aabdb0f..fe3b895 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,7 +19,7 @@ "eslint": "^7.17.0", "eslint-config-prettier": "^7.1.0", "eslint-plugin-prettier": "^3.3.0", - "husky": "^4.3.6", + "husky": "^4.3.7", "lint-staged": "^10.5.3", "prettier": "^2.2.1" } @@ -981,28 +981,34 @@ } }, "node_modules/find-up": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", - "integrity": "sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==", + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", + "integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==", "dev": true, "dependencies": { - "locate-path": "^5.0.0", + "locate-path": "^6.0.0", "path-exists": "^4.0.0" }, "engines": { - "node": ">=8" + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/find-versions": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/find-versions/-/find-versions-3.2.0.tgz", - "integrity": "sha512-P8WRou2S+oe222TOCHitLy8zj+SIsVJh52VP4lvXkaFVnOFFdoWv1H1Jjvel1aI6NCFOAaeAVm8qrI0odiLcww==", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/find-versions/-/find-versions-4.0.0.tgz", + "integrity": "sha512-wgpWy002tA+wgmO27buH/9KzyEOQnKsG/R0yrcjPT9BOFm0zRBVQbZ95nRGXWMywS8YR5knRbpohio0bcJABxQ==", "dev": true, "dependencies": { - "semver-regex": "^2.0.0" + "semver-regex": "^3.1.2" }, "engines": { - "node": ">=6" + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/flat-cache": { @@ -1114,18 +1120,19 @@ } }, "node_modules/husky": { - "version": "4.3.6", - "resolved": "https://registry.npmjs.org/husky/-/husky-4.3.6.tgz", - "integrity": "sha512-o6UjVI8xtlWRL5395iWq9LKDyp/9TE7XMOTvIpEVzW638UcGxTmV5cfel6fsk/jbZSTlvfGVJf2svFtybcIZag==", + "version": "4.3.7", + "resolved": "https://registry.npmjs.org/husky/-/husky-4.3.7.tgz", + "integrity": "sha512-0fQlcCDq/xypoyYSJvEuzbDPHFf8ZF9IXKJxlrnvxABTSzK1VPT2RKYQKrcgJ+YD39swgoB6sbzywUqFxUiqjw==", "dev": true, + "hasInstallScript": true, "dependencies": { "chalk": "^4.0.0", "ci-info": "^2.0.0", "compare-versions": "^3.6.0", "cosmiconfig": "^7.0.0", - "find-versions": "^3.2.0", + "find-versions": "^4.0.0", "opencollective-postinstall": "^2.0.2", - "pkg-dir": "^4.2.0", + "pkg-dir": "^5.0.0", "please-upgrade-node": "^3.2.0", "slash": "^3.0.0", "which-pm-runs": "^1.0.0" @@ -1136,6 +1143,10 @@ }, "engines": { "node": ">=10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/husky" } }, "node_modules/ignore": { @@ -1404,15 +1415,18 @@ } }, "node_modules/locate-path": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", - "integrity": "sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==", + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", + "integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==", "dev": true, "dependencies": { - "p-locate": "^4.1.0" + "p-locate": "^5.0.0" }, "engines": { - "node": ">=8" + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/lodash": { @@ -1700,27 +1714,33 @@ "integrity": "sha512-VgnQEx+bIgCNodGisqZy+7wZxAnyF1rwwS+DJUBLSNhZ+D789yvknABAAEjwrk05SjMysfY4f4+XJlxSs+p1lg==" }, "node_modules/p-limit": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz", - "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", + "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==", "dev": true, "dependencies": { - "p-try": "^2.0.0" + "yocto-queue": "^0.1.0" }, "engines": { - "node": ">=6" + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/p-locate": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-4.1.0.tgz", - "integrity": "sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==", + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz", + "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==", "dev": true, "dependencies": { - "p-limit": "^2.2.0" + "p-limit": "^3.0.2" }, "engines": { - "node": ">=8" + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/p-map": { @@ -1735,15 +1755,6 @@ "node": ">=10" } }, - "node_modules/p-try": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", - "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==", - "dev": true, - "engines": { - "node": ">=6" - } - }, "node_modules/parent-module": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz", @@ -1817,15 +1828,15 @@ } }, "node_modules/pkg-dir": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-4.2.0.tgz", - "integrity": "sha512-HRDzbaKjC+AOWVXxAU/x54COGeIv9eb+6CkDSQoNTt4XyWoIJvuPsXizxu/Fr23EiekbtZwmh1IcIG/l/a10GQ==", + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-5.0.0.tgz", + "integrity": "sha512-NPE8TDbzl/3YQYY7CSS228s3g2ollTFnc+Qi3tqmqJp9Vg2ovUpixcJEo2HJScN2Ez+kEaal6y70c0ehqJBJeA==", "dev": true, "dependencies": { - "find-up": "^4.0.0" + "find-up": "^5.0.0" }, "engines": { - "node": ">=8" + "node": ">=10" } }, "node_modules/please-upgrade-node": { @@ -2036,12 +2047,15 @@ "dev": true }, "node_modules/semver-regex": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/semver-regex/-/semver-regex-2.0.0.tgz", - "integrity": "sha512-mUdIBBvdn0PLOeP3TEkMH7HHeUP3GjsXCwKarjv/kGmUFOYg1VqEemKhoQpWMu6X2I8kHeuVdGibLGkVK+/5Qw==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/semver-regex/-/semver-regex-3.1.2.tgz", + "integrity": "sha512-bXWyL6EAKOJa81XG1OZ/Yyuq+oT0b2YLlxx7c+mrdYPaPbnj6WgVULXhinMIeZGufuUBu/eVRqXEhiv4imfwxA==", "dev": true, "engines": { - "node": ">=6" + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/setimmediate": { @@ -2397,6 +2411,18 @@ "engines": { "node": ">= 6" } + }, + "node_modules/yocto-queue": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", + "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", + "dev": true, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } } }, "dependencies": { @@ -3187,22 +3213,22 @@ } }, "find-up": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", - "integrity": "sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==", + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", + "integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==", "dev": true, "requires": { - "locate-path": "^5.0.0", + "locate-path": "^6.0.0", "path-exists": "^4.0.0" } }, "find-versions": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/find-versions/-/find-versions-3.2.0.tgz", - "integrity": "sha512-P8WRou2S+oe222TOCHitLy8zj+SIsVJh52VP4lvXkaFVnOFFdoWv1H1Jjvel1aI6NCFOAaeAVm8qrI0odiLcww==", + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/find-versions/-/find-versions-4.0.0.tgz", + "integrity": "sha512-wgpWy002tA+wgmO27buH/9KzyEOQnKsG/R0yrcjPT9BOFm0zRBVQbZ95nRGXWMywS8YR5knRbpohio0bcJABxQ==", "dev": true, "requires": { - "semver-regex": "^2.0.0" + "semver-regex": "^3.1.2" } }, "flat-cache": { @@ -3293,18 +3319,18 @@ "dev": true }, "husky": { - "version": "4.3.6", - "resolved": "https://registry.npmjs.org/husky/-/husky-4.3.6.tgz", - "integrity": "sha512-o6UjVI8xtlWRL5395iWq9LKDyp/9TE7XMOTvIpEVzW638UcGxTmV5cfel6fsk/jbZSTlvfGVJf2svFtybcIZag==", + "version": "4.3.7", + "resolved": "https://registry.npmjs.org/husky/-/husky-4.3.7.tgz", + "integrity": "sha512-0fQlcCDq/xypoyYSJvEuzbDPHFf8ZF9IXKJxlrnvxABTSzK1VPT2RKYQKrcgJ+YD39swgoB6sbzywUqFxUiqjw==", "dev": true, "requires": { "chalk": "^4.0.0", "ci-info": "^2.0.0", "compare-versions": "^3.6.0", "cosmiconfig": "^7.0.0", - "find-versions": "^3.2.0", + "find-versions": "^4.0.0", "opencollective-postinstall": "^2.0.2", - "pkg-dir": "^4.2.0", + "pkg-dir": "^5.0.0", "please-upgrade-node": "^3.2.0", "slash": "^3.0.0", "which-pm-runs": "^1.0.0" @@ -3529,12 +3555,12 @@ } }, "locate-path": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-5.0.0.tgz", - "integrity": "sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==", + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", + "integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==", "dev": true, "requires": { - "p-locate": "^4.1.0" + "p-locate": "^5.0.0" } }, "lodash": { @@ -3764,21 +3790,21 @@ "integrity": "sha512-VgnQEx+bIgCNodGisqZy+7wZxAnyF1rwwS+DJUBLSNhZ+D789yvknABAAEjwrk05SjMysfY4f4+XJlxSs+p1lg==" }, "p-limit": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz", - "integrity": "sha512-//88mFWSJx8lxCzwdAABTJL2MyWB12+eIY7MDL2SqLmAkeKU9qxRvWuSyTjm3FUmpBEMuFfckAIqEaVGUDxb6w==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", + "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==", "dev": true, "requires": { - "p-try": "^2.0.0" + "yocto-queue": "^0.1.0" } }, "p-locate": { - "version": "4.1.0", - "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-4.1.0.tgz", - "integrity": "sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==", + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz", + "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==", "dev": true, "requires": { - "p-limit": "^2.2.0" + "p-limit": "^3.0.2" } }, "p-map": { @@ -3790,12 +3816,6 @@ "aggregate-error": "^3.0.0" } }, - "p-try": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", - "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==", - "dev": true - }, "parent-module": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz", @@ -3848,12 +3868,12 @@ "dev": true }, "pkg-dir": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-4.2.0.tgz", - "integrity": "sha512-HRDzbaKjC+AOWVXxAU/x54COGeIv9eb+6CkDSQoNTt4XyWoIJvuPsXizxu/Fr23EiekbtZwmh1IcIG/l/a10GQ==", + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-5.0.0.tgz", + "integrity": "sha512-NPE8TDbzl/3YQYY7CSS228s3g2ollTFnc+Qi3tqmqJp9Vg2ovUpixcJEo2HJScN2Ez+kEaal6y70c0ehqJBJeA==", "dev": true, "requires": { - "find-up": "^4.0.0" + "find-up": "^5.0.0" } }, "please-upgrade-node": { @@ -4024,9 +4044,9 @@ "dev": true }, "semver-regex": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/semver-regex/-/semver-regex-2.0.0.tgz", - "integrity": "sha512-mUdIBBvdn0PLOeP3TEkMH7HHeUP3GjsXCwKarjv/kGmUFOYg1VqEemKhoQpWMu6X2I8kHeuVdGibLGkVK+/5Qw==", + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/semver-regex/-/semver-regex-3.1.2.tgz", + "integrity": "sha512-bXWyL6EAKOJa81XG1OZ/Yyuq+oT0b2YLlxx7c+mrdYPaPbnj6WgVULXhinMIeZGufuUBu/eVRqXEhiv4imfwxA==", "dev": true }, "setimmediate": { @@ -4323,6 +4343,12 @@ "resolved": "https://registry.npmjs.org/yaml/-/yaml-1.10.0.tgz", "integrity": "sha512-yr2icI4glYaNG+KWONODapy2/jDdMSDnrONSjblABjD9B4Z5LgiircSt8m8sRZFNi08kG9Sm0uSHtEmP3zaEGg==", "dev": true + }, + "yocto-queue": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", + "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", + "dev": true } } } diff --git a/package.json b/package.json index dacc24b..9299f79 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "eslint": "^7.17.0", "eslint-config-prettier": "^7.1.0", "eslint-plugin-prettier": "^3.3.0", - "husky": "^4.3.6", + "husky": "^4.3.7", "lint-staged": "^10.5.3", "prettier": "^2.2.1" }, diff --git a/utils/addRole.js b/utils/guildMemberHandlers.js similarity index 62% rename from utils/addRole.js rename to utils/guildMemberHandlers.js index ad82514..3d3a403 100644 --- a/utils/addRole.js +++ b/utils/guildMemberHandlers.js @@ -1,4 +1,4 @@ -module.exports = (bot, discordUserId, roleName) => { +const addRole = (bot, discordUserId, roleName) => { roleName = String(roleName); bot.guilds .fetch(process.env.SERVER_GUILD_ID) @@ -36,3 +36,27 @@ module.exports = (bot, discordUserId, roleName) => { }) .catch((err) => console.log(err)); }; + +const ban = (bot, discordUserId) => { + bot.guilds + .fetch(process.env.SERVER_GUILD_ID) + .then((guild) => { + guild.members + .fetch(discordUserId) + .then((member) => { + member + .ban({ + days: 0, + reason: "Failed the verification process 3 times.", + }) + .catch((err) => console.log(err)); + }) + .catch((err) => console.log(err)); + }) + .catch((err) => console.log(err)); +}; + +module.exports = { + addRole, + ban, +}; diff --git a/utils/usersHandlers.js b/utils/usersHandlers.js new file mode 100644 index 0000000..1edc0eb --- /dev/null +++ b/utils/usersHandlers.js @@ -0,0 +1,87 @@ +const mongo = require("../mongo"); +const User = require("../models/user.model"); + +const add = async (discordUserId) => { + await mongo().then(async (mongoose) => { + try { + const newUser = new User({ + discordId: discordUserId, + failCount: 0, + }); + + await newUser + .save() + .then((doc) => {}) + .catch((err) => console.log(err)); + } finally { + mongoose.connection.close(); + } + }); +}; + +// I am using two DB calls here as I was unable to get the desired result with $inc +const updateFailCount = async (discordUserId) => { + let count = null; + await mongo().then(async (mongoose) => { + try { + await User.findOne({ discordId: discordUserId }, (err, doc) => { + if (err) console.log(err); + else if (doc) { + count = doc.failCount + 1; + } + }); + } finally { + mongoose.connection.close(); + } + }); + await mongo().then(async (mongoose) => { + try { + await User.findOneAndUpdate( + { discordId: discordUserId }, + { failCount: count }, + (err, doc) => { + if (err) console.log(err); + } + ); + } finally { + mongoose.connection.close(); + } + }); + return count; +}; + +const existsInUsers = async (discordUserId) => { + let exists = false; + + await mongo().then(async (mongoose) => { + try { + await User.findOne({ discordId: discordUserId }, (err, doc) => { + if (err) console.log(err); + else if (doc) exists = true; + }); + } finally { + mongoose.connection.close(); + } + }); + + return exists; +}; + +const remove = async (discordUserId) => { + await mongo().then(async (mongoose) => { + try { + await User.findOneAndDelete({ discordId: discordUserId }, (err, doc) => { + if (err) console.log(err); + }); + } finally { + mongoose.connection.close(); + } + }); +}; + +module.exports = { + add, + updateFailCount, + existsInUsers, + remove, +}; diff --git a/verify.js b/verify.js index 82d8b42..27c71a5 100644 --- a/verify.js +++ b/verify.js @@ -1,6 +1,7 @@ var otpGenerator = require("otp-generator"); var nodemailer = require("nodemailer"); -const addRole = require("./utils/addRole"); +const user = require("./utils/usersHandlers"); +const { addRole, ban } = require("./utils/guildMemberHandlers"); const getInstructions = `**✨Hey! Welcome to the KPH server✨** @@ -38,9 +39,9 @@ const sendMail = async (email) => { to: email, subject: "Knuth Programming Hub Discord Server - Verification", text: `This is your OTP for verification: "${OTP}". - Do not share this with anyone! +Do not share this with anyone! - If you didn't request for it, then please ignore this mail.`, +If you didn't request for it, then please ignore this mail.`, }; await mailTransporter.sendMail(mailDetails).then( @@ -96,6 +97,16 @@ const validateCollegeEmail = (email) => { } }; +const handleFail = async (bot, dmChannel, discordUserId) => { + const failCount = await user.updateFailCount(discordUserId); + await dmChannel.send(`*No. of attempts left: ${3 - failCount}*`); + if (failCount >= 3) { + user.remove(discordUserId); + await dmChannel.send("You are banned from the server!!"); + ban(bot, discordUserId); + } +}; + const filter = (m) => m.content.startsWith("/"); module.exports = async (bot, discordUser) => { @@ -113,7 +124,7 @@ module.exports = async (bot, discordUser) => { await dmChannel .awaitMessages(filter, { max: 1, - time: 500000, + time: 300000, errors: ["time"], }) .then(async (message) => { @@ -124,6 +135,7 @@ module.exports = async (bot, discordUser) => { dmChannel.send( "Hmm, that doesn't look like an email address. Send !verify on the server to try again." ); + handleFail(bot, dmChannel, discordUser.id); return; } const [ch, batch] = validateCollegeEmail(email); @@ -133,6 +145,7 @@ module.exports = async (bot, discordUser) => { dmChannel.send( "Hmm, that doesn't look like a JIIT email address. Note that this verification is for **JIIT students** only. Send !verify on the server to try again." ); + handleFail(bot, dmChannel, discordUser.id); return; } @@ -142,12 +155,13 @@ module.exports = async (bot, discordUser) => { dmChannel.send( "It seems that there was some error. Send !verify on the server to try again." ); - return; + handleFail(bot, dmChannel, discordUser.id); } }) .catch(() => { flag = true; - discordUser.send("Timeout! Send !verify on the server to try again."); + discordUser.send("Time's up! Send !verify on the server to try again."); + handleFail(bot, dmChannel, discordUser.id); }); if (flag === true) { @@ -169,19 +183,21 @@ module.exports = async (bot, discordUser) => { dmChannel.send( "Wrong OTP! Send !verify on the server to try again." ); - return; + handleFail(bot, dmChannel, discordUser.id); } else { addRole(bot, discordUser, "JIITian"); addRole(bot, discordUser, batchTag); dmChannel.send( "Yay! You have verified yourself as a JIITian and are now officially a member of the KPH Discord server! 🎉" ); - return; } }) .catch(() => { flag = true; - discordUser.send("Timeout, try again after some time."); + discordUser.send( + "Time's up! Send !verify on the server to try again." + ); + handleFail(bot, dmChannel, discordUser.id); }); }); });