Skip to content

Commit

Permalink
[FEATURE]: add database manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-rabiei committed Nov 14, 2023
1 parent c5bcf0e commit 420df78
Show file tree
Hide file tree
Showing 38 changed files with 333 additions and 361 deletions.
88 changes: 75 additions & 13 deletions lib/commands/info/question.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,84 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-explicit-any */
const discord_js_1 = require("discord.js");
const responses_1 = require("../../functions/interactions/responses");
const tc_messagebroker_1 = __importStar(require("@togethercrew.dev/tc-messagebroker"));
exports.default = {
data: new discord_js_1.SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!')
.addStringOption(option => option.setName('input1')
.setDescription('The input to echo back'))
.addChannelOption(option => option.setName('input2')
.setDescription('The channel to echo into')),
.setName('question')
.setDescription('Ask a question and get an answer from Hivemind!')
.addStringOption(option => option.setName('question')
.setDescription('Your question to Hivemind')
.setRequired(true)),
async execute(interaction) {
await (0, responses_1.createInteractionResponse)(interaction, 4, { content: 'Hi', flags: 64 });
// await createInteractionResponse(interaction, 5, { flags: 64 })
// const { content } = await getOriginalInteractionResponse(interaction);
// await editOriginalInteractionResponse(interaction, { content: 'Edit' })
// await deleteOriginalInteractionResponse(interaction);
const z = await (0, responses_1.createFollowUpMessage)(interaction, { content: 'f1' });
console.log(z);
var _a;
if (!((_a = interaction.member) === null || _a === void 0 ? void 0 : _a.roles.cache.has("1166350549889859657"))) {
return await (0, responses_1.createInteractionResponse)(interaction, { type: 4, data: { content: 'You do not have the required role to use this command!', flags: 64 } });
}
const serializedInteraction = constructSerializableInteraction(interaction);
const serializedData = JSON.stringify(serializedInteraction, stringifyBigIntReplacer);
tc_messagebroker_1.default.publish(tc_messagebroker_1.Queue.HIVEMIND, tc_messagebroker_1.Event.HIVEMIND.INTERACTION_CREATED, { interaction: serializedData });
},
};
function constructSerializableInteraction(interaction) {
return {
id: interaction.id,
applicationId: interaction.applicationId,
type: interaction.type,
guildId: interaction.guildId,
guild: interaction.guild,
channel: interaction.channel,
channelId: interaction.channelId,
token: interaction.token,
user: interaction.user,
createdAt: interaction.createdAt,
deferred: interaction.deferred,
replied: interaction.replied,
webhook: interaction.webhook,
member: interaction.member,
ephemeral: interaction.ephemeral,
createdTimestamp: interaction.createdTimestamp,
appPermissions: interaction.appPermissions,
memberPermissions: interaction.memberPermissions,
locale: interaction.locale,
guildLocale: interaction.guildLocale,
client: interaction.client,
command: interaction.command,
commandId: interaction.commandId,
commandName: interaction.commandName,
commandType: interaction.commandType,
commandGuildId: interaction.commandGuildId,
options: interaction.options,
version: interaction.version,
};
}
function stringifyBigIntReplacer(key, value) {
if (typeof value === 'bigint') {
return value.toString() + 'n';
}
return value;
}
44 changes: 28 additions & 16 deletions lib/database/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.closeConnection = void 0;
const logger_1 = __importDefault(require("../config/logger"));
const logger = logger_1.default.child({ module: 'Connection' });
/**
* Closes a given Mongoose connection.
* @param {Connection} connection - The Mongoose connection object to be closed.
* @returns {Promise<void>} - A promise that resolves when the connection has been successfully closed.
* @throws {MongooseError} - If there is an error closing the connection, it is logged to the console and the error is thrown.
*/
async function closeConnection(connection) {
try {
await connection.close();
logger.info({ database: connection.name }, 'The connection to database has been successfully closed');
const mongoose_1 = __importDefault(require("mongoose"));
const db_1 = require("@togethercrew.dev/db");
class DatabaseManager {
constructor() {
this.modelCache = {};
}
catch (error) {
logger.fatal({ database: connection.name, error }, 'Failed to close the connection to the database');
static getInstance() {
if (!DatabaseManager.instance) {
DatabaseManager.instance = new DatabaseManager();
}
return DatabaseManager.instance;
}
getTenantDb(tenantId) {
const dbName = tenantId;
const db = mongoose_1.default.connection.useDb(dbName, { useCache: true });
this.setupModels(db);
return db;
}
setupModels(db) {
if (!this.modelCache[db.name]) {
db.model('HeatMap', db_1.heatMapSchema);
db.model('RawInfo', db_1.rawInfoSchema);
db.model('MemberActivity', db_1.MemberActivitySchema);
db.model('GuildMember', db_1.guildMemberSchema);
db.model('Channel', db_1.channelSchema);
db.model('Role', db_1.roleSchema);
this.modelCache[db.name] = true;
}
}
}
exports.closeConnection = closeConnection;
exports.default = DatabaseManager;
11 changes: 3 additions & 8 deletions lib/events/channel/channelCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const discord_js_1 = require("discord.js");
const services_1 = require("../../database/services");
const db_1 = require("@togethercrew.dev/db");
const config_1 = __importDefault(require("../../config"));
const connection_1 = require("../../database/connection");
const connection_1 = __importDefault(require("../../database/connection"));
const logger_1 = __importDefault(require("../../config/logger"));
const logger = logger_1.default.child({ event: 'ChannelCreate' });
exports.default = {
Expand All @@ -17,17 +15,14 @@ exports.default = {
if (channel instanceof discord_js_1.TextChannel || channel instanceof discord_js_1.VoiceChannel || channel instanceof discord_js_1.CategoryChannel) {
const logFields = { guild_id: channel.guild.id, channel_id: channel.id };
logger.info(logFields, 'event is running');
const connection = db_1.databaseService.connectionFactory(channel.guild.id, config_1.default.mongoose.dbURL);
const connection = connection_1.default.getInstance().getTenantDb(channel.guild.id);
try {
await services_1.channelService.handelChannelChanges(connection, channel);
logger.info(logFields, 'event is done');
}
catch (err) {
logger.error(Object.assign(Object.assign({}, logFields), { err }), 'Failed to handle channel changes');
}
finally {
await (0, connection_1.closeConnection)(connection);
logger.info(logFields, 'event is done');
}
}
},
};
11 changes: 3 additions & 8 deletions lib/events/channel/channelDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const discord_js_1 = require("discord.js");
const services_1 = require("../../database/services");
const db_1 = require("@togethercrew.dev/db");
const config_1 = __importDefault(require("../../config"));
const connection_1 = require("../../database/connection");
const connection_1 = __importDefault(require("../../database/connection"));
const logger_1 = __importDefault(require("../../config/logger"));
const logger = logger_1.default.child({ event: 'ChannelDelete' });
exports.default = {
Expand All @@ -18,21 +16,18 @@ exports.default = {
if (channel instanceof discord_js_1.TextChannel || channel instanceof discord_js_1.VoiceChannel || channel instanceof discord_js_1.CategoryChannel) {
const logFields = { guild_id: channel.guild.id, channel_id: channel.id };
logger.info(logFields, 'event is running');
const connection = db_1.databaseService.connectionFactory(channel.guild.id, config_1.default.mongoose.dbURL);
const connection = connection_1.default.getInstance().getTenantDb(channel.guild.id);
try {
const channelDoc = await services_1.channelService.getChannel(connection, { channelId: channel.id });
await (channelDoc === null || channelDoc === void 0 ? void 0 : channelDoc.softDelete());
const guildDoc = await services_1.guildService.getGuild({ guildId: channel.guild.id });
const updatedSelecetdChannels = (_a = guildDoc === null || guildDoc === void 0 ? void 0 : guildDoc.selectedChannels) === null || _a === void 0 ? void 0 : _a.filter(selectedChannel => selectedChannel.channelId !== channel.id);
await services_1.guildService.updateGuild({ guildId: channel.guild.id }, { selectedChannels: updatedSelecetdChannels });
logger.info(logFields, 'event is done');
}
catch (err) {
logger.error(Object.assign(Object.assign({}, logFields), { err }), 'Failed to soft delete the channel');
}
finally {
await (0, connection_1.closeConnection)(connection);
logger.info(logFields, 'event is done');
}
}
},
};
9 changes: 2 additions & 7 deletions lib/events/channel/channelUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const discord_js_1 = require("discord.js");
const services_1 = require("../../database/services");
const db_1 = require("@togethercrew.dev/db");
const config_1 = __importDefault(require("../../config"));
const connection_1 = require("../../database/connection");
const connection_1 = __importDefault(require("../../database/connection"));
const logger_1 = __importDefault(require("../../config/logger"));
const logger = logger_1.default.child({ event: 'ChannelUpdate' });
exports.default = {
Expand All @@ -19,15 +17,12 @@ exports.default = {
newChannel instanceof discord_js_1.CategoryChannel) {
const logFields = { guild_id: newChannel.guild.id, channel_id: newChannel.id };
logger.info(logFields, 'event is running');
const connection = db_1.databaseService.connectionFactory(newChannel.guild.id, config_1.default.mongoose.dbURL);
const connection = connection_1.default.getInstance().getTenantDb(newChannel.guild.id);
try {
await services_1.channelService.handelChannelChanges(connection, newChannel);
}
catch (err) {
logger.error(Object.assign(Object.assign({}, logFields), { err }), 'Failed to handle channel changes');
}
finally {
await (0, connection_1.closeConnection)(connection);
logger.info(logFields, 'event is done');
}
}
Expand Down
9 changes: 2 additions & 7 deletions lib/events/client/ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const discord_js_1 = require("discord.js");
const db_1 = require("@togethercrew.dev/db");
const services_1 = require("../../database/services");
const fetchMembers_1 = __importDefault(require("../../functions/fetchData//fetchMembers"));
const fetchChannels_1 = __importDefault(require("../../functions/fetchData/fetchChannels"));
const fetchRoles_1 = __importDefault(require("../../functions/fetchData/fetchRoles"));
const connection_1 = require("../../database/connection");
const connection_1 = __importDefault(require("../../database/connection"));
const logger_1 = __importDefault(require("../../config/logger"));
const config_1 = __importDefault(require("../../config"));
const logger = logger_1.default.child({ event: 'ClientReady' });
exports.default = {
name: discord_js_1.Events.ClientReady,
Expand All @@ -20,7 +18,7 @@ exports.default = {
logger.info('event is running');
const guilds = await services_1.guildService.getGuilds({ isDisconnected: false });
for (let i = 0; i < guilds.length; i++) {
const connection = db_1.databaseService.connectionFactory(guilds[i].guildId, config_1.default.mongoose.dbURL);
const connection = connection_1.default.getInstance().getTenantDb(guilds[i].guildId);
try {
logger.info({ guild_id: guilds[i].guildId }, 'Fetching guild members, roles,and channels');
await (0, fetchMembers_1.default)(connection, client, guilds[i].guildId);
Expand All @@ -31,9 +29,6 @@ exports.default = {
catch (err) {
logger.error({ guild_id: guilds[i].guildId, err }, 'Fetching guild members, roles,and channels failed');
}
finally {
await (0, connection_1.closeConnection)(connection);
}
}
logger.info('event is done');
},
Expand Down
11 changes: 3 additions & 8 deletions lib/events/member/guildMemberAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const discord_js_1 = require("discord.js");
const services_1 = require("../../database/services");
const db_1 = require("@togethercrew.dev/db");
const config_1 = __importDefault(require("../../config"));
const connection_1 = require("../../database/connection");
const connection_1 = __importDefault(require("../../database/connection"));
const logger_1 = __importDefault(require("../../config/logger"));
const logger = logger_1.default.child({ event: 'GuildMemberAdd' });
exports.default = {
Expand All @@ -16,16 +14,13 @@ exports.default = {
async execute(member) {
const logFields = { guild_id: member.guild.id, guild_member_id: member.user.id };
logger.info(logFields, 'event is running');
const connection = db_1.databaseService.connectionFactory(member.guild.id, config_1.default.mongoose.dbURL);
const connection = connection_1.default.getInstance().getTenantDb(member.guild.id);
try {
await services_1.guildMemberService.handelGuildMemberChanges(connection, member);
logger.info(logFields, 'event is done');
}
catch (err) {
logger.error(Object.assign(Object.assign({}, logFields), { err }), 'Failed to handle guild member changes');
}
finally {
await (0, connection_1.closeConnection)(connection);
logger.info(logFields, 'event is done');
}
},
};
11 changes: 3 additions & 8 deletions lib/events/member/guildMemberRemove.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const discord_js_1 = require("discord.js");
const services_1 = require("../../database/services");
const db_1 = require("@togethercrew.dev/db");
const config_1 = __importDefault(require("../../config"));
const connection_1 = require("../../database/connection");
const connection_1 = __importDefault(require("../../database/connection"));
const logger_1 = __importDefault(require("../../config/logger"));
const logger = logger_1.default.child({ event: 'GuildMemberRemove' });
exports.default = {
Expand All @@ -16,17 +14,14 @@ exports.default = {
async execute(member) {
const logFields = { guild_id: member.guild.id, guild_member_id: member.user.id };
logger.info(logFields, 'event is running');
const connection = db_1.databaseService.connectionFactory(member.guild.id, config_1.default.mongoose.dbURL);
const connection = connection_1.default.getInstance().getTenantDb(member.guild.id);
try {
const guildMemberDoc = await services_1.guildMemberService.getGuildMember(connection, { discordId: member.user.id });
await (guildMemberDoc === null || guildMemberDoc === void 0 ? void 0 : guildMemberDoc.softDelete());
logger.info(logFields, 'event is done');
}
catch (err) {
logger.error(Object.assign(Object.assign({}, logFields), { err }), 'Failed to soft delete the guild member');
}
finally {
await (0, connection_1.closeConnection)(connection);
logger.info(logFields, 'event is done');
}
},
};
11 changes: 3 additions & 8 deletions lib/events/member/guildMemberUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const discord_js_1 = require("discord.js");
const services_1 = require("../../database/services");
const db_1 = require("@togethercrew.dev/db");
const config_1 = __importDefault(require("../../config"));
const connection_1 = require("../../database/connection");
const logger_1 = __importDefault(require("../../config/logger"));
const connection_1 = __importDefault(require("../../database/connection"));
const logger = logger_1.default.child({ event: 'GuildMemberUpdate' });
exports.default = {
name: discord_js_1.Events.GuildMemberUpdate,
once: false,
async execute(oldMember, newMember) {
const logFields = { guild_id: newMember.guild.id, guild_member_id: newMember.user.id };
logger.info(logFields, 'event is running');
const connection = db_1.databaseService.connectionFactory(newMember.guild.id, config_1.default.mongoose.dbURL);
const connection = connection_1.default.getInstance().getTenantDb(newMember.guild.id);
logger.info(logFields, 'event is done');
try {
await services_1.guildMemberService.handelGuildMemberChanges(connection, newMember);
}
catch (err) {
logger.error(Object.assign(Object.assign({}, logFields), { err }), 'Failed to handle guild member changes');
}
finally {
await (0, connection_1.closeConnection)(connection);
logger.info(logFields, 'event is done');
}
},
};
Loading

0 comments on commit 420df78

Please sign in to comment.