diff --git a/src/services/discord/core.service.ts b/src/services/discord/core.service.ts index e39db204..11b99f3f 100644 --- a/src/services/discord/core.service.ts +++ b/src/services/discord/core.service.ts @@ -20,27 +20,24 @@ const logger = parentLogger.child({ module: 'DiscordService' }); async function getPropertyHandler(req: IAuthAndPlatform) { const connection = await DatabaseManager.getInstance().getTenantDb(req.platform?.metadata?.id); - - if (req.query.property === 'role') { const filter = pick(req.query, ['name']); if (filter.name) { filter.name = { $regex: filter.name, - $options: 'i' + $options: 'i', }; } filter.deletedAt = null; const options = pick(req.query, ['sortBy', 'limit', 'page']); - return await roleService.queryRoles(connection, filter, options) - } - else if (req.query.property === 'channel') { + return await roleService.queryRoles(connection, filter, options); + } else if (req.query.property === 'channel') { const filter = pick(req.query, ['name']); if (filter.name) { filter.name = { $regex: filter.name, - $options: 'i' + $options: 'i', }; } filter.deletedAt = null; @@ -49,12 +46,35 @@ async function getPropertyHandler(req: IAuthAndPlatform) { } const channels: any = await channelService.getChannels(connection, filter); for (let i = 0; i < channels.length; i++) { - const canReadMessageHistoryAndViewChannel = await channelService.checkBotPermissions(req.platform?.metadata?.id, channels[i], [discord.permissions.ReadData.ViewChannel, discord.permissions.ReadData.ReadMessageHistory]); + const canReadMessageHistoryAndViewChannel = await channelService.checkBotPermissions( + req.platform?.metadata?.id, + channels[i], + [discord.permissions.ReadData.ViewChannel, discord.permissions.ReadData.ReadMessageHistory], + ); let announcementAccess: boolean; if (channels[i].type === 0 || channels[i].type === 4) { - announcementAccess = await channelService.checkBotPermissions(req.platform?.metadata?.id, channels[i], [discord.permissions.Announcement.ViewChannel, discord.permissions.Announcement.SendMessages, discord.permissions.Announcement.SendMessagesInThreads, discord.permissions.Announcement.CreatePrivateThreads, discord.permissions.Announcement.CreatePublicThreads, discord.permissions.Announcement.EmbedLinks, discord.permissions.Announcement.AttachFiles, discord.permissions.Announcement.MentionEveryone]); + announcementAccess = await channelService.checkBotPermissions(req.platform?.metadata?.id, channels[i], [ + discord.permissions.Announcement.ViewChannel, + discord.permissions.Announcement.SendMessages, + discord.permissions.Announcement.SendMessagesInThreads, + discord.permissions.Announcement.CreatePrivateThreads, + discord.permissions.Announcement.CreatePublicThreads, + discord.permissions.Announcement.EmbedLinks, + discord.permissions.Announcement.AttachFiles, + discord.permissions.Announcement.MentionEveryone, + ]); } else { - announcementAccess = await channelService.checkBotPermissions(req.platform?.metadata?.id, channels[i], [discord.permissions.Announcement.ViewChannel, discord.permissions.Announcement.SendMessages, discord.permissions.Announcement.SendMessagesInThreads, discord.permissions.Announcement.CreatePrivateThreads, discord.permissions.Announcement.CreatePublicThreads, discord.permissions.Announcement.EmbedLinks, discord.permissions.Announcement.AttachFiles, discord.permissions.Announcement.MentionEveryone, discord.permissions.Announcement.Connect]); + announcementAccess = await channelService.checkBotPermissions(req.platform?.metadata?.id, channels[i], [ + discord.permissions.Announcement.ViewChannel, + discord.permissions.Announcement.SendMessages, + discord.permissions.Announcement.SendMessagesInThreads, + discord.permissions.Announcement.CreatePrivateThreads, + discord.permissions.Announcement.CreatePublicThreads, + discord.permissions.Announcement.EmbedLinks, + discord.permissions.Announcement.AttachFiles, + discord.permissions.Announcement.MentionEveryone, + discord.permissions.Announcement.Connect, + ]); } channels[i] = { channelId: channels[i].channelId, @@ -62,22 +82,20 @@ async function getPropertyHandler(req: IAuthAndPlatform) { parentId: channels[i].parentId, canReadMessageHistoryAndViewChannel, announcementAccess, - type: channels[i].type - } + type: channels[i].type, + }; } return await sort.sortChannels(channels); - } - - else if (req.query.property === 'guildMember') { + } else if (req.query.property === 'guildMember') { const filter = pick(req.query, ['ngu']); filter.deletedAt = null; const options = pick(req.query, ['sortBy', 'limit', 'page']); - const guildMembers = await guildMemberService.queryGuildMembers(connection, filter, options) + const guildMembers = await guildMemberService.queryGuildMembers(connection, filter, options); guildMembers.results.forEach((guildMember: any) => { guildMember.ngu = guildMemberService.getNgu(guildMember); guildMember.username = guildMemberService.getUsername(guildMember); }); - return guildMembers + return guildMembers; } } @@ -90,7 +108,6 @@ async function getGuildFromDiscordJS(guildId: Snowflake): Promise const client = await DiscordBotManager.getClient(); try { return await client.guilds.fetch(guildId); - } catch (error) { logger.error({ error }, 'Failed to get guild from discordJS'); return null; @@ -98,7 +115,7 @@ async function getGuildFromDiscordJS(guildId: Snowflake): Promise } /** - * leave bot from guild + * leave bot from guild * @param {Snowflake} guildId * @returns {Promise} */ @@ -109,8 +126,6 @@ async function leaveBotFromGuild(guildId: Snowflake) { } } - - /** * exchange discord code with access token * @param {string} code @@ -124,18 +139,17 @@ async function exchangeCode(code: string, redirect_uri: string): Promise try { const response = await fetch('https://discord.com/api/users/@me', { method: 'GET', - headers: { 'Authorization': `Bearer ${accessToken}` } + headers: { Authorization: `Bearer ${accessToken}` }, }); if (response.ok) { return await response.json(); - } - else { + } else { throw new Error(await response.json()); } } catch (error) { @@ -176,12 +189,11 @@ async function getBotFromDiscordAPI(): Promise { try { const response = await fetch('https://discord.com/api/users/@me', { method: 'GET', - headers: { 'Authorization': `Bot ${config.discord.botToken}` } + headers: { Authorization: `Bot ${config.discord.botToken}` }, }); if (response.ok) { return await response.json(); - } - else { + } else { throw new Error(await response.json()); } } catch (error) { @@ -190,7 +202,6 @@ async function getBotFromDiscordAPI(): Promise { } } - /** * get list of permissions that bot has in a specific guild * @param {Snowflake} guildId @@ -203,7 +214,10 @@ async function getBotPermissions(guildId: Snowflake): Promise> { const member = await guild.members.fetch(config.discord.clientId); return member.permissions.toArray(); } catch (error) { - logger.error({ bot_token: config.discord.botToken, error }, 'Failed to get list of permissions that bot has in a specific guild'); + logger.error( + { bot_token: config.discord.botToken, error }, + 'Failed to get list of permissions that bot has in a specific guild', + ); throw new ApiError(590, 'Failed to get list of permissions that bot has in a specific guild'); } } @@ -255,7 +269,7 @@ function getCombinedPermissionsValue(permissionsArray: Array) { * The function iterates through each category of permissions (like ReadData, Announcement) in the * discord object and checks if the given permissions are present in each category. * If a permission is present, it is marked as true, otherwise false. - * + * * @param {string[]} permissionsToCheck - An array of permission names to check against the discord permissions. * @returns {any} An object with each category of permissions containing key-value pairs of permission names and their boolean status (true if present in the array, false otherwise). */ @@ -290,7 +304,6 @@ class DiscordBotManager { ], }); await DiscordBotManager.client.login(config.discord.botToken); - } return DiscordBotManager.client; } @@ -306,5 +319,5 @@ export default { getBotPermissions, getRequirePermissionsForModule, getCombinedPermissionsValue, - getPermissionsStatus -} \ No newline at end of file + getPermissionsStatus, +}; diff --git a/src/utils/sort.ts b/src/utils/sort.ts index a567a0de..2007bc9e 100644 --- a/src/utils/sort.ts +++ b/src/utils/sort.ts @@ -1,9 +1,9 @@ function sortChannels(channels: any[]) { const sortedChannels: any[] = []; const unCategorized: any = { - channelId: "0", - title: "unCategorized", - subChannels: [] + channelId: '0', + title: 'unCategorized', + subChannels: [], }; for (const channel of channels) { @@ -17,7 +17,12 @@ function sortChannels(channels: any[]) { }); } else { unCategorized.subChannels.push({ - announcementAccess: channel.announcementAccess, canReadMessageHistoryAndViewChannel: channel.canReadMessageHistoryAndViewChannel, channelId: channel.channelId, parentId: channel.channelId, name: channel.name, type: channel.type + announcementAccess: channel.announcementAccess, + canReadMessageHistoryAndViewChannel: channel.canReadMessageHistoryAndViewChannel, + channelId: channel.channelId, + parentId: channel.channelId, + name: channel.name, + type: channel.type, }); } } @@ -40,10 +45,7 @@ function sortByHandler(sortBy: string): Record { return sortParams; } - - - export default { sortChannels, sortByHandler, -} \ No newline at end of file +};