const { Collection, Client, MessageEmbed } = require('discord.js'); const MongoClient = require('mongodb').MongoClient; const mongoose = require('mongoose'); const Logger = require("../util/Logger"); const path = require("path"); const fs = require('fs'); class QuarksBot extends Client { constructor(options, config) { super(options) this.config = config; this.commands = new Collection(); this.logger = new Logger(path.join(__dirname, "..", "logs/Logs.log")); if (this.config.Token === "") return new TypeError( "The config.js is not filled out. Please make sure nothing is blank, otherwise the bot will not work properly." ); this.db; this.dbo; this.connectMongo(this.config.mongoURI, this.config.dbo); this.LoadCommands(); this.LoadEvents(); this.Ready = false; this.ws.on("INTERACTION_CREATE", async (interaction) => { if (interaction.type!=3) { client.log("Interaction") let GuildDB = await this.GetGuild(interaction.guild_id); const command = interaction.data.name.toLowerCase(); const args = interaction.data.options; //Easy to send respnose so ;) interaction.guild = await this.guilds.fetch(interaction.guild_id); interaction.send = async (message) => { return await this.api .interactions(interaction.id, interaction.token) .callback.post({ data: { type: 4, data: typeof message == "string" ? { content: message } : message.type && message.type === "rich" ? { embeds: [message] } : message, }, }); }; let cmd = client.commands.get(command); if (cmd.SlashCommand && cmd.SlashCommand.run) cmd.SlashCommand.run(this, interaction, args, { GuildDB }); } }); const client = this; } async connectMongo(mongoURI, dbo) { // Connect to MongoDB database. this.db = await MongoClient.connect(mongoURI); this.dbo = this.db.db(dbo); mongoose.connect(`${mongoURI}/${dbo}`); this.log('Successfully connected to mongoDB'); } exists(n) {return null != n && undefined != n && "" != n} padding(data) {return (` ${data}` + " ".repeat((9-data.toString().length)))} LoadCommands() { let CommandsDir = path.join(__dirname, '..', 'commands'); fs.readdir(CommandsDir, (err, files) => { if (err) this.log(err); else files.forEach((file) => { let cmd = require(CommandsDir + "/" + file); if (!cmd.name || !cmd.description) return this.log( "Unable to load Command: " + file.split(".")[0] + ", Reason: File doesn't had run/name/desciption" ); this.commands.set(file.split(".")[0].toLowerCase(), cmd); this.log("Command Loaded: " + file.split(".")[0]); }); }); } LoadEvents() { let EventsDir = path.join(__dirname, '..', 'events'); fs.readdir(EventsDir, (err, files) => { if (err) this.log(err); else files.forEach((file) => { const event = require(EventsDir + "/" + file); this.on(file.split(".")[0], event.bind(null, this)); this.logger.log("Event Loaded: " + file.split(".")[0]); }); }); } sendTime(Channel, Error) { let embed = new MessageEmbed() .setColor(this.config.EmbedColor) .setDescription(Error); Channel.send(embed); } RegisterSlashCommands() { this.guilds.cache.forEach((guild) => { require("../util/RegisterSlashCommands")(this, guild.id); }); } async GetGuild(GuildId) { let serverID = GuildId; let customChannelStatus, allowedChannels; let actionChannel, tweetChannel; let startingBalance, incomeRoles; let guild = await this.dbo.collection("guilds").findOne({"server.serverID":GuildId}).then(guild => guild); // If guild not found, generate guild default if (!guild) { let newGuild = { server: { serverID: GuildId, hasCustomChannels: false, allowedChannels: [], actionChannel: null, tweetChannel: null, startingBalance: 1000.00, incomeRoles: [], } } this.dbo.collection("guilds").insertOne(newGuild, function(err, res) { if (err) throw err; }); customChannelStatus = newGuild.server.hasCustomChannels; allowedChannels = newGuild.server.allowedChannels; actionChannel = newGuild.server.actionChannel; tweetChannel = newGuild.server.tweetChannel; startingBalance = newGuild.server.startingBalance; incomeRoles = newGuild.server.incomeRoles; } else { customChannelStatus = guild.server.hasCustomChannels; allowedChannels = guild.server.allowedChannels; actionChannel = guild.server.actionChannel; tweetChannel = guild.server.tweetChannel; startingBalance = guild.server.startingBalance; incomeRoles = guild.server.incomeRoles; } let guildData = { serverID: serverID, customChannelStatus: customChannelStatus, allowedChannels: allowedChannels, actionChannel: actionChannel, tweetChannel: tweetChannel, startingBalance: startingBalance, incomeRoles: incomeRoles, } return guildData; } /* This command is to verify a user has the correct role to use a command ie. has cop role to use name-search */ async verifyUseCommand(serverID, rolesCache, isList) { let { customRoleStatus } = await this.GetGuild(serverID) if (customRoleStatus) { let hasRole = await this.checkRoleStatus(rolesCache, serverID, isList); if (hasRole) { return true // User has role, can use command } else return false // User does not have role, can't use command } else return true // There is no role limits } log(Text) { this.logger.log(Text); } sendError(Channel, Error) { let embed = new MessageEmbed() .setTitle("An error occured") .setColor("RED") .setDescription(Error) Channel.send(embed); } build() { this.login(this.config.Token); } } module.exports = QuarksBot;