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

[DEBUG] #140

Merged
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
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 @@ -39,18 +39,22 @@
});

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

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 All @@ -77,7 +81,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 84 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 84 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 @@ -98,10 +102,15 @@
};

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 @@
);

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 @@

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 Expand Up @@ -207,7 +223,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 226 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 226 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 226 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 226 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
Loading