Skip to content

Commit

Permalink
refactor database ⚡
Browse files Browse the repository at this point in the history
  • Loading branch information
saiteja-madha committed Feb 20, 2022
1 parent 0250815 commit 065657a
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 28 deletions.
16 changes: 13 additions & 3 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ require("module-alias/register");
require("@src/helpers/extenders");

const path = require("path");
const { startupCheck } = require("@utils/botUtils");
const { validateConfig, checkForUpdates } = require("@utils/botUtils");
const { initializeMongoose } = require("@src/database/mongoose");
const { BotClient } = require("@src/structures");

global.__appRoot = path.resolve(__dirname);
Expand All @@ -18,7 +19,15 @@ client.loadEvents("src/events");
process.on("unhandledRejection", (err) => client.logger.error(`Unhandled exception`, err));

(async () => {
await startupCheck();
validateConfig();

// initialize the database
await initializeMongoose();

// check for updates
await checkForUpdates();

// start the dashboard
if (client.config.DASHBOARD.enabled) {
client.logger.log("Launching dashboard");
try {
Expand All @@ -28,6 +37,7 @@ process.on("unhandledRejection", (err) => client.logger.error(`Unhandled excepti
client.logger.error("Failed to launch dashboard", ex);
}
}
await client.initializeMongoose();

// start the client
await client.login(process.env.BOT_TOKEN);
})();
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
OWNER_IDS: [], // Bot owner ID's
OWNER_IDS: ["280223531134877717"], // Bot owner ID's
PREFIX: "!", // Default prefix for the bot
SUPPORT_SERVER: "", // Your bot support server
PRESENCE: {
Expand Down
2 changes: 1 addition & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@root/*": ["./*"],
"@commands/*": ["./src/commands/*"],
"@features/*": ["./src/features/*"],
"@schemas/*": ["./src/schemas/*"],
"@schemas/*": ["./src/database/schemas/*"],
"@src/*": ["./src/*"],
"@utils/*": ["./src/utils/*"]
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@root": ".",
"@commands": "src/commands/",
"@features": "src/features/",
"@schemas": "src/schemas/",
"@schemas": "src/database/schemas/",
"@src": "src/",
"@utils": "src/utils/"
},
Expand Down
32 changes: 32 additions & 0 deletions src/database/mongoose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const mongoose = require("mongoose");
const { log, success, error } = require("../helpers/logger");

module.exports = {
async initializeMongoose() {
log(`Connecting to MongoDb...`);

try {
await mongoose.connect(process.env.MONGO_CONNECTION, {
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
});

success("Mongoose: Database connection established");
} catch (err) {
error("Mongoose: Failed to connect to database");
process.exit(1);
}
},

schemas: {
Giveaways: require("./schemas/Giveaways"),
Guild: require("./schemas/Guild").model,
Member: require("./schemas/Member").model,
Message: require("./schemas/Message").model,
ModLog: require("./schemas/ModLog").model,
TranslateLog: require("./schemas/TranslateLog").model,
User: require("./schemas/User").model,
},
};
File renamed without changes.
2 changes: 2 additions & 0 deletions src/schemas/Guild.js → src/database/schemas/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ const Schema = mongoose.Schema({
const Model = mongoose.model("guild", Schema);

module.exports = {
model: Model,

getSettings: async (guild) => {
if (cache.contains(guild.id)) return cache.get(guild.id);

Expand Down
2 changes: 2 additions & 0 deletions src/schemas/Member.js → src/database/schemas/Member.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const Schema = mongoose.Schema({
const Model = mongoose.model("members", Schema);

module.exports = {
model: Model,

getMember: async (guildId, memberId) => {
const key = `${guildId}|${memberId}`;
if (cache.contains(key)) return cache.get(key);
Expand Down
2 changes: 2 additions & 0 deletions src/schemas/Message.js → src/database/schemas/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const rrCache = new Map();
const getKey = (guildId, channelId, messageId) => `${guildId}|${channelId}|${messageId}`;

module.exports = {
model: Model,

getTicketConfig: async (guildId, channelId, messageId) =>
Model.findOne({
guild_id: guildId,
Expand Down
2 changes: 2 additions & 0 deletions src/schemas/ModLog.js → src/database/schemas/ModLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const Schema = mongoose.Schema({
const Model = mongoose.model("mod-logs", Schema);

module.exports = {
model: Model,

addModLogToDb: async (admin, target, reason, type) =>
await new Model({
guild_id: admin.guild.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const Schema = mongoose.Schema({
const Model = mongoose.model("logs-translation", Schema);

module.exports = {
model: Model,

isTranslated: async (message, code) =>
Model.findOne({
guild_id: message.guildId,
Expand Down
2 changes: 2 additions & 0 deletions src/schemas/User.js → src/database/schemas/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const Schema = mongoose.Schema({
const Model = mongoose.model("user", Schema);

module.exports = {
model: Model,

getUser: async (userId) => {
const cached = cache.get(userId);
if (cached) return cached;
Expand Down
19 changes: 3 additions & 16 deletions src/structures/BotClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ const { Client, Collection, Intents, WebhookClient } = require("discord.js");
const path = require("path");
const fs = require("fs");
const { table } = require("table");
const mongoose = require("mongoose");
const logger = require("../helpers/logger");
const MusicManager = require("./MusicManager");
const Command = require("./Command");
const BaseContext = require("./BaseContext");
const GiveawayManager = require("./GiveawayManager");
const { schemas } = require("@src/database/mongoose");

module.exports = class BotClient extends Client {
constructor() {
Expand Down Expand Up @@ -68,22 +68,9 @@ module.exports = class BotClient extends Client {

// Logger
this.logger = logger;
}

/**
* Initialize mongoose connection and keep it alive
*/
async initializeMongoose() {
this.logger.log(`Connecting to MongoDb...`);

await mongoose.connect(process.env.MONGO_CONNECTION, {
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
});

this.logger.success("Mongoose: Database connection established");
// Database
this.database = schemas;
}

/**
Expand Down
8 changes: 2 additions & 6 deletions src/utils/botUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ function validateConfig() {
if (!config.SUPPORT_SERVER) warn("config.js: SUPPORT_SERVER is not provided");
}

async function startupCheck() {
await checkForUpdates();
validateConfig();
}

const permissions = {
CREATE_INSTANT_INVITE: "Create instant invite",
KICK_MEMBERS: "Kick members",
Expand Down Expand Up @@ -138,7 +133,8 @@ function canSendEmbeds(channel) {
module.exports = {
permissions,
parsePermissions,
startupCheck,
validateConfig,
checkForUpdates,
musicValidations,
canSendEmbeds,
};

0 comments on commit 065657a

Please sign in to comment.