diff --git a/.env.example b/.env.example index 44639e8..4bf59b1 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ BOTUSERNAME=Your bot account username -PASSWORD=Your bot account password \ No newline at end of file +PASSWORD=Your bot account password +DATABASEURL=The url of your mongoDB database (can be local or cluster and need to be URL friendly) \ No newline at end of file diff --git a/Commands/Bot/help.js b/Commands/Bot/help.js new file mode 100644 index 0000000..67181a1 --- /dev/null +++ b/Commands/Bot/help.js @@ -0,0 +1,19 @@ +const Command = require("../../Structure/Command.js"); + +module.exports = class extends Command { + constructor(client) { + super(client, { + name: "help", + description: "Return the commands list.", + category: "Bot", + enabled: true, + aliases: ["?"], + cooldown: 3, + dmOnly: false, + }); + } + + async run(message, args) { + message.chat.sendMessage("Here are the commands you can use ! https://github.com/Mr-KayJayDee/instagram-bot#available-commands") + } +} \ No newline at end of file diff --git a/Commands/Utils/myInfos.js b/Commands/Utils/myInfos.js index 4b41ded..333dcd5 100644 --- a/Commands/Utils/myInfos.js +++ b/Commands/Utils/myInfos.js @@ -14,8 +14,9 @@ module.exports = class extends Command { } async run(message, args) { - message.chat.sendMessage( -`FUll Name: ${message.author.fullName} + message.author.fetch().then(e => { + message.chat.sendMessage( + `Full Name: ${message.author.fullName} Username: ${message.author.username} ID: ${message.author.id} Biography:\n${message.author.biography} @@ -27,5 +28,6 @@ Private: ${message.author.isPrivate} Number Of Posts: ${message.author.mediaCount} Number Of IGTV posts: ${message.author.totalIgtvVideos} `) + }) } } \ No newline at end of file diff --git a/Events/connected.js b/Events/connected.js index 1cffda9..fa85980 100644 --- a/Events/connected.js +++ b/Events/connected.js @@ -5,10 +5,10 @@ module.exports = class { async emit() { this.client.logger.log(`Logged in as ${this.client.user.fullName} (${this.client.user.username}).`, "ready"); - this.client.logger.log(`Followers: ${this.client.user.followerCount}`, "ready") - this.client.logger.log(`Following: ${this.client.user.followingCount}`, "ready") - this.client.logger.log(`Business : ${this.client.user.isBusiness}`, "ready") - this.client.logger.log(`Verified: ${this.client.user.isVerified}`, "ready") - this.client.logger.log(`Private: ${this.client.user.isPrivate}`, "ready") + this.client.logger.log(`Followers: ${this.client.user.followerCount}`, "ready"); + this.client.logger.log(`Following: ${this.client.user.followingCount}`, "ready"); + this.client.logger.log(`Business : ${this.client.user.isBusiness}`, "ready"); + this.client.logger.log(`Verified: ${this.client.user.isVerified}`, "ready"); + this.client.logger.log(`Private: ${this.client.user.isPrivate}`, "ready"); } } diff --git a/Events/messageCreate.js b/Events/messageCreate.js index 476656e..57ef323 100644 --- a/Events/messageCreate.js +++ b/Events/messageCreate.js @@ -6,8 +6,21 @@ module.exports = class { } async emit(message) { + const data = {}; + data.config = this.client.config; + if (message.authorID === this.client.user.id) return; message.markSeen(); + if (message.chat.isGroup) { + const groupMembersData = await this.client.findOrCreateGroupMember({ id: message.author.id, groupID: message.chat.id }); + data.groupMembers = groupMembersData; + console.log(groupMembersData) + const groupsData = await this.client.findOrCreateGroup({ groupID: message.chat.id }); + data.groups = groupsData; + } else { + const usersData = await this.client.findOrCreateUser({ id: message.author.id }); + data.users = usersData; + } let prefixes = this.client.config.defaultPrefix; if (message.content.indexOf(prefixes) !== 0) return; @@ -46,6 +59,6 @@ module.exports = class { tStamps.set(message.authorID, timeNow); setTimeout(() => tStamps.delete(message.authorID), cdAmount); - cmd.run(message, args); + cmd.run(message, args, data); } } diff --git a/Models/GroupMembers.js b/Models/GroupMembers.js new file mode 100644 index 0000000..4d8e7b1 --- /dev/null +++ b/Models/GroupMembers.js @@ -0,0 +1,7 @@ +const mongoose = require("mongoose"), + Schema = mongoose.Schema; + +module.exports = mongoose.model("GroupMembers", new Schema({ + id: String, + groupID: String, +})); diff --git a/Models/Groups.js b/Models/Groups.js new file mode 100644 index 0000000..333aada --- /dev/null +++ b/Models/Groups.js @@ -0,0 +1,10 @@ +const mongoose = require("mongoose"), + Schema = mongoose.Schema; + +module.exports = mongoose.model("Groups", new Schema({ + groupID: String, + reminds: { + type: Array, + default: [] + }, +})); diff --git a/Models/Users.js b/Models/Users.js new file mode 100644 index 0000000..c4863d4 --- /dev/null +++ b/Models/Users.js @@ -0,0 +1,10 @@ +const mongoose = require("mongoose"), + Schema = mongoose.Schema; + +module.exports = mongoose.model("Users", new Schema({ + id: String, + reminds: { + type: Array, + default: [] + }, +})); diff --git a/README.md b/README.md index 5f9fd7d..79d8433 100644 --- a/README.md +++ b/README.md @@ -85,13 +85,13 @@ it means that the bot is ready to be used. 🎉 # Known bugs -- A story answer return an error (TypeError: Cannot read property 'indexOf' of undefined) +- A story answer return an error (TypeError: Cannot read property 'indexOf' of undefined, same for all system messages) # Future updates - Ability to change the prefix on a chat - Ability to change the lang -- Ability to make reminders +- Ability to make reminders (working on it) - Ability to get the informations about an user Feel free to make a pull request if you have any edit to do in the code or just open an issue if you don't know how to fix it. diff --git a/Structure/Client.js b/Structure/Client.js index 6d70ace..f8c0b64 100644 --- a/Structure/Client.js +++ b/Structure/Client.js @@ -1,20 +1,27 @@ const { Client } = require("@androz2091/insta.js"), -Collection = require("@discordjs/collection") + Collection = require("@discordjs/collection") path = require("path"); class InstaBot extends Client { constructor(options) { super(options); - ["commands", "aliases", "cooldowns"].forEach(x => this[x] = new Collection()); + ["commands", "aliases", "cooldowns"].forEach(x => this[x] = new Collection()); this.config = require("../config"); this.logger = require("../Helpers/logger"); + this.usersData = require("../Models/Users"); + this.groupsData = require("../Models/Groups") + this.groupMembersData = require("../Models/GroupMembers") + this.databaseCache = {}; + this.databaseCache.users = new Collection(); + this.databaseCache.groups = new Collection(); + this.databaseCache.groupMembers = new Collection(); } loadCommand(commandPath, commandName) { try { const props = new (require(`.${commandPath}${path.sep}${commandName}`))(this); props.conf.location = commandPath; - if (props.init){ + if (props.init) { props.init(this); } this.commands.set(props.help.name, props); @@ -27,22 +34,73 @@ class InstaBot extends Client { } } - async unloadCommand (commandPath, commandName) { + async unloadCommand(commandPath, commandName) { let command; - if(this.commands.has(commandName)) { + if (this.commands.has(commandName)) { command = this.commands.get(commandName); - } else if(this.aliases.has(commandName)){ + } else if (this.aliases.has(commandName)) { command = this.commands.get(this.aliases.get(commandName)); } - if(!command){ + if (!command) { return `The command \`${commandName}\` doesn't seem to exist, nor is it an alias. Try again!`; } - if(command.shutdown){ + if (command.shutdown) { await command.shutdown(this); } delete require.cache[require.resolve(`.${commandPath}${path.sep}${commandName}.js`)]; return false; } + + async findOrCreateUser({ id: userID }, isLean) { + if (this.databaseCache.users.get(userID)) { + return isLean ? this.databaseCache.users.get(userID).toJSON() : this.databaseCache.users.get(userID); + } else { + let userData = (isLean ? await this.usersData.findOne({ id: userID }).populate("members").lean() : await this.usersData.findOne({ id: userID }).populate("members")); + if (userData) { + if (!isLean) this.databaseCache.users.set(userID, userData); + return userData; + } else { + userData = new this.usersData({ id: userID }); + await userData.save(); + this.databaseCache.users.set(userID, userData); + return isLean ? userData.toJSON() : userData; + } + } + } + + async findOrCreateGroupMember({ id: userID, groupID: groupID }, isLean) { + if (this.databaseCache.groupMembers.get(`${userID}-${groupID}`)) { + return isLean ? this.databaseCache.groupMembers.get(`${userID}-${groupID}`).toJSON() : this.databaseCache.groupMembers.get(`${userID}-${groupID}`); + } else { + let groupMemberData = (isLean ? await this.groupMembersData.findOne({ id: userID, groupID: groupID }).populate("members").lean() : await this.groupMembersData.findOne({ id: userID, groupID: groupID }).populate("members")); + if (groupMemberData) { + if (!isLean) this.databaseCache.groupMembers.set(`${userID}-${groupID}`, groupMemberData); + return groupMemberData; + } else { + groupMemberData = new this.groupMembersData({ id: userID, groupID: groupID }); + await groupMemberData.save(); + this.databaseCache.groupMembers.set(`${userID}-${groupID}`, groupMemberData); + return isLean ? groupMemberData.toJSON() : groupMemberData; + } + } + } + + async findOrCreateGroup({ groupID: groupID }, isLean) { + if (this.databaseCache.groups.get(groupID)) { + return isLean ? this.databaseCache.groupMembers.get(groupID).toJSON() : this.databaseCache.groupMembers.get(groupID); + } else { + let groupData = (isLean ? await this.groupMembersData.findOne({ groupID: groupID }).populate("members").lean() : await this.groupMembersData.findOne({ groupID: groupID }).populate("members")); + if (groupData) { + if (!isLean) this.databaseCache.groupMembers.set(groupID, groupData); + return groupData; + } else { + groupData = new this.groupMembersData({ groupID: groupID }); + await groupData.save(); + this.databaseCache.groupMembers.set(groupID, groupData); + return isLean ? groupData.toJSON() : groupData; + } + } + } } module.exports = InstaBot; \ No newline at end of file diff --git a/index.js b/index.js index f547392..162f559 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,8 @@ const InstaBot = require("./Structure/Client"), const util = require("util"), fs = require("fs"), - readdir = util.promisify(fs.readdir); + readdir = util.promisify(fs.readdir), + mongoose = require("mongoose"); require("dotenv").config(); const initialize = async () => { @@ -26,5 +27,11 @@ const initialize = async () => { delete require.cache[require.resolve(`./Events/${file}`)]; }); client.login(process.env.BOTUSERNAME, process.env.PASSWORD); + + mongoose.connect(process.env.DATABASEURL, { useNewUrlParser: true, useUnifiedTopology: true }).then(() => { + client.logger.log("Connected to the Mongodb database.", "ready"); + }).catch((err) => { + client.logger.log("Unable to connect to the Mongodb database. Error:"+err, "error"); + }); }; initialize(); \ No newline at end of file diff --git a/package.json b/package.json index 5e72898..3c3b89b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "instabot", - "version": "1.1.2", + "version": "1.1.5", "description": "A fully working instagram bot using Insta.js by androz2091", "main": "index.js", "scripts": { @@ -14,6 +14,9 @@ "@discordjs/collection": "^0.1.6", "chalk": "^4.1.0", "discord-image-generation": "^1.3.7", - "dotenv": "^8.2.0" + "dotenv": "^8.2.0", + "mongoose": "^5.10.1", + "ms": "^2.1.2", + "node-fetch": "^2.6.0" } }