Skip to content

Commit

Permalink
format:
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-rabiei committed Jul 8, 2024
1 parent bc8ad45 commit ba038ca
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/database/services/guildMember.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,5 @@ export default {
deleteGuildMembers,
handelGuildMemberChanges,
getNeededDateFromGuildMember,
getOldestGuildMember,
};
36 changes: 14 additions & 22 deletions src/functions/fetchMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
/* eslint-disable no-unneeded-ternary */
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
import fetch from 'node-fetch';
import {
type Message,
TextChannel,
type User,
type Role,
ThreadChannel,
type Snowflake,
} from 'discord.js';
import { type Message, TextChannel, type User, type Role, ThreadChannel, type Snowflake } from 'discord.js';
import { type IRawInfo, type IPlatform, type IDiscordUser } from '@togethercrew.dev/db';
import { rawInfoService, platformService } from '../database/services';
import { type Connection, type HydratedDocument } from 'mongoose';
Expand Down Expand Up @@ -56,7 +49,7 @@ async function getReactions(message: Message): Promise<string[]> {
// Standard emoji: just encode the name
encodedEmoji = encodeURIComponent(emoji.name);
} else {
logger.error({ message_id: message.id,emoji }, 'Emoji name is null or undefined.');
logger.error({ message_id: message.id, emoji }, 'Emoji name is null or undefined.');

continue;
}
Expand All @@ -74,28 +67,33 @@ async function getReactions(message: Message): Promise<string[]> {
}
}


/**
* Fetches all users who reacted with a specific emoji on a message using pagination.
* @param {string} channelId - The ID of the channel containing the message.
* @param {string} messageId - The ID of the message containing the reactions.
* @param {string} encodedEmoji - The URL-encoded emoji string.
* @returns {Promise<{ id: string; username: string; discriminator: string; avatar: string | null }[]>} - A promise that resolves to an array of user objects. */
async function fetchAllUsersForReaction(channelId: string, messageId: string, encodedEmoji: string): Promise<IDiscordUser[]> {
let users:IDiscordUser[] = [];
async function fetchAllUsersForReaction(
channelId: string,
messageId: string,
encodedEmoji: string,
): Promise<IDiscordUser[]> {
let users: IDiscordUser[] = [];
let after = '';
const limit = 100;
let hasMore = true;

while (hasMore) {
const url = `https://discord.com/api/v9/channels/${channelId}/messages/${messageId}/reactions/${encodedEmoji}?limit=${limit}${after ? `&after=${after}` : ''}`;
const url = `https://discord.com/api/v9/channels/${channelId}/messages/${messageId}/reactions/${encodedEmoji}?limit=${limit}${
after ? `&after=${after}` : ''
}`;
const response = await fetch(url, {
method: 'GET',
headers: {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Authorization: `Bot ${config.discord.botToken}`,
'Content-Type': 'application/json'
}
'Content-Type': 'application/json',
},
});

if (response.ok) {
Expand All @@ -107,14 +105,13 @@ async function fetchAllUsersForReaction(channelId: string, messageId: string, en
after = fetchedUsers[fetchedUsers.length - 1].id;
}
} else {
logger.error({ message_id: messageId, error:await response.text() }, 'Error fetching users for reaction');
logger.error({ message_id: messageId, error: await response.text() }, 'Error fetching users for reaction');
hasMore = false;
}
}
return users;
}


// /**
// * Fetches reaction details from a message.
// * @param {Message} message - The message object from which reactions are to be fetched.
Expand Down Expand Up @@ -142,10 +139,6 @@ async function fetchAllUsersForReaction(channelId: string, messageId: string, en
// }
// }





/**
* Extracts necessary data from a given message.
* @param {Message} message - The message object from which data is to be extracted.
Expand Down Expand Up @@ -369,4 +362,3 @@ export default async function handleFetchMessages(connection: Connection, platfo
logger.error({ guild_id: platform.metadata?.id, error }, 'Failed to fetch messages');
}
}

2 changes: 1 addition & 1 deletion src/rabbitmq/events/fetchEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const fetchMethod = async (msg: any): Promise<void> => {
await Promise.all([
fetchMembers(connection, platform),
fetchChannels(connection, platform),
fetchRoles(connection, platform)
fetchRoles(connection, platform),
]);
} else {
addGuildExtraction(platform);
Expand Down
5 changes: 4 additions & 1 deletion src/rabbitmq/events/fetchMembersEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type HydratedDocument } from 'mongoose';
import fetchMembers from '../../functions/fetchMembers';
import fetchChannels from '../../functions/fetchChannels';
import fetchRoles from '../../functions/fetchRoles';
import guildMemberService from '../../database/services/guildMember.service';
import { Event, MBConnection } from '@togethercrew.dev/tc-messagebroker';
import parentLogger from '../../config/logger';
import { platformService } from '../../database/services';
Expand All @@ -14,10 +15,12 @@ const logger = parentLogger.child({
const fetchInitialData = async (platform: HydratedDocument<IPlatform>): Promise<void> => {
try {
const connection = await DatabaseManager.getInstance().getTenantDb(platform.metadata?.id);
console.log(await guildMemberService.getOldestGuildMember(connection, {}));

await Promise.all([
fetchMembers(connection, platform),
fetchChannels(connection, platform),
fetchRoles(connection, platform)
fetchRoles(connection, platform),
]);
} catch (error) {
logger.error({ error }, 'fetchInitialData is failed');
Expand Down
1 change: 0 additions & 1 deletion src/scripts/deleteRnDAOGuildCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ async function deleteRnDAOGuildCommands(): Promise<void> {
await rest.delete(Routes.applicationGuildCommand(config.discord.clientId, '915914985140531240', command.id));
});
} catch (error) {
console.log(error);
logger.error('Failed to delete RnDAO guild commands', error);
}
}
Expand Down

0 comments on commit ba038ca

Please sign in to comment.