diff --git a/src/functions/fetchChannels.ts b/src/functions/fetchChannels.ts index dc08145c..cb701c96 100644 --- a/src/functions/fetchChannels.ts +++ b/src/functions/fetchChannels.ts @@ -30,7 +30,10 @@ function pushChannelsToArray( */ export default async function fetchGuildChannels(connection: Connection, client: Client, platform: HydratedDocument) { try { + const hasBotAccessToGuild = await platformService.checkBotAccessToGuild(client, platform.metadata?.id); + logger.info({ hasBotAccessToGuild, guildId: platform.metadata?.id, type: 'channel' }) + if (!hasBotAccessToGuild) { return; } @@ -40,6 +43,8 @@ export default async function fetchGuildChannels(connection: Connection, client: channel => channel.type === 0 || channel.type === 2 || channel.type === 4 ) as Array; pushChannelsToArray(channelsToStore, textAndVoiceChannels); + logger.info({ channels: channelsToStore }) + await channelService.createChannels(connection, channelsToStore); // assuming a 'channelService' } catch (error) { logger.error({ guild_id: platform.metadata?.id, error }, 'Failed to fetch channels'); diff --git a/src/functions/fetchMembers.ts b/src/functions/fetchMembers.ts index 8ef2d943..c5416a76 100644 --- a/src/functions/fetchMembers.ts +++ b/src/functions/fetchMembers.ts @@ -31,6 +31,8 @@ function pushMembersToArray(arr: IGuildMember[], guildMembersArray: GuildMember[ export default async function fetchGuildMembers(connection: Connection, client: Client, platform: HydratedDocument) { try { const hasBotAccessToGuild = await platformService.checkBotAccessToGuild(client, platform.metadata?.id); + logger.info({ hasBotAccessToGuild, guildId: platform.metadata?.id, type: 'guild member' }) + if (!hasBotAccessToGuild) { return; } @@ -38,6 +40,8 @@ export default async function fetchGuildMembers(connection: Connection, client: const membersToStore: IGuildMember[] = []; const fetchMembers = await guild.members.fetch(); pushMembersToArray(membersToStore, [...fetchMembers.values()]); + logger.info({ members: membersToStore }) + await guildMemberService.createGuildMembers(connection, membersToStore); } catch (error) { logger.error({ guild_id: platform.metadata?.id, error }, 'Failed to fetch guild members'); diff --git a/src/functions/fetchRoles.ts b/src/functions/fetchRoles.ts index 137259b7..e15a64c3 100644 --- a/src/functions/fetchRoles.ts +++ b/src/functions/fetchRoles.ts @@ -29,12 +29,16 @@ function pushRolesToArray(arr: IRole[], roleArray: Role[]): IRole[] { export default async function fetchGuildRoles(connection: Connection, client: Client, platform: HydratedDocument) { try { const hasBotAccessToGuild = await platformService.checkBotAccessToGuild(client, platform.metadata?.id); + logger.info({ hasBotAccessToGuild, guildId: platform.metadata?.id, type: 'role' }) + if (!hasBotAccessToGuild) { return; } const guild = await client.guilds.fetch(platform.metadata?.id); const rolesToStore: IRole[] = []; pushRolesToArray(rolesToStore, [...guild.roles.cache.values()]); + logger.info({ roles: rolesToStore }) + await roleService.createRoles(connection, rolesToStore); } catch (error) { logger.error({ guild_id: platform.metadata?.id, error }, 'Failed to fetch roles'); diff --git a/src/index.ts b/src/index.ts index 2ac0fb8d..55da3b48 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,6 +44,8 @@ const partial = func(...args, ...rest); const fetchMethod = async (msg: any) => { + + console.log(11) logger.info({ msg }, 'fetchMethod is running'); if (!msg) return; const { content } = msg; @@ -51,6 +53,8 @@ const fetchMethod = async (msg: any) => { logger.info({ saga: saga.data }, 'the saga info'); const platformId = saga.data['platformId']; const platform = await platformService.getPlatform({ _id: platformId }); + console.log(saga) + console.log(platform) if (platform) { const isPlatformCreated = saga.data['created']; @@ -98,10 +102,15 @@ const notifyUserAboutAnalysisFinish = async ( }; const fetchInitialData = async (platform: HydratedDocument) => { - const connection = DatabaseManager.getInstance().getTenantDb(platform.metadata?.id); - await fetchRoles(connection, client, platform); - await fetchChannels(connection, client, platform); - await fetchMembers(connection, client, platform); + try { + const connection = DatabaseManager.getInstance().getTenantDb(platform.metadata?.id); + await fetchRoles(connection, client, platform); + await fetchChannels(connection, client, platform); + await fetchMembers(connection, client, platform); + } catch (error) { + logger.error({ error }, 'fetchInitialData is failed'); + } + }; // APP @@ -127,15 +136,20 @@ async function app() { ); RabbitMQ.onEvent(Event.DISCORD_BOT.FETCH, async msg => { - logger.info({ msg, event: Event.DISCORD_BOT.FETCH }, 'is running'); - if (!msg) return; + try { + logger.info({ msg, event: Event.DISCORD_BOT.FETCH }, 'is running'); + if (!msg) return; - const { content } = msg; - const saga = await MBConnection.models.Saga.findOne({ sagaId: content.uuid }); + const { content } = msg; + const saga = await MBConnection.models.Saga.findOne({ sagaId: content.uuid }); - const fn = partial(fetchMethod, msg); - await saga.next(fn); - logger.info({ msg, event: Event.DISCORD_BOT.FETCH }, 'is done'); + const fn = partial(fetchMethod, msg); + await saga.next(fn); + logger.info({ msg, event: Event.DISCORD_BOT.FETCH }, 'is done'); + } catch (error) { + logger.error({ msg, event: Event.DISCORD_BOT.FETCH_MEMBERS, error }, 'is failed'); + + } }); RabbitMQ.onEvent(Event.DISCORD_BOT.SEND_MESSAGE, async msg => { @@ -170,7 +184,9 @@ async function app() { const platform = await platformService.getPlatform({ _id: platformId }); + logger.info({ msg, event: Event.DISCORD_BOT.FETCH_MEMBERS, platform, platformId }) if (platform) { + logger.info({ event: "FETCHING Initial DATA" }) const fn = fetchInitialData.bind({}, platform); await saga.next(fn); }