Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is bot to guild member db migration #156

Merged
merged 6 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"lint": "eslint **/*.ts",
"lint-fix": "eslint --fix **/*.ts",
"format": "prettier --write src/**/*.ts",
"dev-migrate:create": "migrate create --template-file ./src/migrations/utils/template.ts --migrations-dir=\"./src/migrations/db\" --compiler=\"ts:./src/migrations/utils/ts-compiler.js\"",
"dev-migrate:up": "migrate --migrations-dir=\"./src/migrations/db\" up --compiler=\"ts:./src/migrations/utils/ts-compiler.js\"",
"dev-migrate:down": "migrate --migrations-dir=\"./src/migrations/db\" --compiler=\"ts:./src/migrations/utils/ts-compiler.js\" down",
"migrate:create": "migrate create --template-file ./src/migrations/utils/template.ts --migrations-dir=\"./src/migrations/db\"",
"migrate:up": "migrate --migrations-dir=\"./lib/migrations/db\" up",
"migrate:down": "migrate --migrations-dir=\"./lib/migrations/db\" down"
Expand All @@ -29,7 +32,7 @@
"homepage": "https://github.com/Behzad-rabiei/tc-discordBot#readme",
"dependencies": {
"@sentry/node": "^7.51.2",
"@togethercrew.dev/db": "^3.0.18",
"@togethercrew.dev/db": "^3.0.28",
"@togethercrew.dev/tc-messagebroker": "^0.0.40",
"babel-jest": "^29.5.0",
"bullmq": "^3.14.0",
Expand Down
2 changes: 1 addition & 1 deletion src/events/channel/channelCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default {
if (channel instanceof TextChannel || channel instanceof VoiceChannel || channel instanceof CategoryChannel) {
const logFields = { guild_id: channel.guild.id, channel_id: channel.id };
logger.info(logFields, 'event is running');
const connection = DatabaseManager.getInstance().getTenantDb(channel.guild.id);
const connection = await DatabaseManager.getInstance().getTenantDb(channel.guild.id);
try {
await channelService.handelChannelChanges(connection, channel);
logger.info(logFields, 'event is done');
Expand Down
2 changes: 1 addition & 1 deletion src/events/channel/channelDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default {
if (channel instanceof TextChannel || channel instanceof VoiceChannel || channel instanceof CategoryChannel) {
const logFields = { guild_id: channel.guild.id, channel_id: channel.id };
logger.info(logFields, 'event is running');
const connection = DatabaseManager.getInstance().getTenantDb(channel.guild.id);
const connection = await DatabaseManager.getInstance().getTenantDb(channel.guild.id);
try {
const channelDoc = await channelService.getChannel(connection, { channelId: channel.id });
await channelDoc?.softDelete();
Expand Down
2 changes: 1 addition & 1 deletion src/events/channel/channelUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default {
) {
const logFields = { guild_id: newChannel.guild.id, channel_id: newChannel.id };
logger.info(logFields, 'event is running');
const connection = DatabaseManager.getInstance().getTenantDb(newChannel.guild.id);
const connection = await DatabaseManager.getInstance().getTenantDb(newChannel.guild.id);
try {
await channelService.handelChannelChanges(connection, newChannel);
logger.info(logFields, 'event is done');
Expand Down
2 changes: 1 addition & 1 deletion src/events/client/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
logger.info('event is running');
const platforms = await platformService.getPlatforms({ disconnectedAt: null });
for (let i = 0; i < platforms.length; i++) {
const connection = DatabaseManager.getInstance().getTenantDb(platforms[i].metadata?.id);
const connection = await DatabaseManager.getInstance().getTenantDb(platforms[i].metadata?.id);
try {
logger.info({ platform_id: platforms[i].id }, 'Fetching guild members, roles,and channels');
await fetchMembers(connection, client, platforms[i]);
Expand Down
2 changes: 1 addition & 1 deletion src/events/member/guildMemberAdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
async execute(member: GuildMember) {
const logFields = { guild_id: member.guild.id, guild_member_id: member.user.id };
logger.info(logFields, 'event is running');
const connection = DatabaseManager.getInstance().getTenantDb(member.guild.id);
const connection = await DatabaseManager.getInstance().getTenantDb(member.guild.id);
try {
await guildMemberService.handelGuildMemberChanges(connection, member);
logger.info(logFields, 'event is done');
Expand Down
2 changes: 1 addition & 1 deletion src/events/member/guildMemberRemove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
async execute(member: GuildMember) {
const logFields = { guild_id: member.guild.id, guild_member_id: member.user.id };
logger.info(logFields, 'event is running');
const connection = DatabaseManager.getInstance().getTenantDb(member.guild.id);
const connection = await DatabaseManager.getInstance().getTenantDb(member.guild.id);
try {
const guildMemberDoc = await guildMemberService.getGuildMember(connection, { discordId: member.user.id });
await guildMemberDoc?.softDelete();
Expand Down
2 changes: 1 addition & 1 deletion src/events/member/guildMemberUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
async execute(oldMember: GuildMember, newMember: GuildMember) {
const logFields = { guild_id: newMember.guild.id, guild_member_id: newMember.user.id };
logger.info(logFields, 'event is running');
const connection = DatabaseManager.getInstance().getTenantDb(newMember.guild.id);
const connection = await DatabaseManager.getInstance().getTenantDb(newMember.guild.id);
try {
await guildMemberService.handelGuildMemberChanges(connection, newMember);
logger.info(logFields, 'event is done');
Expand Down
2 changes: 1 addition & 1 deletion src/events/role/roleCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
async execute(role: Role) {
const logFields = { guild_id: role.guild.id, role_id: role.id };
logger.info(logFields, 'event is running');
const connection = DatabaseManager.getInstance().getTenantDb(role.guild.id);
const connection = await DatabaseManager.getInstance().getTenantDb(role.guild.id);
try {
await roleService.handelRoleChanges(connection, role);
logger.info(logFields, 'event is done');
Expand Down
2 changes: 1 addition & 1 deletion src/events/role/roleDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
async execute(role: Role) {
const logFields = { guild_id: role.guild.id, role_id: role.id };
logger.info(logFields, 'event is running');
const connection = DatabaseManager.getInstance().getTenantDb(role.guild.id);
const connection = await DatabaseManager.getInstance().getTenantDb(role.guild.id);
try {
const roleDoc = await roleService.getRole(connection, { roleId: role.id });
await roleDoc?.softDelete();
Expand Down
2 changes: 1 addition & 1 deletion src/events/role/roleUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
async execute(oldRole: Role, newRole: Role) {
const logFields = { guild_id: newRole.guild.id, role_id: newRole.id };
logger.info(logFields, 'event is running');
const connection = DatabaseManager.getInstance().getTenantDb(newRole.guild.id);
const connection = await DatabaseManager.getInstance().getTenantDb(newRole.guild.id);
try {
await roleService.handelRoleChanges(connection, newRole);
logger.info(logFields, 'event is done');
Expand Down
2 changes: 1 addition & 1 deletion src/events/user/userUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default {
try {
const platforms = await platformService.getPlatforms({ disconnectedAt: null });
for (let i = 0; i < platforms.length; i++) {
const connection = DatabaseManager.getInstance().getTenantDb(platforms[i].metadata?.id);
const connection = await DatabaseManager.getInstance().getTenantDb(platforms[i].metadata?.id);
await guildMemberService.updateGuildMember(
connection,
{ discordId: newUser.id },
Expand Down
2 changes: 1 addition & 1 deletion src/functions/cronJon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default async function cronJob(client: Client) {
logger.info('event is running');
const platforms = await platformService.getPlatforms({ disconnectedAt: null });
for (let i = 0; i < platforms.length; i++) {
const connection = DatabaseManager.getInstance().getTenantDb(platforms[i].metadata?.id);
const connection = await DatabaseManager.getInstance().getTenantDb(platforms[i].metadata?.id);
try {
logger.info({ platform_Id: platforms[i].metadata?.id }, 'is running cronJob for platform');
await guildExtraction(connection, client, platforms[i]);
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
});

const partial =
(func: any, ...args: any) =>

Check warning on line 42 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 42 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 42 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 42 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

Unexpected any. Specify a different type
(...rest: any) =>

Check warning on line 43 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 43 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

Unexpected any. Specify a different type
func(...args, ...rest);

const fetchMethod = async (msg: any) => {

Check warning on line 46 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 46 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

Unexpected any. Specify a different type

logger.info({ msg }, 'fetchMethod is running');
if (!msg) return;
Expand All @@ -55,7 +55,7 @@

if (platform) {
const isPlatformCreated = saga.data['created'];
const connection = DatabaseManager.getInstance().getTenantDb(platform.metadata?.id);
const connection = await DatabaseManager.getInstance().getTenantDb(platform.metadata?.id);
if (isPlatformCreated) {
await fetchChannels(connection, client, platform);
await fetchMembers(connection, client, platform);
Expand All @@ -77,7 +77,7 @@
const guild = await client.guilds.fetch(guildId);
const channels = await guild.channels.fetch();

const arrayChannels = Array.from(channels, ([name, value]) => ({ ...value } as Channel));

Check warning on line 80 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

'name' is defined but never used

Check warning on line 80 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

'name' is defined but never used
const textChannels = arrayChannels.filter(channel => channel.type == ChannelType.GuildText) as TextChannel[];
const rawPositionBasedSortedTextChannels = textChannels.sort((textChannelA, textChannelB) =>
textChannelA.rawPosition > textChannelB.rawPosition ? 1 : -1
Expand All @@ -99,7 +99,7 @@

const fetchInitialData = async (platform: HydratedDocument<IPlatform>) => {
try {
const connection = DatabaseManager.getInstance().getTenantDb(platform.metadata?.id);
const connection = await DatabaseManager.getInstance().getTenantDb(platform.metadata?.id);
await fetchChannels(connection, client, platform);
await fetchRoles(connection, client, platform);
await fetchMembers(connection, client, platform);
Expand Down Expand Up @@ -216,7 +216,7 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const worker = new Worker(
'cronJobQueue',
async (job: Job<any, any, string> | undefined) => {

Check warning on line 219 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 219 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 219 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

Unexpected any. Specify a different type

Check warning on line 219 in src/index.ts

View workflow job for this annotation

GitHub Actions / test/node 18.x/ubuntu-latest

Unexpected any. Specify a different type
if (job) {
// Call the extractMessagesDaily function
await cronJob(client);
Expand Down

This file was deleted.

25 changes: 25 additions & 0 deletions src/migrations/db/1706110397838-add-isbot-to-guildmember-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'dotenv/config';
import { connectDB } from '../../database';
import isBotLogic from '../utils/isBotLogic';
import { DatabaseManager } from '@togethercrew.dev/db';
import { Client, GatewayIntentBits } from 'discord.js';
import config from '../../config';
const { Guilds, GuildMembers, GuildMessages, GuildPresences, DirectMessages } = GatewayIntentBits;


export const up = async () => {
const client = new Client({
intents: [Guilds, GuildMembers, GuildMessages, GuildPresences, DirectMessages],
});

await client.login(config.discord.botToken);
await connectDB();
const connection1 = await DatabaseManager.getInstance().getTenantDb('1023936505321881641');
const connection2 = await DatabaseManager.getInstance().getTenantDb('949124961187016764');
await isBotLogic(connection1, client, '1023936505321881641');
await isBotLogic(connection2, client, '949124961187016764');
};

export const down = async () => {
// TODO: Implement rollback logic if needed
};
78 changes: 78 additions & 0 deletions src/migrations/utils/isBotLogic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Client, Snowflake } from 'discord.js';
import { GuildMember } from 'discord.js';
import { Connection } from 'mongoose';
import parentLogger from '../../config/logger';
import { guildMemberService } from '../../database/services';
import { IGuildMember, } from '@togethercrew.dev/db';

const logger = parentLogger.child({ module: 'Migration-isBot' });

/**
* Iterates over a list of guild members and pushes extracted data from each guild member to an array.
* @param {GuildMember[]} guildMembersArray - An array of guild members from which data is to be extracted.
* @returns {Promise<IGuildMember[]>} - A promise that resolves to the updated array containing the extracted data.
*/
function pushMembersToArray(arr: IGuildMember[], guildMembersArray: GuildMember[]): IGuildMember[] {
for (const guildMember of guildMembersArray) {
arr.push(guildMemberService.getNeededDateFromGuildMember(guildMember));
}
return arr;
}

/**
*
* @param {Connection} connection - Mongoose connection object for the database.
* @param {Snowflake} guildId - The identifier of the guild to extract information from.
*/
export default async function isBotLogic(connection: Connection, client: Client, guildId: Snowflake) {
logger.info({ guild_id: guildId }, 'add-isBot-to-guilbMember-schema migration is running');
try {
const botGuildMembers = [];
const noneBotGuildMembers = [];

const guild = await client.guilds.fetch(guildId);
const membersToStore: IGuildMember[] = [];
const fetchedMembers = await guild.members.fetch();
const guildMembers = pushMembersToArray(membersToStore, [...fetchedMembers.values()]);

console.log(guildMembers.length, guildId)
if (guildMembers) {
for (const guildMember of guildMembers) {
if (guildMember.isBot) {
botGuildMembers.push(guildMember.discordId);
} else {
noneBotGuildMembers.push(guildMember.discordId);
}
}
}

if (botGuildMembers.length > 0) {
await guildMemberService.updateGuildMembers(
connection,
{ discordId: { $in: botGuildMembers } },
{ isBot: true }
);
}

if (noneBotGuildMembers.length > 0) {
await guildMemberService.updateGuildMembers(
connection,
{ discordId: { $in: noneBotGuildMembers } },
{ isBot: false }
);
}

const mergedArray = botGuildMembers.concat(noneBotGuildMembers);
if (mergedArray.length > 0) {
await guildMemberService.updateGuildMembers(
connection,
{ discordId: { $nin: mergedArray } },
{ isBot: null }
);
}

} catch (err) {
logger.error({ guild_id: guildId, err }, 'add-isBot-to-guilbMember-schema migration is failed');
}
logger.info({ guild_id: guildId }, 'add-isBot-to-guilbMember-schema migration is done');
}
6 changes: 3 additions & 3 deletions src/migrations/utils/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import 'dotenv/config';
import { DatabaseManager } from '@togethercrew.dev/db';

export const up = async () => {
await connectDB();
const connection = DatabaseManager.getInstance().getTenantDb("681946187490000803");
await connectDB();
const connection = await DatabaseManager.getInstance().getTenantDb("681946187490000803");

await connection.createCollection('my_collection');
await connection.createCollection('my_collection');
};

export const down = async () => {
Expand Down
Loading