Skip to content

Commit

Permalink
[DEBUG]
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-rabiei committed Dec 5, 2023
1 parent 30be1a3 commit 73e9664
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/functions/fetchChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ function pushChannelsToArray(
*/
export default async function fetchGuildChannels(connection: Connection, client: Client, platform: HydratedDocument<IPlatform>) {
try {

const hasBotAccessToGuild = await platformService.checkBotAccessToGuild(client, platform.metadata?.id);
logger.info({ hasBotAccessToGuild, guildId: platform.metadata?.id, type: 'channel' })

if (!hasBotAccessToGuild) {
return;
}
Expand All @@ -40,6 +43,8 @@ export default async function fetchGuildChannels(connection: Connection, client:
channel => channel.type === 0 || channel.type === 2 || channel.type === 4
) as Array<TextChannel | VoiceChannel>;
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');
Expand Down
4 changes: 4 additions & 0 deletions src/functions/fetchMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ function pushMembersToArray(arr: IGuildMember[], guildMembersArray: GuildMember[
export default async function fetchGuildMembers(connection: Connection, client: Client, platform: HydratedDocument<IPlatform>) {
try {
const hasBotAccessToGuild = await platformService.checkBotAccessToGuild(client, platform.metadata?.id);
logger.info({ hasBotAccessToGuild, guildId: platform.metadata?.id, type: 'guild member' })

if (!hasBotAccessToGuild) {
return;
}
const guild = await client.guilds.fetch(platform.metadata?.id);
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');
Expand Down
4 changes: 4 additions & 0 deletions src/functions/fetchRoles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ function pushRolesToArray(arr: IRole[], roleArray: Role[]): IRole[] {
export default async function fetchGuildRoles(connection: Connection, client: Client, platform: HydratedDocument<IPlatform>) {
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');
Expand Down
38 changes: 27 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ const partial =
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

console.log(11)
logger.info({ msg }, 'fetchMethod is running');
if (!msg) return;
const { content } = msg;
const saga = await MBConnection.models.Saga.findOne({ sagaId: content.uuid });
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'];
Expand Down Expand Up @@ -98,10 +102,15 @@ const notifyUserAboutAnalysisFinish = async (
};

const fetchInitialData = async (platform: HydratedDocument<IPlatform>) => {
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
Expand All @@ -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 => {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 73e9664

Please sign in to comment.